为什么routes.rb文件中的资源没有创建所有路由?

时间:2013-10-21 21:02:23

标签: ruby-on-rails routes crud rails-routing activeresource

我正在制作一个rails应用程序。用户注册后(我有 已经用设计创建了用户注册),他们可以填写这个 包含其个人资料信息的表单。表格应该 向信息控制器中的创建操作提交发布请求,但由于某种原因,我无法正确配置路由。当我运行rake路由时,对于信息#create,表单应该是什么,它有一个空白路径。还有信息#index,这就是我现在想的。我如何获取表单转到信息#create路径是否为空?我已经好几次这样做了,我找不到有什么问题。这是模型:

class Information < ActiveRecord::Base
    belongs_to :user
end

这是控制器:

class InformationsController < ApplicationController

    def new
        @information = Information.new
    end
    def create
        @information = Information.create(params[:information])
        redirect_to student_path
    end
    def index
    end
end

以下是新行动的观点。

<div class="span6 offset3 text-center">
<h1>Edit your information</h1>

    <%= simple_form_for @information do |f| %>
        <%= f.input :skills %>


        <%= f.input :looking_for, :label => 'What help do you need?' %>
        <%= f.input :my_idea %>
        <%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
    <% end %>
</div>

以下是路线文件中的行:

resources :informations

我收到以下错误:

#&lt;#:0x007f9c00c7b3e0&gt;

的未定义方法`information_index_path'

这是文件的rake路线

 informations GET    /informations(.:format)          informations#index
             POST   /informations(.:format)          informations#create

尝试加载显示表单的页面时,这是我的完整堆栈跟踪:

Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
  Rendered informations/new.html.erb within layouts/application (64.2ms)
Completed 500 Internal Server Error in 70ms

ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8b23e8>):
    2:  <div class="span6 offset3 text-center">
    3:  <h1>Edit your information</h1>
    4: 
    5:      <% simple_form_for @information do |f| %>
    6:          <%= f.input :skills %>
    7:          
    8:          
  app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334909089600'


  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.1ms)


Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
  Rendered informations/new.html.erb within layouts/application (8.3ms)
Completed 500 Internal Server Error in 13ms

ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8708a8>):
    2:  <div class="span6 offset3 text-center">
    3:  <h1>Edit your information</h1>
    4: 
    5:      <% simple_form_for @information do |f| %>
    6:          <%= f.input :skills %>
    7:          
    8:          
  app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334908978600'


  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
  Rendered /usr/local/rvm/gems/ruby-2.0.0-p247@firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (14.0ms)

3 个答案:

答案 0 :(得分:1)

您的问题与信息这个词既是复数形式又是单数形式有关。就像 water paper 证据一样。 Rails会考虑这些词uncountable

您可以执行以下两项操作:将模型命名为其他内容或编辑文件config/initializers/inflections.rb,如下所示:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.uncountable %w( information )
end

答案 1 :(得分:0)

您的表单应该是一个信息对象。不适用于信息对象的集合。请注意,我已将simple_form_for @informations更改为simple_form_for @information

<div class="span6 offset3 text-center">
<h1>Edit your information</h1>

    <% simple_form_for @information do |f| %>
        <%= f.input :skills %>


        <%= f.input :looking_for, :label => 'What help do you need?' %>
        <%= f.input :my_idea %>
        <%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
    <% end %>
</div>

你的失败也不是因为rails没有生成information_index_path。这是因为信息#index的命名路由是informations_path。路径生成错误,因为您正在使用对象集合。

答案 2 :(得分:0)

不确定这是否有帮助,但尝试在simple_form_for前面添加一个=。这样它将具有&lt;%=而不是&lt;%