Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Working with select_tag


  • Please log in to reply
2 replies to this topic

#1 jschrock1

jschrock1

    Dispatcher

  • Members
  • 48 posts
  • LocationIndianapolis

Posted 18 November 2013 - 10:17 PM

Im trying to get my first select combo box working and having trouble..  I;ve read some forums, fiddled with the variety of select procedures but still stuck..

 

I have three models Im working, User, Userskill, and JobCategory, with all the appropriate actions built from rails generate...

 

When displaying the user's profile for edit, I want to add an aditional combo box field that will list all the job categories for the user to choose.  Keeping in mind that the user model does not have a field called job_category, rather the linkage is in the user_skills table.  The user_skills table belongs to user...

 

I've been fiddling witht the following select and getting very close..

 

<%= f.collection_select(:name, JobCategory.all, :id, :name) %>

 

The combo box looks fine, however my problem is with :name.  I just stubbed that in as I knew it was a field in the user model, but really when the selection is made I want the value to come in an abitrary named field that does not exist in the user model.  What I intend is to use that value and create the UserSkill record in the update action of the user controller.

 

I've played with the following too:  @job_categories was created in the edit action...

 

<%= select_tag(:skill_id, options_for_select(@job_categories{|m| [m.name, m.id]})) %>

 

but here im getting a syntax error that I don't understand. 

 syntax error, unexpected '{', expecting ')'
...for_select(@job_categories{|m| [m.name, m.id]})) );@outp...

The later select_tag seems like its what I need as I can use an arbitrary value in return :skill_id that is not in the user model...

 

Any advise on how/which select tag may work with my situation?

 

thanks, in advance,

 



#2 jschrock1

jschrock1

    Dispatcher

  • Members
  • 48 posts
  • LocationIndianapolis

Posted 18 November 2013 - 10:21 PM

Im trying to get my first select combo box working and having trouble..  I;ve read some forums, fiddled with the variety of select procedures but still stuck..

 

I have three models Im working, User, Userskill, and JobCategory, with all the appropriate actions built from rails generate...

 

When displaying the user's profile for edit, I want to add an aditional combo box field that will list all the job categories for the user to choose.  Keeping in mind that the user model does not have a field called job_category, rather the linkage is in the user_skills table.  The user_skills table belongs to user...

 

I've been fiddling witht the following select and getting very close..

 

<%= f.collection_select(:name, JobCategory.all, :id, :name) %>

 

The combo box looks fine, however my problem is with :name.  I just stubbed that in as I knew it was a field in the user model, but really when the selection is made I want the value to come in an abitrary named field that does not exist in the user model.  What I intend is to use that value and create the UserSkill record in the update action of the user controller.

 

I've played with the following too:  @job_categories was created in the edit action...

 

<%= select_tag(:skill_id, options_for_select(@job_categories{|m| [m.name, m.id]})) %>

 

but here im getting a syntax error that I don't understand. 

 syntax error, unexpected '{', expecting ')'
...for_select(@job_categories{|m| [m.name, m.id]})) );@outp...

The later select_tag seems like its what I need as I can use an arbitrary value in return :skill_id that is not in the user model...

 

Any advise on how/which select tag may work with my situation?

 

thanks, in advance,

I've also tried

<%= select_tag(:skill_id, options_for_select(JobCategory.all{|m| [m.name, m.id]})) %>

This does not have a syntax error, but the values in the combo box are #<JobCategory:0x5030c220>  I was expecting the name attribute of each object in the .all call....



#3 jschrock1

jschrock1

    Dispatcher

  • Members
  • 48 posts
  • LocationIndianapolis

Posted 18 November 2013 - 11:19 PM

Im trying to get my first select combo box working and having trouble..  I;ve read some forums, fiddled with the variety of select procedures but still stuck..

 

I have three models Im working, User, Userskill, and JobCategory, with all the appropriate actions built from rails generate...

 

When displaying the user's profile for edit, I want to add an aditional combo box field that will list all the job categories for the user to choose.  Keeping in mind that the user model does not have a field called job_category, rather the linkage is in the user_skills table.  The user_skills table belongs to user...

 

I've been fiddling witht the following select and getting very close..

 

<%= f.collection_select(:name, JobCategory.all, :id, :name) %>

 

The combo box looks fine, however my problem is with :name.  I just stubbed that in as I knew it was a field in the user model, but really when the selection is made I want the value to come in an abitrary named field that does not exist in the user model.  What I intend is to use that value and create the UserSkill record in the update action of the user controller.

 

I've played with the following too:  @job_categories was created in the edit action...

 

<%= select_tag(:skill_id, options_for_select(@job_categories{|m| [m.name, m.id]})) %>

 

but here im getting a syntax error that I don't understand. 

 syntax error, unexpected '{', expecting ')'
...for_select(@job_categories{|m| [m.name, m.id]})) );@outp...

The later select_tag seems like its what I need as I can use an arbitrary value in return :skill_id that is not in the user model...

 

Any advise on how/which select tag may work with my situation?

 

thanks, in advance,

I just got this to work....

 

<%= select("category", "skill_id", @job_categories.map {|u| [u.name,u.id]}) %>






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users