【问题标题】:Why is this rails simple form not working?为什么这种导轨简单形式不起作用?
【发布时间】:2013-10-21 20:16:03
【问题描述】:

我正在制作一个 Rails 应用程序。用户注册后(我已经使用 devise 创建了用户注册),他们可以填写此表格,其中包含他们的个人资料信息。我已经这样做了好几次了,我找不到问题所在。这是模型:

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#create,这是表单应该去的,它有一个空白路径。还有informations#index,我猜这就是它现在的样子。如果路径为空白,我如何让它去 informations#create?

【问题讨论】:

  • ERB 的语法有问题。现在尝试摆脱 cmets。另外,您是否使用 IDE 来提醒您注意格式错误的 ERB?
  • 我没有使用 IDE。我正在使用崇高。我摆脱了 cmets 并使用新错误编辑了问题。

标签: ruby-on-rails forms routes simple-form rails-routing


【解决方案1】:

请尝试在视图的第 6 行和第 9 行中拉出 cmets(# 符号)。他们可能会搞乱 ERB 处理。

【讨论】:

  • 谢谢。我试过了,现在我得到了错误:#:0x007f9c00c7b3e0> 的未定义方法`information_index_path'
  • 是时候进行一些rake routes 操作了。
  • 有趣。当我做 rake 路线时,对于 informations#create,这是表单应该去的,它有一个空白路径。还有informations#index,我猜这就是它现在的样子。如果路径为空白,我如何让它转到 informations#create?
  • 请使用routes.rb 编辑您的问题,或接受@Dave 的回复并提出新问题。
【解决方案2】:

你能试试informations_path吗?见here

【讨论】:

  • 其实我看了一会儿。你具体指的是哪一部分?你的意思是“simple_form_for informations_path”吗?
  • 是的,我想通了。只要确保你看到了那个参考——特别是第 2.3 节。没看到你是怎么使用路径的,但是你用information_index_path的地方,换成informations_path试试。
  • 我从不使用 information_index_path。它会自动带我去那里。我想去 informations#create 但由于某种原因 routes.rb 文件没有创建该路由。
  • 我很困惑。您是说没有名为new_information_path 的路径吗?也许你想要simple_form_for new_information_path。我没用过simple_form_for
  • 我跑了 rake 路线。 informations#create 没有路径,我不知道为什么,因为在 routes.rb 文件中我说 resources :informations 应该像其他信息路由一样自动创建它。
【解决方案3】:

问题在于命名资源信息。由于信息的复数形式与单数形式相同,因此令人困惑。我重命名了模型描述和控制器 descriptions_controller,它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    相关资源
    最近更新 更多