2011-10-07 17 views
0

我想通过collection_select值与link_to_remote方法。当我这样做时,我得到了内部服务器错误。我正在使用Rails 2.3.8。我的代码:如何在Rails中对onchange进行操作?

<%= collection_select("event", "trainer_id", @trainers , :id, :name, {:prompt => 'Select a Trainer'}, {:onchange=> "#{link_to_remote(:url => {:controller => 'events', :action => 'find_new' }, :with=>"'trainer_id='+value")}"}) %> 

新增

这是我的控制器代码:

def find_new 
@trainers= Trainer.all 
if ["0"].include?(params[:trainer_id]) 
    render :partial=>'events/me' 
    else 
    render :partial=> 'events/something' 
    end 
end 

回答

2

onchange会想到的JavaScript,你提供一个HTML元素alink_to_remote

我猜你想把用户重定向到你选择的页面?

<%= collection_select("event", "trainer_id", @trainers , :id, :name, {:prompt => 'Select a Trainer'}, {:onchange=> "redirectToTrainer(this.value)"}) %> 

<%= javascript_tag do %> 
    function redirectToTrainer(trainerId){ 
    window.location = "<%= url_for(:controller => 'events', :action => 'find_new') %>?" + trainerId; 
    } 
<% end %> 

有更干净的方法来做到这一点,一个不显眼的Javascript,但我认为这就是你要找的。

+0

我做了这个...但仍然Iam得到错误。我在上面添加我的控制器代码。为什么我得到这个错误? – Rosh

+0

您可以发布错误的堆栈跟踪吗?您的collection_select中可能有语法错误吗?另外,你想要在find_new中完全实现什么?如果你只是想弄清楚它是否是一个,我建议不要让整个培训师集合? –

+0

我没有得到任何堆栈跟踪...我从萤火虫得到这个错误。我在pop_up框中做了这段代码。它使用jQuery。我想让所有培训师... – Rosh

相关问题