Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to add to the controller after using f.collection_select

f.collection_select controller

  • Please log in to reply
1 reply to this topic

#1 Szabolcs

Szabolcs

    Passenger

  • Members
  • 1 posts

Posted 27 March 2014 - 01:37 PM

Hi, 

I have a Categ model, which I want to associate with my Listing model. In the _form.html.erb I have added this:

<%= f.label :category %><br>
      
<%= f.collection_select(:categ_id, @categs, :id, :name) %>

In the listings_controller.rb

before_filter :prepare_categs  

def create
    @listing = Listing.new(listing_params)
    @listing.categ_id = :categ_id
....



private 
def prepare_categs
  @categs = Categ.all
end

This @listing.categ_id = :categ_id doesn't work. I can chose the category but not saving. Any idea what could be wrong? How can I refer the selection of a drop-down menu list in the controller?

 

Thank you, Szabolcs



#2 Lauree

Lauree

    Passenger

  • Members
  • 6 posts
  • LocationNew York

Posted 09 May 2014 - 04:39 AM

Hello Szabolcs,

 

The collection_select helper will create a select list with the name as listing[categ_id]. When the form is submitted you will be having parameter as params[:listing][:categ_id].

 

So when you are initializing the @listing object as Listing.new(listing_params) you are getting and object with categ_id provided from params hence you don't need to reset it in the next line.

 

Please check the parameters received in server log.

 

If you are not getting the categ_id in listing_params[:categ_id] then it Might be the case that you have not added categ_id in permitted parameters list.

 

Thanks,

Lauree


Hire skilled Ruby on Rails Developer

Check out Ruby on Rails Outsourcing Services by Allerin






Also tagged with one or more of these keywords: f.collection_select, controller

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users