select_tag and rendering form for selected value
I have working form_for @company. But in controller: @company = Company.first. I have many companies and I want to choose company by select_tag and when I select company in view page this form_for wor开发者_如何学Pythonk with this company. Ho can I make it?
Just add a select_tag before your form like this:
= select_tag :chosen_company_id, options_from_collection_for_select(Company.all, 'id', 'name')
Then with javascript (jquery variant) do:
$(document).ready(function(event){
$('#chosen_company_id').change(function(event){
url = "http://your.domain.com/companies/" + $(this).val();
document.location.href = url;
});
});
And don't forget to change your controller code to:
@company = (params[:id] ? Company.find(params[:id]) : Company.first)
精彩评论