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"=>"[email protected]"}, "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" = '[email protected]' AND "users"."id" != 6) LIMIT 1
CACHE (0.0ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = '[email protected]' 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"=>"[email protected]"}, "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| %>