gpt4 book ai didi

ruby-on-rails - 使用 rails form_for 时使用 "_path"的未定义方法

转载 作者:数据小太阳 更新时间:2023-10-29 06:24:34 24 4
gpt4 key购买 nike

我在使用 Rails form_for 助手时遇到了(我认为)路由错误。我一直在四处寻找并查看this question ,但是带有复数形式的“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

我的 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

路线:

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 %>

最佳答案

只有使用 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'

关于ruby-on-rails - 使用 rails form_for 时使用 "_path"的未定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8706774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com