Rails 4. Attribute not saving when creating model - ruby-on-rails

I am stumped as to why this isn't working. I have a review system, and a link to create a new review passing 2 attributes; user_id and gigid. Both id's are being generated as integers but when I save the review, the gigid saves as 'nil' rather than the integer.
View:
<%= link_to 'Click here to rate this user', new_user_review_path(:user_id => request.user.id, :gigid => request.gig.id) %>
Here both ID's are set correctly (according to server output). User_id is the user which is to be reviewed (works correctly) and gigid is a reference for post validation. (works correctly until I try and save)
_form :
<%= simple_form_for([#user, #user.reviews.build]) do |f| %>
<div id="rating-form">
<label>Rating</label>
</div>
<%= f.input :comment %>
<%= f.button :submit %>
<% end %>
Controller:
def new
if user_signed_in?
#review = current_user.reviews.new
else
redirect_to(root_url)
flash[:danger] = "You must log in to rate a user"
end
end
def create
#review = #user.reviews.new review_params
#review.reviewed_id = current_user.id
if #review.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def review_params
params.require(:review).permit(:rating, :comment, :gigid, :user_id)
end
end
Server output when clicking link:
Started GET "/users/21/reviews/new?gigid=17&locale=en" for 127.0.0.1 at 2016-01-13 18:08:26 +0100
Processing by ReviewsController#new as HTML
Parameters: {"gigid"=>"17", "locale"=>"en", "user_id"=>"21"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 21]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered reviews/_form.html.erb (4.9ms)
I then add a comment and a rating and click create.
Server output when clicking create:
Started POST "/users/21/reviews?locale=en" for 127.0.0.1 at 2016-01-13 18:10:44 +0100
Processing by ReviewsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"RToZwvodvXxQp1/spEhgemO/oi+4rof8jREuLw27CcEsEBqFWGLeYne1CgH3kvWu9OzAV+bHvOk9g9nq8JMPnw==", "review"=>{"rating"=>"5", "comment"=>"sdfsdfdd"}, "commit"=>"Create Review", "locale"=>"en", "user_id"=>"21"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 21]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "reviews" ("rating", "comment", "user_id", "reviewed_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["rating", 5], ["comment", "sdfsdfdd"], ["user_id", 21], ["reviewed_id", 1], ["created_at", "2016-01-13 17:10:44.675386"], ["updated_at", "2016-01-13 17:10:44.675386"]]
(93.6ms) commit transaction
I notice that 'gigid' is being completely ignored here, but EVERYTHING else saves fine.
I don't understand why one attribute saves and the other doesn't, any insight would be greatly appreciated!

Your form doesn't have anything for gigid to send it to create action. You can use hidden field in the form like below
<%= simple_form_for([#user, #user.reviews.build]) do |f| %>
<div id="rating-form">
<label>Rating</label>
</div>
<%= f.input :comment %>
<%= f.input :gigid, :as => :hidden, :input_html => { :value => params[:gigid] }
<%= f.button :submit %>
<% end %>

There is no gigid in review params. Look carefully on your params when you are performing POST: "review"=>{"rating"=>"5", "comment"=>"sdfsdfdd"}. Just review and comment. And after it you are doing:
params.require(:review).permit(:rating, :comment, :gigid, :user_id)

Related

Why the database didn't update anything although terminal shows "Completed 200" for Rails?

My controller code is like these:
def edit
#user = User.find params[:id]
end
def update
#user = User.find params[:id]
if #user.update_attributes(params[:user])
flash[:notice] = "success!"
else
flash[:notice] = "fail!"
end
end
attr_accessor :username, :introduction, :first_name, :last_name
private
def user_params
params.require(:user).permit(:username, :introduction, :first_name, :last_name)
end
View:
<%= form_for #user, :url =>edit_path(#user), :method => :PUT do |f| %>
<%= f.label :username %><br>
<%= f.text_field :username %>
<br>
<%= f.label :Introduction %><br>
<%= f.text_area :introduction %>
<br>
<%= f.label :FirstName %><br>
<%= f.text_field :first_name %>
<br>
<%= f.label :LastName %><br>
<%= f.text_field :last_name %>
<br>
<%= f.submit "Save changes"%><% end %>
And the terminal is:
Started PUT "/profile/1/edit" for 127.0.0.1 at 2017-12-06 04:30:05 -0500
Processing by ProfileController#edit as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eBvvAG892DTOFzYuFTKPCgbOuSQXB8BVOub7A8CgaPHLW2vaBFMpUGNHhgYZednfd5pqICCy1DykIarD7QYtIg==", "user"=>{"username"=>"aaa", "introduction"=>"aaaaa", "first_name"=>"a", "last_name"=>"a"}, "commit"=>"Save changes", "id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Notification Load (0.1ms) SELECT "notifications".* FROM "notifications"
Chat Exists (0.0ms) SELECT 1 AS one FROM "chats" WHERE "chats"."sender_id" = ? LIMIT 1 [["sender_id", 1]]
Chat Exists (0.0ms) SELECT 1 AS one FROM "chats" WHERE "chats"."recip_id" = ? LIMIT 1 [["recip_id", 1]]
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]]
Rendered profile/edit.html.erb within layouts/game (1.5ms)
User Load (0.2ms) SELECT "users".* FROM "users"
Completed 200 OK in 32ms (Views: 28.5ms | ActiveRecord: 0.5ms)
But database is not updating:
1|du.1#osu.edu|$2a$11$2WHUjqQHU1gtlzw3ObXgVeE7dGG1qby2tLmATeoUyMnh1cg5wXv86||||3|2017-12-06 08:43:44.469588|2017-12-06 05:32:19.252680|127.0.0.1|127.0.0.1|2017-12-05 17:04:39.689636|2017-12-06 08:43:44.470487||1970-1-1|||This guy is lazy. Without an Introduction.|
Try to use safe user_params instead of params[:user]:
if #user.update_attributes(user_params)
# ...
BTW you probably don't need attr_accessor :username, :introduction, :first_name, :last_name line in your controller.
Something is definitely askew. Look here:
Started PUT "/profile/1/edit" for 127.0.0.1 at 2017-12-06 04:30:05 -0500
You're doing a PUT to /profile/1/edit. Conventionally, you would do a GET to /profile/1/edit which would map to the edit action of your ProfileController. So, perhaps you have your routes mis-configured because I would normally expect that to throw an error. Instead (if your console output is correct), PUT "/profile/1/edit" is mapping to the edit action of your ProfileController, as you can see here:
Processing by ProfileController#edit as HTML
And, naturally, edit doesn't do any updating:
def edit
#user = User.find params[:id]
end
So, no changes to your database.
By the way, are you using Devise or something like that? As you can see, you're loading your user twice:
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
...
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]]
So I wonder if, in edit,
def edit
#user = User.find params[:id]
end
#user = User.find params[:id] is even needed.

Rails file_field tag passing only String file name to controller

My file_field tag is only passing the file name, not the file itself to the controller.
In my User model:
mount_uploader :avatar, AvatarUploader
validate :avatar_size
...
def avatar_size
errors.add :avatar, "should be less than 5MB" if avatar.size > 5.megabytes
end
The AvatarUploader:
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process resize_to_limit: [50, 50]
if Rails.env.production?
storage :fog
else
storage :file
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg, jpeg, gif, png)
end
end
And the User Controller:
before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: [:destroy]
skip_before_action :verify_authenticity_token, only: [:create]
...
def update
#user = User.find(params[:id])
if #user.update_attributes user_params
flash[:success] = "Profile updated"
redirect_to user_path #user
else
render 'edit'
end
end
...
def user_params
params.require(:user).permit :name, :email, :password, :password_confirmation, :avatar
end
def correct_user
#user = User.find params[:id]
redirect_to root_path unless #user == current_user
end
def admin_user
redirect_to root_path unless current_user.admin?
end
The form in the view:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<% if #user.avatar? %>
<%= image_tag #user.avatar.url, size: "50x50" %>
<% else %>
<%= image_tag "stock_ava.png", size: "50x50" %>
<% end %>
<%= f.file_field :avatar, accept: 'image/jpeg,image/gif,image/png' %>
<%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>
In the console I can see the attribute come in as "FinnJake.png", rather than the appropriate ActionDispatch.
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"h9tD/g2M1zJ5L4cnysIQwHCWxDdhhhJR1I9a9+FAWeT9aTXPte16Uyn8GvI4w23klfvYtTaLCDrGVonHfmFUag==", "user"=>{"name"=>"Example User", "email"=>"example#user.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "avatar"=>"FinnJake.png"}, "commit"=>"Save Changes", "id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.1ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND ("users"."id" != ?) LIMIT ? [["email", "bexample#user.com"], ["id", 1], ["LIMIT", 1]]
SQL (1.1ms) UPDATE "users" SET "avatar" = ?, "updated_at" = ? WHERE "users"."id" = ? [["avatar", nil], ["updated_at", "2017-10-02 14:50:41.226691"], ["id", 1]]
(142.3ms) commit transaction
Redirected to https://rubyonrails-xxx.c9users.io/users/1
Completed 302 Found in 155ms (ActiveRecord: 144.1ms)
I'm very new to rails, but I cannot figure out why the file_field isn't uploading a file. I can see in the browser that I end up with this tag:
<input accept="image/jpeg,image/gif,image/png" type="file" name="user[avatar]" id="user_avatar">
Which looks right to me. I also cannot seem to manually update that column through the rails console, though I get no errors:
2.4.0 :002 > user = User.first
User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<User id: 1, name: "Example User", email: "example#user.com", created_at: "2017-10-02 14:32:22", updated_at: "2017-10-02 16:35:55", password_digest: "$2a$10$sww7gItks0mYz6xz8zV.QOIjr6to/o5Jytjmuo0wxny...", remember_digest: nil, admin: true, activation_digest: "$2a$10$W396r7A8aZaii3DC7b3W7u70etSmXh3MORMIoWtcSHo...", activated_at: "2017-10-02 14:32:22", reset_digest: nil, reset_sent_at: nil, avatar: nil>
2.4.0 :003 > user.update avatar: "String"
(0.2ms) begin transaction
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND ("users"."id" != ?) LIMIT ? [["email", "example#user.com"], ["id", 1], ["LIMIT", 1]]
SQL (2.4ms) UPDATE "users" SET "updated_at" = ?, "avatar" = ? WHERE "users"."id" = ? [["updated_at", "2017-10-02 17:27:06.097363"], ["avatar", nil], ["id", 1]]
(11.4ms) commit transaction
=> true
Thanks in advance for any advice you can provide.
You should use multipart form like:
form_for(#user, html: { multipart: true }) do ...
See http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-file_field
Found the problem. See this comment
Essentially, the form in the partial was being wrapped by another form which had a different encoding type.

Strong params not saved in nested form

In my app I want to create events that have nameand price
In these events I want to be able to add participants they have a first_nameand a salary
I am using the gem Cocoon for the nested forms
From the EventsController I am trying to create my participants...
I don't manage to get the right way to do this...
EventsController
class EventsController < ApplicationController
def new
#event = Event.new
#participant = Participant.new
end
def create
#event = Event.create(params_event)
if #event.save
#event.participants.create([{first_name: params[:first_name], salary: params[:salary], event_id: params[:id]}])
#binding.pry
redirect_to #event
end
end
def show
#event = Event.find(params[:id])
end
private
def params_event
params
.require(:event).permit(:name,:total_price,
participants_attributes: [:first_name, :salary, :_destroy ] )
end
end
I want that my create methode render an array of participants in the given event like this...
#event.participants.create!([john={first_name: "John", salary: 2000, event_id: params[:id]}, mike={first_name: "Mike", salary: 1400, event_id: params[:id]}])
Here is the result of binding.pry
8: def create
9: #event = Event.create(params_event)
10: if #event.save
11: #event.participants.create([{first_name: params[:first_name], salary: params[:salary], event_id: params[:id]}])
12: ##event.participants.create!([john={first_name: "John", salary: 2000, event_id: params[:id]}, mike={first_name: "Mike", salary: 1400, event_id: params[:id]}])
13: binding.pry
=> 14: redirect_to #event
15: end
16: end
[1] pry(#<EventsController>)>
[1] pry(#<EventsController>)> #event
=> #<Event:0x007f89f8013c98
id: 67,
name: "Rent a plane",
total_price: 3400,
created_at: Mon, 13 Mar 2017 01:18:24 UTC +00:00,
updated_at: Mon, 13 Mar 2017 01:18:24 UTC +00:00>
[2] pry(#<EventsController>)> #event.participants
Participant Load (0.2ms) SELECT "participants".* FROM "participants" WHERE "participants"."event_id" = $1 [["event_id", 67]]
=> [#<Participant:0x007f89f6773580
id: 30,
first_name: nil,
salary: nil,
created_at: Mon, 13 Mar 2017 01:18:24 UTC +00:00,
updated_at: Mon, 13 Mar 2017 01:18:24 UTC +00:00,
event_id: 67>]
If neeeded here are my models:
class Event < ApplicationRecord
has_many :participants, dependent: :destroy
accepts_nested_attributes_for :participants, reject_if: :all_blank, allow_destroy: true
end
class Participant < ApplicationRecord
belongs_to :event
end
And my forms:
new.html.erb
<%= simple_form_for #event do |f| %>
<%= f.input :name, label: "Event's name" %>
<%= f.input :total_price, label: "What is the total price" %>
<h2> Add your friends to share the bill</h2>
<%= f.simple_fields_for :participants do |participant| %>
<%= render "events/participants_fields", f: participant %>
<% end %>
<%= link_to_add_association 'add a friend', f, :participants, partial: "events/participants_fields", class:"btn btn-primary" %>
<%= f.button :submit %>
<% end %>
partial: _participants_fields.html.erb
<div class='nested-fields participant-background'>
<%= f.input :first_name, label: "Enter your friend's first name" %>
<%= f.input :salary, label: "Enter his/her monthly pay" %>
<%= link_to_remove_association "Remove this friend", f , class: "btn btn-danger btn-xs" %>
</div>
EDIT
These are my logs when I create an event with participants they aren't saved: ( I just kept the #event = Event.new(params_event)
tarted GET "/" for ::1 at 2017-03-13 13:55:20 +0100
ActiveRecord::SchemaMigration Load (7.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by EventsController#new as HTML
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering events/new.html.erb within layouts/application
Rendered events/_participants_fields.html.erb (5.8ms)
Rendered events/new.html.erb within layouts/application (98.6ms)
Rendered shared/_navbar.html.erb (2.5ms)
Rendered shared/_flashes.html.erb (0.4ms)
Completed 200 OK in 408ms (Views: 358.0ms | ActiveRecord: 20.9ms)
Started POST "/events" for ::1 at 2017-03-13 13:55:48 +0100
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mMuWIZe06ofG6DCafY2EPrJouIcF4YzKKJx6Y9ct5XUZNHqIyvFY4l3dqiEnxC5NhQapvSnFDukgblJnHg+14g==", "event"=>{"name"=>"Rent a van", "total_price"=>"390"}, "commit"=>"Create Event"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (11.9ms) INSERT INTO "events" ("name", "total_price", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "Rent a van"], ["total_price", 390], ["created_at", 2017-03-13 12:55:48 UTC], ["updated_at", 2017-03-13 12:55:48 UTC]]
(6.0ms) COMMIT
(0.2ms) BEGIN
(0.2ms) COMMIT
Redirected to http://localhost:3000/events/93
Completed 302 Found in 25ms (ActiveRecord: 18.6ms)
Started GET "/events/93" for ::1 at 2017-03-13 13:55:48 +0100
Processing by EventsController#show as HTML
Parameters: {"id"=>"93"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Event Load (0.4ms) SELECT "events".* FROM "events" WHERE "events"."id" = $1 LIMIT $2 [["id", 93], ["LIMIT", 1]]
Rendering events/show.html.erb within layouts/application
Participant Load (0.5ms) SELECT "participants".* FROM "participants" WHERE "participants"."event_id" = $1 [["event_id", 93]]
Rendered events/show.html.erb within layouts/application (3.1ms)
Rendered shared/_navbar.html.erb (3.3ms)
Rendered shared/_flashes.html.erb (0.8ms)
Completed 200 OK in 41ms (Views: 34.1ms | ActiveRecord: 1.5ms)
The problem was in my forms....
Cocoon needs special div
<div class="container">
<div class="col-xs-12">
<%= simple_form_for #event do |f| %>
<%= f.input :name, label: "Event's name" %>
<%= f.input :total_price, label: "What is the total price" %>
<h2> Add your friends to share the bill</h2>
<div id="participants">
<%= f.simple_fields_for :participants do |participant| %>
<%= render "events/participants_fields", f: participant %>
<% end %>
</div>
<div class="links">
<%= link_to_add_association 'add a friend', f, :participants, partial: "events/participants_fields", class:"btn btn-primary" %>
</div>
<%= f.button :submit %>
<% end %>
</div>
</div>
I cleaned the controller like so:
class EventsController < ApplicationController
def new
#event = Event.new
end
def create
#event = Event.new(params_event)
if #event.save
redirect_to event_path(#event)
end
end
def show
#event = Event.find(params[:id])
end
private
def params_event
params
.require(:event).permit(:name,:total_price, participants_attributes: [:first_name, :salary, :_destroy ] )
end
end

Why comments don't work

I create user->posts->comments associations, but comments don't work right.When I commented post, comment save to data base, column user_id was recorded (ok), but column post_id is empty.
There is partial _post_list.html.erb
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<%= render 'shared/comment' %>
<%= post.user.name %>
</tr></br></br>
<% end %>
There is partial _comment.html.erb
<%= form_for(#comment) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>
controller comments_controller
class CommentsController < ApplicationController
def new
#comment = Comment.new
end
def create
#comment = current_user.comments.build(comment_params)
if #comment.save
redirect_to root_url
else
render 'static_pages/home'
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
UPD there is server log
Started POST "/posts/6/comments" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZvyHKxSdH0a1rf79yaWmo9N/pD9/YhSQ9Ek8bdMLhOI=", "comment"=>{"body"=>"stackoverflow"}, "commit"=>"commented", "post_id"=>"6"}
User Load (4.9ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
(0.2ms) begin transaction
SQL (0.6ms) INSERT INTO "comments" ("body", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["body", "stackoverflow"], ["created_at", "2014-11-16 20:28:26.544500"], ["updated_at", "2014-11-16 20:28:26.544500"], ["user_id", 5]]
(131.7ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 152ms (ActiveRecord: 137.4ms)
Started GET "/" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by StaticPagesController#home as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
Rendered shared/_post_form.html.erb (2.4ms)
Post Load (0.4ms) SELECT "posts".* FROM "posts" ORDER BY created_at DESC
Rendered shared/_comment.html.erb (1.8ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 5]]
Rendered shared/_comment.html.erb (2.8ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (4.0ms)
CACHE (3.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (1.5ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (2.9ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_post_list.html.erb (45.3ms)
Rendered static_pages/home.html.erb within layouts/application (52.1ms)
Rendered layouts/_header.html.erb (1.2ms)
Completed 200 OK in 364ms (Views: 347.0ms | ActiveRecord: 4.5ms)
controller static_pages_controller
class StaticPagesController < ApplicationController
def index
end
def show
end
def home
#post = current_user.posts.build
if signed_in?
end
#posts = Post.all
#comment = current_user.comments.build
end
end
view home.html.erb
<%= render 'shared/post_form' %>
<%= render 'shared/post_list' %>
How fix?
sorry for my bad English
You are far from showing enough code to allow us to understand what can happen. But I feel strange that your form does not include somewhere a reference to post.id or whatever you name the identifier for post objects.
The controller will only be able to use :
input fields of the form (here text of comment)
session stored values (user id)
If the current post id is neither in form fields, not in session, the controller will not get id, it you cannot store it in database.
It looks like you are looping through all of your posts and creating a comment form on the same page for each. You may want to set your comment routes up as nested resources by wrapping it within the post resource.
# config/routes.rb
resources :posts do
  resources :comments
end
Pass your #post object into the partial like so:
<%= render 'shared/comment', post: post %>
Then you should be able to create your comment form like so:
<%= form_for([post,#comment]) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>
By including that post, you are telling the comment which post it belongs to. Don't forget to update your strong parameters in the controller if you are using them.
EDIT: Based on your update, you'll need to add :post_id to the permit at the bottom of our controller.

Rails STI: ActiveRecord PUT transaction works, but fields don't update?

I am creating an application using STI for the first time and I've stumbled onto a puzzling roadblock.
Given the following two models with inheritance:
User.rb
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, #more follows
Waiter.rb
class Waiter < User
I made a form on /waiters/users/[:id]/edit that does the following:
<%= simple_form_for #user,
:url => { :controller => "admin/waiters/users", :action => "update"} do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :start_date %>
<%= f.button :submit, "Save", class: "btn btn-primary"%>
<% end %>
However, pressing submit is putting a post request through but failing to update the actual data. Is it because there's still a wrong route? See the SQL dump below:
Started PUT "/admin/waiters/users/6" for 127.0.0.1 at 2013-02-10 20:45:59 -0800
Processing by Admin::Waiters::UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6EB82/aWryLgj/tZcvoOWmw98PAPmJYUViAQronv6Fw=", "waiter"=>{"first_name"=>"John", "last_name"=>"Smith", "email"=>"john#foobaz.com"}, "commit"=>"Save", "id"=>"6"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Role Load (0.5ms) SELECT "roles".* FROM "roles" INNER JOIN "user_roles" ON "roles"."id" = "user_roles"."role_id" WHERE "user_roles"."user_id" = 1
Waiter Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 6 LIMIT 1
(0.1ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'john#foobaz.com' AND "users"."id" != 6) LIMIT 1
CACHE (0.0ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'john#foobaz.com' AND "users"."id" != 6) LIMIT 1
(2.4ms) UPDATE "users" SET "perishable_token" = 'FplOAv6d4eOBv0gHW3b', "updated_at" = '2013-02-11 04:45:59.206797' WHERE "users"."id" = 6
(0.7ms) COMMIT
Redirected to http://localhost:3000/admin/waiters/users
The controller seems to be redirecting just fine after the update action, but the data isn't saved. What am I missing here? Appreciate the help for a Rails starter.
This is the Users Controller found in controllers/admin/waiters/
def edit
form_info
#user = Waiter.find(params[:id])
end
def update
#user = Waiter.find_by_id(params[:id])
if #user.update_attributes(params[:user])
flash[:notice] = "Successfully assigned Waiter."
redirect_to admin_waiters_users_url()
else
form_info
render :action => 'edit'
end
end
The problem is that you are pulling the wrong values from the params hash. Look at the logs:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6EB82/aWryLgj/tZcvoOWmw98PAPmJYUViAQronv6Fw=", "waiter"=>{"first_name"=>"John", "last_name"=>"Smith", "email"=>"john#foobaz.com"}, "commit"=>"Save", "id"=>"6"}
The updated attributes are hashed through the waiter key. Now look at your update code:
if #user.update_attributes(params[:user])
You are getting params[:user]. Since you are using STI, I assume you want to keep params[:user], so you need to change your form code for the waiter. You should do:
<%= simple_form_for #user, as: :user,
:url => { :controller => "admin/waiters/users", :action => "update"} do |f| %>

Resources