我在使用Rails form_for helper時遇到了(我認爲)路由錯誤。我一直在四處搜尋,看着this question,但複數的「static_event」是「static_events」,所以我很茫然。任何幫助將被認識。下面是詳細信息....使用rails時使用「_path」的未定義方法form_for
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 %>
太好了,我已經更新了它。謝謝Fábio! – Alekx 2012-01-04 06:15:51
在rails 4中,您需要指定http方法以及'via' – courtsimas 2013-10-23 14:48:27
作爲一個方面,對於嵌套路由,您必須傳遞一對值以便'form_for([@ static_event,@ sub_event] )' – 2014-01-29 02:23:03