Ruby-on-rails 使用 rails form_for 时带有“_path”的未定义方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8706774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:33:39  来源:igfitidea点击:

Undefined method with "_path" while using rails form_for

ruby-on-railsruby

提问by Alekx

I'm running into a (I think) routing error while using the Rails form_for helper. I have been searching around and looked at this question, but the plural for "static_event" with pluralize is "static_events" so I am at a loss. Any help would be apprecited. Here are the details....

我在使用 Rails form_for 帮助程序时遇到了(我认为)路由错误。我一直在四处寻找并查看这个问题,但是“static_event”与复数的复数是“static_events”,所以我不知所措。任何帮助将不胜感激。以下是详细信息......

ActionView::Template::Error (undefined method `static_events_path' for #<#<Class:0x007f9fcc48a918>:0x007f9fcc46fa78>):

My Model:

我的型号:

class StaticEvent < ActiveRecord::Base
attr_accessible :content, :title, :discount, :location, :day_of_week, :start_time

My Controller:

我的控制器:

    class StaticEventsController < ApplicationController

  before_filter :authenticate, :only => [:create, :destroy]
  before_filter :authorized_user, :only => [:destroy] 


  def new
    @title = "Share An Event"
    @static_event = StaticEvent.new 
  end

  def create
    @static_event = current_user.static_events.build(params[:event])
    if @static_event.save
      flash[:success] = "Event Shared"
      redirect_to @static_event #this was the old version
    else
      render :new
    end
  end

The route:

路线:

match '/static-events/new', :to => 'static_events#new'
match '/static-events/',     :to => 'static_events#index'
match '/static-events/:id', :to => 'static_events#show'

The view

风景

<%= form_for (@static_event) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= text_field "static_event", "title", "size" => 48 %>
<%= time_select "static_event", "start_time", {:ampm => true, :minute_step => 15} %>
<%= text_area "static_event", "content", "cols" => 42, "rows" => 5 %>
<%= text_field "static_event", "discount", "size" => 48 %>
<%= text_field "static_event", "location", "size" => 48 %>
<%= text_field "static_event", "day_of_week", "size" => 48 %>
<input name="" type="submit" class="button" value="share on chalkboard" />
<% end %>

采纳答案by Fábio Batista

Only routes created using the resourcesmethod are automatically named.

只有使用该resources方法创建的路由才会自动命名。

If you want to name your routes, use the :asoption:

如果要命名路由,请使用以下:as选项:

match '/static-events/new', :to => 'static_events#new', :as => :new_static_event
match '/static-events/',     :to => 'static_events#index', :as => :static_events
match '/static-events/:id', :to => 'static_events#show', :as => :static_event

However, it's better to use the resourcesmethod. You must pass the "true" name of your model as the first parameter, then override the path if you want:

但是,最好使用该resources方法。您必须将模型的“真实”名称作为第一个参数传递,然后根据需要覆盖路径:

resources :static_events, :path => 'static-events'

回答by basgys

First of all, you should define your routes this way:

首先,你应该这样定义你的路由:

resources 'static-events', :only => [:new, :create]

This will create a route for new and create methods.

这将为 new 和 create 方法创建一个路由。

Because when you use a new ActiveRecord object as an argument to form for, it will looks for *s_path like static_events_path in your routes file with the POST verb.

因为当你使用一个新的 ActiveRecord 对象作为 form for 的参数时,它会在你的路由文件中使用 POST 动词寻找 *s_path 之类的 static_events_path 。

I think the way you have defined your routes doesn't create the static_events_path with POST verb (you can check that by using rake routes as megas said). So don't use match anymore, use resources or get/post/... instead of match in your Rails 3 projects.

我认为您定义路由的方式不会使用 POST 动词创建 static_events_path (您可以通过使用 megas 所说的 rake 路由来检查它)。所以不要再使用 match,在 Rails 3 项目中使用资源或 get/post/... 而不是 match。

EDIT

编辑

I did not notice yesterday, but there is no route for create method. Add the route below before static_events#indexor remove all your routes and do like I said above.

昨天没注意,但是没有create方法的路由。在static_events#index之前添加下面的路由或删除所有路由,然后像我上面说的那样做。

post '/static-events/', :to => 'static_events#create'

回答by megas

Run rake routesand you'll see the list of your routes. Then you can fix the route file to have appropriate route path.

运行rake routes,你会看到你的路线列表。然后您可以修复路由文件以具有适当的路由路径。

回答by Doug

This happened to me when i was using a nested resource, but forgot to actually initialize the parent resource using load_and_authorize_resourcein cancan. Therefore, the parent resource was null and it threw this error.

当我使用嵌套资源时,这发生在我身上,但忘记load_and_authorize_resource在 cancan 中使用实际初始化父资源。因此,父资源为空并抛出此错误。

I fixed it by declaring load_and_authorize_resourceon the parent in the controller.

我通过load_and_authorize_resource在控制器中声明父级来修复它。