【问题标题】:Why is resources in the routes.rb file not creating ALL of the routes?为什么 routes.rb 文件中的资源没有创建所有路由?
【发布时间】:2013-10-21 21:02:23
【问题描述】:

我正在制作一个 Rails 应用程序。用户注册后(我有 已经用设计创建了用户注册),他们可以填写这个 将包含他们的个人资料信息的表格。表格应 向信息控制器中的创建操作提交发布请求,但由于某种原因,我无法正确配置路由。当我运行 rake 路由时,对于 informations#create,这是表单应该去的,它有一个空白路径。还有informations#index,我猜这就是它现在的样子。如果路径为空白,如何让表单转到 informations#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

我收到以下错误:

# 的未定义方法 `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)

【问题讨论】:

  • 错误来自哪一行?
  • 请发布您的完整堆栈跟踪
  • 我不确定堆栈跟踪是什么。当我访问该页面时,您的意思是终端中显示的内容吗?
  • 是的,整个例外
  • 好的。我进行了编辑并添加了堆栈跟踪。

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


【解决方案1】:

您的问题与 information 这个词既是复数形式又是单数形式有关。就像,或,或证据。 Rails 会考虑这些词 uncountable

您可以做两件事:将模型命名为其他名称或编辑文件config/initializers/inflections.rb,如下所示:

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

【讨论】:

    【解决方案2】:

    您的表单应该针对一个单一的信息对象。不适用于信息对象的集合。请注意,我已将 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。这是因为 information#index 的命名路由是informations_path。路径生成错误,因为您使用的是对象集合。

    【讨论】:

    • 不,我把它打错了堆栈溢出。在文档中我有@information。
    【解决方案3】:

    不确定这是否会有所帮助,但请尝试在 simple_form_for 前面添加一个 =。这样它将有一个

    【讨论】:

      猜你喜欢
      • 2016-09-24
      • 2012-12-26
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多