Note that there are some explanatory texts on larger screens.

plurals
  1. PONested attribute doesn't show up attr_accessible
    text
    copied!<p>I have a nested attribute message.rb</p> <pre><code> class Message &lt; ActiveRecord::Base belongs_to :trip attr_accessible :name, :email, :subject, :body end </code></pre> <p>Which belongs trip.rb</p> <pre><code> class Trip &lt; ActiveRecord::Base has_many :messages accepts_nested_attributes_for :messages, :allow_destroy =&gt; true attr_accessible :messages_attributes end </code></pre> <p>messages_controller.rb</p> <pre><code> class MessagesController &lt; ApplicationController def create @trip = Trip.find(params[:trip_id]) @message = @trip.messages.create(params[:message]) redirect_to trip_path(@trip) end end </code></pre> <p>Message form is in the \trips\show.html.erb</p> <pre><code> &lt;%= form_for([@trip, @trip.messages.build]) do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.label :name%&gt;&lt;br /&gt; &lt;%= f.text_field :name %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :email %&gt;&lt;br /&gt; &lt;%= f.text_field :email %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :subject %&gt;&lt;br /&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :body %&gt;&lt;br /&gt; &lt;%= f.text_area :body %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>And it should appear there as well \trips\show.html.erb</p> <pre><code> &lt;h2&gt;Messages&lt;/h2&gt; &lt;% @trip.messages.each do |message| %&gt; &lt;p&gt; &lt;b&gt;NAme:&lt;/b&gt; &lt;%= message.name %&gt; &lt;/p&gt; &lt;b&gt;Email:&lt;/b&gt; &lt;%= message.email %&gt; &lt;/p&gt; &lt;b&gt;Subject:&lt;/b&gt; &lt;%= message.subject %&gt; &lt;/p&gt; &lt;b&gt;Message:&lt;/b&gt; &lt;%= message.body %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>The problem is that it just doesn't.</p> <p>My Development.log.</p> <blockquote> <p>Started GET "/trips/44? utf8=%E2%9C%93&amp;authenticity_token=%2F0Qs42AHHm9dpO17xfJsu5LSaYCKIschXu3boFjRg54%3D&amp;message> %5Bname%5D=emils&amp;message%5Bemail%5D=emils.veveris%40inbox.lv&amp;message%5Bsubject%5D=aasdasda> ds&amp;message%5Bbody%5D=aaaaaaaaaaaaaaaaaaaaaaaaaaa&amp;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"=>"emils.veveris@inbox.lv", "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) </p> </blockquote> <p>I don't really understand why, I kinda did set attr_accessible. Can You help ? Thank You! </p> <h2><strong>Update</strong></h2> <p>via rails console it works:</p> <blockquote> <p>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]]</p> <p>32, created_at: "2012-08-01 13:52:29", updated_at: "2012-08-01 13:52:29"> </p> </blockquote> <p>And my routes.rb nested as well:</p> <pre><code>resources :trips do resources :messages end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload