在Rails应用程序嵌套形式的错误 [英] Nested form error in Rails app
问题描述
有关这个问题的模式:用户,朋友,兴趣,person_interest。 Person_interest是polymoprhic并且可以存储或者属于一个用户或朋友的利益。
Models relevant to this question: user, friend, interest, person_interest. Person_interest is polymoprhic and can store interests that either belong to a user or a friend.
兴趣爱好是pre填充了约。 30的利益。当他们被要求用户注册,选择自己的利益,然后在较低的形式进入他们的朋友的名字选择该人的利益。
Interests is pre-populated with approx. 30 interests. When a user registers they are asked to select their own interests, then lower in the form enter in their friends names select the interests of that person.
试图与&LT来实现这一点;%= friend_f.input:利益:为=> :check_boxes,:标签=>假%>
但得到以下错误:
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%>
混帐回购协议是在这里: https://github.com/nelsonkeating/ReminDeal
推荐答案
你有什么理由,以避免 friend_f.association
?
Do you have any reasons to avoid friend_f.association
?
<%= friend_f.association :interests, :as => :check_boxes, :label => false %>
我的作品。
或更改:利益
到:interest_ids
在卡斯帕的code,也应努力
Works for me.
Or change :interests
to :interest_ids
in kasper's code, also should work.
<%= friend_f.input :interest_ids, :as => :check_boxes, :label => false, :collection => Interest.all %>
这篇关于在Rails应用程序嵌套形式的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!