我有一个嵌套属性message.rb
class Message < ActiveRecord::Base
belongs_to :trip
attr_accessible :name, :email, :subject, :body
end
属于trip.rb
class Trip < ActiveRecord::Base
has_many :messages
accepts_nested_attributes_for :messages, :allow_destroy => true
attr_accessible :messages_attributes
end
messages_controller.rb
class MessagesController < ApplicationController
def create
@trip = Trip.find(params[:trip_id])
@message = @trip.messages.create(params[:message])
redirect_to trip_path(@trip)
end
end
消息表单位于\ trips \ show.html.erb
中 <%= form_for([@trip, @trip.messages.build]) do |f| %>
<div class="field">
<%= f.label :name%><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :subject %><br />
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
它应该出现在那里\ trips \ show.html.erb
<h2>Messages</h2>
<% @trip.messages.each do |message| %>
<p>
<b>NAme:</b>
<%= message.name %>
</p>
<b>Email:</b>
<%= message.email %>
</p>
<b>Subject:</b>
<%= message.subject %>
</p>
<b>Message:</b>
<%= message.body %>
</p>
<% end %>
问题在于它没有。
我的开发。日志。
开始GET“/ trips / 44?utf8 =%E2%9C%93&amp; authenticity_token =%2F0Qs42AHHm9dpO17xfJsu5LSaYCKIschXu3boFjRg54%3D&amp; message&gt;%5Bname%5D = emils&amp; message%5Bemail%5D = emils.veveris%40inbox.lv&amp;消息%5Bsubject%5D = aasdasda&gt; ds&amp; message%5Bbody%5D = aaaaaaaaaaaaaaaaaaaaaaaaaa&amp; commit =创建+消息“for 127.0.0.1 at 2012-07-31 23:11:16 +0300 由TripsController处理#show as HTML 参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“/ 0Qs42AHHm9dpO17xfJsu5LSaYCKIschXu3boFjRg54 =”,“message”=&gt;&gt;&gt;&gt; &GT; {“name”=&gt;“emils”,“email”=&gt;“emils.veveris@inbox.lv”,“subject”=&gt;“aasdasdads”,“body”=&gt;“aaaaaaaaaaaaaaaaaaaaaaaaaaa”},“提交“=&gt;”创建消息“,”id“=&gt;”44“} [1m [35mTrip Load(0.0ms)[0m SELECT“trip”。* FROM“trip”WHERE“trip”。“id”=?限制1 [[“id”,“44”]] [1m [36mCategory Load(0.0ms)[0m [1mSELECT“categories”。* FROM“categories”INNER JOIN“categories_trips”ON“categories”。“id”=“categories_trips”。“category_id”WHERE“categories_trips”。“trip_id “= 44 [0m [1m [35mLocation Load(0.0ms)[0m SELECT“locations”。* FROM“locations”WHERE&gt; “locations”。“id”= 18 LIMIT 1 [1m [36mCACHE(0.0ms)[0m [1mSELECT“trip”。* FROM“trip”WHERE“trip”。“id”=?限制1 [0m [1m [35mImage Load(1.0ms)[0m SELECT“images”。* FROM“images”WHERE“images”。“trip_id”= 44 [1m [36mMessage Load(0.0ms)[0m [1mSELECT“messages”。* FROM“messages”WHERE“messages”。“trip_id”= 44 [0m] [1m [35mAccomodation Load(0.0ms)[0m SELECT“accomodations”。* FROM“accomodations”WHERE“accomodations”。“trip_id”= 44 [1m [36mLesson载入(1.0ms)[0m [1mSELECT“课程”。* FROM“课程”在哪里“课程”。“trip_id”= 44 [0m [1m [35mDetail Load(0.0ms)[0m SELECT“details”。* FROM“details”INNER JOIN“details_trips”ON“details”。“id”=“details_trips”。“detail_id”WHERE“details_trips”。“trip_id” = 44 [1m [36mUser Load(0.0ms)[0m [1mSELECT“users”。* FROM“users”WHERE“users”。“id”= 3 LIMIT 1 [0m] 渲染C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/gmaps4rails-1.5.2/app/views/gmaps4rails/_gmaps4rails.html.erb(1.0ms) [1m [35mReview Load(0.0ms)[0m SELECT“评论”。* FROM“评论”WHERE“评论”。“trip_id”= 44 在布局/应用程序中渲染trip / show.html.erb(261.0ms) 渲染布局/ _shim.html.erb(0.0ms) 渲染布局/ _header.html.erb(3.0ms) 渲染布局/ _footer.html.erb(1.0ms) 在404ms完成200 OK(浏览次数:295.0ms | ActiveRecord:9.0ms)
我真的不明白为什么,我确实设置了attr_accessible。你能帮我吗 ? 谢谢!
通过rails console它可以工作:
IRB(主):019:0&GT; first_trip.messages.create(name:“emils2”) ←[1m←[35mSQL(2.0ms)←[0m INSERT INTO“messages”(“body”,“created_at”,“email” “,”name“,”subject“,”trip_id“,”updated_at“)VALUES(?,?,?,?,?,?,?)[[” body“,nil],[”created_at“,Wed,01 Aug 2012 13:52:29 UTC +00:00],[”email“,nil ],[“name”,“emils2”],[“subject”,nil],[“trip_id”,32],[“updated_at”,Wed,01 2012年8月13:52:29 UTC +00:00]]
32,created_at:“2012-08-01 13:52:29”,updated_at:“2012-08-01 13:52:29”&gt;
我的routes.rb也嵌套了:
resources :trips do
resources :messages
end