Solved

rails select box

Posted on 2009-05-07
10
1,789 Views
Last Modified: 2013-11-13
Hi there,

i'm having a hard setting up a select list in a rails form.
I want the select list to update a field called 'artist_cat' in a table called "artists" with the id value of a table called "categories", and i want the list of choices to be the value categories.name.

The box itself appears but the field 'artist_cat' is not updated...

i don't understand what's wrong with my code...

Any ideas ?

Cheers,

Terence
<p><label for="artists">Artiste ou espoir</label><br/></p>
<%= select("artist", "artist_cat", Categorie.find_all.collect {|c| [ c.cat_name, c.id ] })  %>

Open in new window

0
Comment
Question by:terencepires
  • 5
  • 5
10 Comments
 
LVL 14

Expert Comment

by:wesgarrison
ID: 24332976
What does your update action look like?

Paste your update action for this form and the params when submitting the form and we'll check them out to see what's going on.
0
 
LVL 1

Author Comment

by:terencepires
ID: 24336799
Ok, here it is
  def update
    
    @artist = Artist.find(params[:id])
    if @artist.update_attributes(params[:artist])
      flash[:notice] = "L'artiste a bien été enregistré."
      #redirect_to :action => 'show', :id => @artist
      @artists = Artist.find(:all, :order => "artist_cat, name")
      render :action => "list"
    else
      flash[:notice] = 'Les modifications ont bien été enregistrées.'
      redirect_to :action => 'list'
    end
  end

Open in new window

0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 24336860
Can you send the params too?

Check your log/development.log.
Go to the bottom.
Look for the update action being called.
It'll look something like the below.

Just the parameters line (well, all of them) is what I'd like to see.
Processing ArtistController#update (for 127.0.0.1 at 2009-05-07 00:04:19) [PUT]
  Parameters: {"commit"=>"Update", "authenticity_token"=>"k3QXSv3j8t7VcDgH92lalDMfxmb+pKrT9o1/HUXBvxY=", "artist"=>{"name"=>"Aaron", ...}, "id"=>"833241487"}

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:terencepires
ID: 24336906
Ok they are in the txt file attached
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 24336922
Whoops.  No file!
0
 
LVL 1

Author Comment

by:terencepires
ID: 24336941
oh :)

sorry :)
parameters.txt
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 24337083
Okay, you ARE update the artist_cat value:
UPDATE artists SET `bio` = '', `name_fr` = 'Alexandra Maria Lara', `name` = 'Alexandra Maria Lara', `bio_zh` = '', `artist_cat` = 0, ...

What makes you think that you aren't updating the value?

You should probably redirect_to :list after a successful update, too. Why re-render when you can redirect?
0
 
LVL 1

Author Comment

by:terencepires
ID: 24337285
well, i see the field, but for some reason, the value passed to the update action is always the value that in current in the database and not the selected one.

here is the generated drop down list code, which moreover is correct...

so i assume there is something being reinitialized somewhere in the controller, am i correct ?
<select id="artist_artist_cat" name="artist[artist_cat]"><option value="0">acteurs</option>
<option value="1">musiciens</option>
<option value="2" selected="selected">plasticiens</option></select>

Open in new window

0
 
LVL 14

Accepted Solution

by:
wesgarrison earned 500 total points
ID: 24337350
Do you have another artist_cat field there?  Maybe a hidden field or something?

Everything looks fine to me. I don't see a re-initialization in the controller, just the update_attributes() call.

Your option values look correct.  In fact, it looks the db is set to value 2 in the code snippet you just posted because it was set to be selected by default.
0
 
LVL 1

Author Closing Comment

by:terencepires
ID: 31578961
Ok solved, you were right !

I had a checkbox linked to artist_cat, it's all sorted out now !

Thank you very much, you helped a lot !
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Assigning profile resource to users 4 316
Rake errors while installing gitorious 10 719
SSH Rail Server Command 7 996
ruby gem installation 2 52
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now