I''ve tried pulling this together from examples in the book, searching the web and this list, but so far I''ve not been able to get it working just right. My table is a categories table with id and category fields. My form has the following: <td><%= select(''category'', ''category'', @categories = Category.find(:all, :order => "category").map {|cat| [cat.category, cat.category]}) %> I used [[cat.category, cat.category] as I am not interested in the id and want both values to be equal to the category itself. This form is a Donation form. One of the fields in the donation record is a category. The database does not relate these two tables. What I want to do is simply populate the dropdown category box from the categories table and allow a category to be selected and that category be populated to the donation record. In other words: 1) Pull all categories into the form from the categories table into a dropdown box on adding the record 2) Upon editing the record, again pull the categories as in step one, but this time cause the category selected when the record was added to be selected in the dropdown box, otherwise upon update the wrong category would be updated to the record. The dropdown box is populated with the categories; however, when the record is added to the database, the category field is not populated. The log file shows the following: [4;35;1mCategory Load (0.078000) SELECT * FROM categories ORDER BY category So far so good. And then on adding the record: Parameters: {"commit"=>"Create", "category"=>{"category"=>"Furniture - Kitchen"}, "action"=>"create", [4;35;1mSQL (0.062000) INSERT INTO donations (`best_contact_time`, `city`, `created_on`, `item_desc`, `category`, ... The category of "Furniture - Kitchen" is probably the eighth in the list and is in fact the one chosen. Two things then: 1) Why isn''t this being updated into the database record (the field in the donation record is named category) 2) Will the current code be sufficent to cause the category in the record to be selected upon editing of the record? Since the code in the form populates the dropdown box, might I need to then after that immediately cause the category from the record to be the selected value with following additonal code? I suspect that there is no actual "selection" happening and if there were, the donation''s record field would be populated and that would also solve the issue when updating as well, and yet the create statement in the log file appears to be showing a value! Thanks for any help on this. I''ve found the dropdown boxes to be particularly troubling. -- Posted via http://www.ruby-forum.com/.
Prob worth posting you model definitions but I''ve made some assumptions about your likely models with some suggestions below: Scott Helfrich wrote:> I''ve tried pulling this together from examples in the book, searching > the web and this list, but so far I''ve not been able to get it working > just right. > > My table is a categories table with id and category fields. > > My form has the following: > <td><%= select(''category'', ''category'', @categories = Category.find(:all, > :order => "category").map {|cat| [cat.category, cat.category]}) %> > > I used [[cat.category, cat.category] as I am not interested in the id > and want both values to be equal to the category itself. > >You may not be interested in the id, but rails will be (assuming you have a has_many/belongs_to relationship between Category and Donation) as this is what it will use in the category_id field in the donations table. Assuming this is part of a Donation form you will prob benefit from changing the select to select(''donation'', ''category_id'', @categories ....), depending on your models and how your controller action will assign the category (automatically, through Donation.new(params[:donation]), or through specific assignment @donation = Donation.new; @donation.category = Category.find(params[:category]); #etc
Scott Helfrich
2006-Jul-09 13:48 UTC
[Rails] Re: Trouble getting select to work - RESOLVED
Chris T wrote:> Prob worth posting you model definitions but I''ve made some assumptions > about your likely models with some suggestions below: > > Scott Helfrich wrote: >> I used [[cat.category, cat.category] as I am not interested in the id >> and want both values to be equal to the category itself. >> >> > You may not be interested in the id, but rails will be (assuming you > have a has_many/belongs_to relationship between Category and Donation) > as this is what it will use in the category_id field in the donations > table. Assuming this is part of a Donation form you will prob benefit > from changing the select to select(''donation'', ''category_id'', > @categories ....), depending on your models and how your controller > action will assign the category (automatically, through > Donation.new(params[:donation]), or through specific assignment > @donation = Donation.new; @donation.category > Category.find(params[:category]); #etcThank you for the response Chris. It is much appreciated. I apologize for the confusion resulting from my lack of clarity and insufficient information surrounding the problem. Two tables are involved here. The first is the categories table with the following definition: id category The second table is donations with the following defintion (partial): id item item_desc category (actual category and not category_id) ... There is no relationship of 1:m or of any kind in the database or Rails. Why? Design choice really. Only one way for categories to get into the database: through the categories maintenance forms. The dropdown box on the donations forms, create and update provide the only means of category populating the donations record, and since the dropdown box on the donations form is populated directly from the categories table, well, the data must certainly be valid. The scenario is that upon creating or updating a donations record, the dropdown box is populated with categories from the categories table. A number of things have to happen here. 1) By default the first category retrieved from the categories table will be showing in the dropdown box. If say the third category is selected, that category of course needs to be populated in the donations record. 2) When updating the donations record, again the categories are retrieved from the categories table, but the default category, the first retrieved from the categories table, must not be showing in the dropdown box, but instead the one in the existing donations record must be showing; in this scenario it would be the third category in the list. 3) If the category is not changed, the one showing, again in this scenario the third one, must still be the one to populate the donations record upon update. And of course if the category is changed, the one then showing would update into the donations record. I ran into this very same problem when doing this app using JSPs, Hibernate and Tiles. I''d scoured the books, the internet, reading articles and so forth for just the perfect example of how to do this. I just could not find one that suited this same scenario, which I found odd. Hopefully my explanation of this problem and the database, form and general setup is sufficient so that the resolution makes sense as well. Of course I played around so much that I can''t even remember now what didn''t work exactly! The book, Agile Web Development with Rails gave me the following code example, which provided the basis from which to begin with: <%= User.find(:all, :order => "name").map {|u| [u.name, u.id]} select(:user, :name, @users) %> What I ended up with, and this works exactly as I had hoped for and needed, was: <%= select("donation", ''category'', @categories = Category.find(:all, :order => "category").map {|cat| [cat.category, cat.category]}) %> I didn''t use the id here from the category table records as all I really wanted returned from the form was the category value itself which was then populated into the donations record and table. You however Chris did provide me with the clue I needed when you mentioned using ''donation''. Thank you. That led me to what I now have! -- Posted via http://www.ruby-forum.com/.