在使用rails form_for时出现了"_path"未定义的方法错误

40

我在使用Rails的form_for辅助函数时遇到(我认为是)路由错误。我已经搜索了一下并查看了这个问题,但是使用pluralize(复数形式)将"static_event"变成"static_events",所以我无法解决问题。任何帮助都将不胜感激。以下是细节....

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

我的模型:

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

我的控制器:

    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

路线:

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

视图

<%= 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 %>
4个回答

33

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

如果您想要命名您的路由,请使用: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

然而,更好的方法是使用 resources 方法。您必须将您的模型真实的名称作为第一个参数传递,然后如果需要可以覆盖路径:

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

在Rails 4中,您需要使用“via”指定HTTP方法。 - courtsimas
1
顺便提一下,对于嵌套路由,您需要传递一对值,因此 form_for ([@static_event,@sub_event]) - Jason Axelson
在Rails5中,将resources :static_events添加到routes.rb文件中。 - Youness

9

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

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

这将创建一个新的路由和create方法。
因为当您使用一个新的ActiveRecord对象作为form_for的参数时,它会在您的路由文件中查找类似于static_events_path的*s_path与POST动词匹配。
我认为您定义路由的方式没有使用POST动词来创建static_events_path(您可以使用megas提到的rake routes来检查)。所以在您的Rails 3项目中不再使用match,而是使用resources或get/post/...。
编辑
昨天我没有注意到,但是现在没有create方法的路由。在static_events#index之前添加下面的路由或删除所有的路由并按照我上面说的做。
post '/static-events/', :to => 'static_events#create'

:static-events 不是一个有效的符号。 - Lee Jarvis
你说得对,injekt,符号不能包含破折号。我已经修正了这个错误。 - basgys

6
运行rake routes命令,您将看到路由列表。然后,您可以修复路由文件以获得适当的路由路径。

从我所看到的,我的路由看起来很准确。 - Alekx
好的调试技巧 - 这帮了我。 - infl3x

3
当我使用嵌套资源时,出现了这种情况,但我忘记了在cancan中使用load_and_authorize_resource来初始化父资源。因此,父资源为空,导致出现了此错误。
我通过在控制器中声明load_and_authorize_resource来修复它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接