1

I am working on simple application to create your music collection. To add album to collection I am using Collection.rb model, it is working very well, but I decided to add a parameter :to_buy to Collection, and add second button "Add to wishlist".

collection_controller:

def create
@album = Album.find(params[:collection][:album_id])
current_user.collect!(@album)
@album.update_attributes(params[:collection][:to_buy])
respond_to do |format|
  format.html { redirect_to @album }
  format.js
end
end

Code for button to add album:

<%= form_for(current_user.collections.build(album_id: @album.id), remote: true) do |f| %>
 <div><%= f.hidden_field :album_id %></div>
 <div><%= f.hidden_field :to_buy, :value => true %></div>
 <%= f.submit "Add to my wishlist", class: "btn btn-large btn-primary" %>
<% end %>

Album is adding to collection, but I want to send value true to :to_buy in collection, and it is still null. Could anyone explain why?

4
  • correct me if i am wrong but to my eyes your controller line @album.update_attributes(params[:collection][:to_buy]) should be @album.update_attributes(params[:collection]) as it requres complete hash Commented Jan 23, 2014 at 17:50
  • maybe it should be like this, but this didnt change anything, still not changing value in db Commented Jan 23, 2014 at 17:54
  • just to verify try this @album.update_attribute(:tobuy, params[:collection][:to_buy]) Commented Jan 23, 2014 at 17:57
  • what are you trying to achieve with this, current_user.collect!(@album). Are you devise or anything for authentication. how is curent_user object available in controller Commented Jan 23, 2014 at 21:58

2 Answers 2

1
@album.update_attributes(params[:collection][:to_buy]) 

should be changed to

@album.update_attributes(:to_buy, params[:collection][:to_buy])

as you need to provide the attribute to be updated. I would also suggest to use where in place of find when querying over the model object.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought the syntax was update_attributes(to_buy: params[:collection][:to_buy]), rather than update_attribute(:attr, "param") stackoverflow.com/questions/2778522/…
1

Although GhostRider has valid information, let me explain what we'd do for this:


Custom Method

If you want to update one attribute, I'd make a custom method in your controller & route to it:

#config/routes.rb
resources :controller do 
    post :buy
end

#app/controllers/your_controller.rb
def buy
    @album = Album.find(params[:collection][:album_id])
    @album.update_attributes(to_buy: params[:collection][:to_buy])
    respond_to do |format|
         format.js
    end
end 

#app/views/
<%= form_for(current_user.collections.build(album_id: @album.id), url: buy_path(@album), remote: true) do |f| %>
    <div><%= f.hidden_field :album_id %></div>
    <div><%= f.hidden_field :to_buy, :value => true %></div>
    <%= f.submit "Add to my wishlist", class: "btn btn-large btn-primary" %>
<% end %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.