rails FormBuilder select setting selected value with an "odd" association
I've got this slightly left of center situation that I can't get working. The problem in simplest terms is that I build this select using a has_one/belongs开发者_运维百科_to association and it populates fine, returns valid values on a submit, BUT if the submit fails for some reason (a different input validation fails for example) when the form is repainted with the error message, the select defaults to the first value in the list rather than the one selected...Here's some context.
I have the following two models. The first one is a table that I inherited from a PHP app and have to "play nice" with so it's not following rails convention:
class Listing < ActiveRecord::Base
set_primary_key :lid
has_one :site
end
class Site < ActiveRecord::Base
belongs_to :listing
end
You can see I had to jump through a tiny hoop to cover the fact that this legacy table uses "lid" instead of "id" as its primary key.
In my view I have this form select using ActionView::Helpers::FormBuilder::select:
.field
.left.form-label
= f.label :listing
.left.form-field
= f.select( :listing_id, options_from_collection_for_select(Listing.all.sort {|a,b| a.address <=> b.address}, :lid, :address), :prompt => "Please select an address", { :selected => @site.listing_id })
When I do a submit and it returns an error for some other unrelated reason, the select is set to the first option in the list, not the one that the user actually selected.
A few other bits of context. Here are the incoming params on the save that is destined to fail first:
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"X/yqRO0VA0/3GrkGT0sc2KVPd4sVLF2Nd/vnjQM7GOI=", "site"=>
{"domain"=>"butterballasshat.info", "listing_id"=>"937", "user_id"=>"21",
"description"=>"asdf"}, "commit"=>"Save"}
And here is the relevant controller code:
def create
@site = Site.new(params[:site])
respond_to do |format|
if success and @site.save
format.html { redirect_to(sites_url, :notice => 'Site was successfully created.') }
format.xml { render :xml => sites_url, :status => :created, :location => @site }
else
format.html { render :action => "new" }
format.xml { render :xml => @site.errors, :status => :unprocessable_entity }
end
end
end
If you need any other information to wrap your head around my problem I will gladly provide it...
as requested :
Not sure what the problem is without seeing the error, but to solve your select issue, add the selected stuff to the options_from_collection_for_select method, in the following structure: options_from_collection_for_select(collection, value, label, selected_id)
This is an easier way to do this now:
<%= f.collection_select(:city_id, City.all, :id, :name) %>
精彩评论