I'm currently using Rails 4.2 with these models:
class User < ActiveRecord::Base
has_many :user_accessories, dependent: :destroy
accepts_nested_attributes_for :user_accessories,
reject_if: :all_blank,
allow_destroy: true
has_many :accessories, through: :user_accessories
end
class UserAccessory < ActiveRecord::Base
belongs_to :user, required: true
belongs_to :accessory, required: true
end
class Accessory < ActiveRecord::Base
has_many :user_accessories, dependent: :destroy
has_many :users, through: :user_accessories
end
This is a simple many-to-many relationship, where a user can "own" many accessories, and an accessory can be "owned" by many users.
I've set up my routes in the following way:
resources :users, only: [ :index, :show, :edit, :update, :accessories ] do
member do
get 'accessories'
end
end
This way, a user can go to their "accessories" path (/users/1/accessories
) to see a list of their accessories, as well as to add new accessory associations to their account.
Here's the view that renders for the accessories path:
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<%= render @accessories %>
</tbody>
</table>
<%= form_for( @user ) do |f| %>
<%= f.collection_select(
:accessory_ids,
Accessory.all,
:id,
:name, { include_hidden: false }, { multiple: true } )
%>
<%= f.button "Add Equipment" %>
<% end %>
This just renders a table of current accessories, and then builds a collection_select box where you can choose multiple accessories to add. I've simplified this by removing Bootstrap styling.
Finally, here are the relevant controller actions:
def update
@user = User.find( params[:id] )
if( @user.update_attributes( update_params ) )
redirect_to @user
else
render 'edit'
end
end
def accessories
@user = User.find( params[:id] )
@accessories = @user.accessories
end
private
def update_params
params.require( :user ).permit(
:first_name,
:last_name,
:region_id,
:country,
:profile_image,
:remove_profile_image,
accessory_ids: [:id],
)
end
Now the Problems:
Submitting the form when I already have a value manually entered in the database actually just deletes all currently stored values and does not add any new ones:
Started PATCH "/users/nuggles" for 127.0.0.1 at 2015-02-18 13:31:50 -0500 Processing by UsersController#update as HTML Parameters: { "utf8"=>"✓",···· "authenticity_token"=>"my_token",· "user"=>{ "accessory_ids"=>["5"] },· "button"=>"",· "id"=>"nuggles" } User Load (40.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 4]] User Load (36.4ms) SELECT "users".* FROM "users" WHERE "users"."slug" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["slug", "nuggles"]]· (33.5ms) BEGIN Accessory Load (34.1ms) SELECT "accessories".* FROM "accessories" INNER JOIN "user_accessories" ON "accessories"."id" = "user_accessories"."accessory_id" WHERE "user_accessories"."user_id" = $1 [["user_id", 4]] **SQL (34.1ms) DELETE FROM "user_accessories" WHERE "user_accessories"."user_id" = $1 AND "user_accessories"."accessory_id" IN (1, 2, 3) [["user_id", 4]]** User Exists (37.7ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."username") = LOWER('Nuggles') AND "users"."id" != 4) LIMIT 1 (34.2ms) COMMIT Redirected to http://localhost:3000/users/nuggles Completed 302 Found in 304ms (ActiveRecord: 250.0ms)
When submitting the form with nothing selected, I get the following error:
param is missing or the value is empty: user private def update_params params.require( :user ).permit( :first_name, :last_name, :region_id,