0

我正在制作一个 Rails 应用程序。用户注册后(我已经使用 devise 创建了用户注册),他们可以填写包含其个人资料信息的表格。表单应该向信息控制器中的创建操作提交一个发布请求,但由于某种原因,我无法正确配置路由。当我运行 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

我收到以下错误:

#<#:0x007f9c00c7b3e0> 的未定义方法“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)
4

3 回答 3

1

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

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

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.uncountable %w( information )
end
于 2013-10-23T04:24:30.510 回答
0

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

于 2013-10-21T22:25:23.013 回答
0

您的表格应该针对一个单一的信息对象。不适用于信息对象的集合。请注意,我已更改simple_form_for @informationssimple_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. 路径生成错误,因为您正在使用对象集合。

于 2013-10-21T21:11:08.723 回答