【问题标题】:Nested form error in Rails appRails 应用程序中的嵌套表单错误
【发布时间】:2012-05-19 01:32:17
【问题描述】:

与此问题相关的模型:用户、朋友、兴趣、person_interest。 Person_interest 是多态的,可以存储属于用户或朋友的兴趣。

兴趣预先填充了大约。 30个兴趣。当用户注册时,他们被要求选择自己的兴趣,然后在表格下方输入他们的朋友姓名选择该人的兴趣。

尝试通过<%= friend_f.input :interests, :as => :check_boxes, :label => false %>实现这一目标

但出现以下错误:

NoMethodError in User_steps#show

Showing /Users/nelsonkeating/Desktop/ReminDeal/app/views/user_steps/show.html.erb where line #30 raised:

undefined method `to_i' for []:ActiveRecord::Relation
Extracted source (around line #30):

27:         :end_year => Date.today.year - 12,
28:         :order => [:month, :day, :year ] %>
29:       <%= friend_f.input :gender, :collection => ['male','female'] %>
30:       <%= friend_f.input :interests, :as => :check_boxes, :label => false %>
31:     <%end%>
32:   
33: 
Rails.root: /Users/nelsonkeating/Desktop/ReminDeal

Application Trace | Framework Trace | Full Trace
app/views/user_steps/show.html.erb:30:in `block (2 levels) in _app_views_user_steps_show_html_erb__1202886937753978667_70281885932700'
app/views/user_steps/show.html.erb:23:in `block in _app_views_user_steps_show_html_erb__1202886937753978667_70281885932700'
app/views/user_steps/show.html.erb:1:in `_app_views_user_steps_show_html_erb__1202886937753978667_70281885932700'
Request

Parameters:

{"id"=>"show"}

这是表格..

<%= simple_form_for @user do |f| %>

  <%= f.input :name %>
  <%= f.input :city %>
  <%= f.input :address, :live => true  %>
  <%= f.input :zipcode %>
  <%= f.input :date_of_birth, :as => :date, :start_year => 1900,
    :end_year => Date.today.year - 12,
    :order => [ :day, :month, :year] %>
  <%= f.input :gender, :collection => [:male, :female] %>


    <h4>Select your top 3 interests..</h4>
      <%= f.association :interests, :as => :check_boxes, :label => false %>

    <h4>What holidays do you celebrate?</h4>
      <%= f.association :holidays, :as => :check_boxes, :label => false %> 



<h4>Add up to 10 friends birthdays that you would like to remember..</h4>
    <%= f.simple_fields_for :friends do |friend_f| %>

      <%= friend_f.input :name %>
      <%= friend_f.input :dob, :label => :Birthday, :as => :date, :start_year => Date.today.year - 90,
        :end_year => Date.today.year - 12,
        :order => [:month, :day, :year ] %>
      <%= friend_f.input :gender, :collection => ['male','female'] %>
      <%= friend_f.input :interests, :as => :check_boxes, :label => false %>
     <%end%>


 <%= f.button :submit, :class => 'btn btn-success' %>
      <%end%>

Git 仓库在这里:https://github.com/nelsonkeating/ReminDeal

【问题讨论】:

  • 所以它的这一行 " :check_boxes, :label => false %>"...

标签: ruby-on-rails ruby-on-rails-3 activerecord simple-form


【解决方案1】:

你有什么理由回避friend_f.association

<%= friend_f.association :interests, :as => :check_boxes, :label => false %>

为我工作。 或者在 kasper 的代码中将:interests 更改为:interest_ids,也应该可以工作。

<%= friend_f.input :interest_ids, :as => :check_boxes, :label => false, :collection => Interest.all %>

【讨论】:

    【解决方案2】:

    尝试使用:collection 键编写代码&lt;%= friend_f.input :interests, :as =&gt; :check_boxes, :label =&gt; false %&gt;。 例如:

    <%= friend_f.input :interests, :as => :check_boxes, :label => false, :collection => Interest.all %>
    

    【讨论】:

    • 这给了我以下错误:“User_steps#show 中的 NameError .....未初始化的常量 ActionView::CompiledTemplates::Interests”
    • 在 kasper 的代码中,将 :collection =&gt; Interests.all 更改为 :collection =&gt; Interest.all 并尝试一下。模型应该是单数的。
    • 我一直这样做。希望有帮助!如果这不能解决问题,我可能有其他解决方案。
    • 感谢您的回答!.. 现在我得到“[]:ActiveRecord::Relation 的未定义方法 `to_i'”
    • @StephRose 你的 alt 是什么。主意??再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多