I have a nested attribute message.rb
class Message < ActiveRecord::Base
belongs_to :trip
attr_accessible :name, :email, :subject, :body
end
Which belongs 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
Message form is in the \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 %>
And it should appear there as well \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 %>
The problem is that it just doesn't.
My Development.log.
Started GET "/trips/44? utf8=%E2%9C%93&authenticity_token=%2F0Qs42AHHm9dpO17xfJsu5LSaYCKIschXu3boFjRg54%3D&message> %5Bname%5D=emils&message%5Bemail%5D=emils.veveris%40inbox.lv&message%5Bsubject%5D=aasdasda> ds&message%5Bbody%5D=aaaaaaaaaaaaaaaaaaaaaaaaaaa&commit=Create+Message" for 127.0.0.1 at 2012-07-31 23:11:16 +0300 Processing by TripsController#show as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"/0Qs42AHHm9dpO17xfJsu5LSaYCKIschXu3boFjRg54=", "message"=>>>> > {"name"=>"emils", "email"=>"[email protected]", "subject"=>"aasdasdads", "body"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaa"}, "commit"=>"Create Message", "id"=>"44"} [1m[35mTrip Load (0.0ms)[0m SELECT "trips".* FROM "trips" WHERE "trips"."id" = ? LIMIT 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> "locations"."id" = 18 LIMIT 1 [1m[36mCACHE (0.0ms)[0m [1mSELECT "trips".* FROM "trips" WHERE "trips"."id" = ? LIMIT 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 Load (1.0ms)[0m [1mSELECT "lessons".* FROM "lessons" WHERE "lessons"."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 Rendered 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 "reviews".* FROM "reviews" WHERE "reviews"."trip_id" = 44 Rendered trips/show.html.erb within layouts/application (261.0ms) Rendered layouts/_shim.html.erb (0.0ms) Rendered layouts/_header.html.erb (3.0ms) Rendered layouts/_footer.html.erb (1.0ms) Completed 200 OK in 404ms (Views: 295.0ms | ActiveRecord: 9.0ms)
I don't really understand why, I kinda did set attr_accessible. Can You help ? Thank You!
Update
via rails console it works:
irb(main):019:0> 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 Aug 2012 13:52:29 UTC +00:00]]
32, created_at: "2012-08-01 13:52:29", updated_at: "2012-08-01 13:52:29">
And my routes.rb nested as well:
resources :trips do
resources :messages
end