在使用Rails form_作为助手时,我(我想)遇到了一个路由错误.我一直在四处搜索,看了this question个,但是"static_events"和pluralize的复数形式是"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 %>

推荐答案

只有使用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法.必须将模型的"true"名称作为第一个参数传递,然后根据需要覆盖路径:

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

Ruby-on-rails相关问答推荐

Rails TurboStream:可以向元素动态添加一个css类吗?

方法和括号之间没有空格时出错

Rails版本7.1.2:当验证失败且控制器发送422状态时,JS停止工作

错误:部署到 Heroku 时找不到模块tailwindcss/defaultTheme

复制我的活动时,活动描述没有复制过来

这种使用on:的语法在 Ruby on Rails 中意味着什么?

如何在 YAML 中声明带有单引号和双引号的字符串?

使用 Rspec 测试 Rails 3.1 可安装引擎

理解 Gemfile.lock:删除 Gemfile.lock 然后再次运行 bundle install 可以吗?

如何更改 Heroku 应用程序的 DATABASE_URL

RSpec 设置会话对象

如何避免 has_many :through 关系中的重复?

如何在 Ruby on Rails 中的 cookie 上设置 HttpOnly 标志

Rails Devise:设置密码重置令牌并重定向用户

Rails 4模块的未初始化常量

'respond_to' 格式语句中的 `:location => ...` 和 `head :ok` 是什么意思?

Heroku - 在浏览器中显示当前提交的哈希

Rails:为什么 with_exclusive_scope 受保护?关于如何使用它的任何好的做法?

ruby on rails 如何处理 NaN

为什么 Mac OS X 带有 ruby​​/rails?