Inserting multiple rows

Form collects multiple parameters from a select list (multiple enabled) what kind of code do i need in the controller ?

Stuart

Not making much progress here , so let me give more detail:

Form has this element -

select name=canindustry[category_id] size=“6” multiple=“multiple”> <%= options_from_collection_for_select @categories, :id, :name %> Params being submitted (example) Parameters : {“commit”=>“Create”, “canindustry”=>{“category_id”=>[“2”, “3”, “4”, “5”]},

The fields in the canindustry model are: id | candidate_id | category_id I want to insert multiple rows , so using above parameters

id | candidate_id | category_id | 1 11 2

2 11 3 3 11 4 4 11 5

Problem is in my controller where i"ve tried a few different ways with no success:

Right now I have this in the create action:

@canindustry.candidate_id = current_user.id

a = (params[:canindustry])
a.each {|x| a.canindustry.save

}

and getting back an error -

“You have a nil object when you didn’t expect it! The error occurred while evaluating nil.candidate_id=”

??? Stuart

Not much around on this googling. Anyone ever do this before ?

Dark Ambient wrote:

> > Not making much progress here , so let me give more detail: > > Form has this element - > select name=canindustry[category_id] size="6" multiple="multiple"> > <%= options_from_collection_for_select @categories, :id, :name %> > > Params being submitted (example) > *Parameters*: {"commit"=>"Create", "canindustry"=>{"category_id"=>["2", > "3", "4", "5"]}, > > The fields in the canindustry model are: id | candidate_id | > category_id > I want to insert multiple rows , so using above parameters > > id | candidate_id | category_id | > 1 11 2 > 2 11 3 > 3 11 4 > 4 11 5 > > Problem is in my controller where i"ve tried a few different ways with no > success: > Right now I have this in the create action: > > @canindustry.candidate_id = current_user.id >

I assume you set @canindustry somewhere to a proper object, but the error stated otherwise.

> a = (params[:canindustry]) > a.each {|x| a.canindustry.save > } >

Here I think params[:canindustry][:category_id] will give you the array of selected category ids. Each 'a' element is a String, which dnu #canindustry let alone #save.

Seems to me you need something like the following in your a.each block.

    canindustry = Foo.new     canindustry.candidate_id = current_user.id     canindustry.category_id = x     canindustry.save

Long www.edgesoft.ca

Dark Ambient wrote:

Not making much progress here , so let me give more detail:

Form has this element - select name=canindustry[category_id] size=“6” multiple=“multiple”>

<%= options_from_collection_for_select @categories, :id, :name %>

Params being submitted (example) Parameters: {“commit”=>“Create”, “canindustry”=>{“category_id”=>[“2”,

“3”, “4”, “5”]},

The fields in the canindustry model are: id | candidate_id | category_id I want to insert multiple rows , so using above parameters

id | candidate_id | category_id | 1 11 2 2 11 3 3 11 4

4 11 5

Problem is in my controller where i"ve tried a few different ways with no success: Right now I have this in the create action:

@canindustry.candidate_id = current_user.id

I assume you set @canindustry somewhere to a proper object, but the error stated otherwise.

Yeah , it was set up as a proper object though I was getting all sorts of errors.

a = (params[:canindustry])
a.each {|x| a.canindustry.save

}

Here I think params[:canindustry][:category_id] will give you the array of selected category ids. Each ‘a’ element is a String, which dnu #canindustry let alone #save.

Seems to me you need something like the following in your a.each block.

canindustry = Foo.new
canindustry.candidate_id = current_user.id
canindustry.category_id = x
canindustry.save

Yep , it works ! Thank you!

a = (params[:canindustry][:category_id])

 a.each {|x|
  canindustry = Canindustry.new
  canindustry.candidate_id = current_user.id
  canindustry.category_id = x
  canindustry.save }

Let this thread serve as a warning jumping to fast into Rails without having Ruby array methods burned deeply in your brain.

Stuart