开发者

select_tag and rendering form for selected value

开发者 https://www.devze.com 2023-03-08 18:54 出处:网络
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

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消