0

我是 ruby​​ on rails 的新手。是否可以在单选按钮内调用控制器操作。

<%= form_tag :action => 'show' do %>
  <strong>Select device: </strong> 
  <br></br>
  <strong>Chose: </strong><%=  radio_button_tag :name,:time, @time, :onclick => "this.parentNode.submit();"%>Time
  <%=  radio_button_tag :name,:graph%>Graph

选择第一个单选按钮时,我必须去显示控制器内的方法。它工作正常。选择第二个单选按钮我还必须转到控制器内的另一种方法。怎么可能?

4

3 回答 3

1

I would do the following :

The form will lead to the same action for both radio buttons.

Inside the action, depending on the selected radio, it will call the right action :

def show
  if params[:name] == :time
    self.action_for_time
  elsif params[:name] == :graph
    self.action_for_graph
  end
end

protected

def action_for_time
  # ...
end

def action_for_graph
  # ...
end
于 2013-02-28T08:51:21.470 回答
0

尝试:

<%= radio_button_tag :name,:graph, :onclick => "window.location.href=<YOUR_PATH>" %>Graph

你可以写(例如你想去主页)

<%= radio_button_tag :name,:graph, :onclick => "window.location.href=/home/" %>Graph
  # or
<%= radio_button_tag :name,:graph, :onclick => "window.location.href=<%=home_path%>/" %>Graph
于 2013-02-28T08:46:23.877 回答
0

我认为这样做更干净的方法是,

<div id="my-select" >
--
<<view code>>
--
</div>

应用程序.js

$(function() {

   $("#my-select").on("click",function() {
    <<do-what-you-want>>;
   });

});

我可能对你的路由和控制器有点随意,但如果你喜欢这个解决方案,你会弄明白的。

于 2013-03-01T06:52:08.907 回答