ruby on rails将参数从表单传递给ruby脚本

时间:2011-12-22 10:07:04

标签: ruby-on-rails ruby ajax

我正在尝试执行一个Ruby脚本,该脚本使用从Ruby on Rails表单传递给它的参数。

这是来自我的代码片段:

<%= form_for(@test, :html => {:name => "test", :id => "test"}) do |f| %>
    <div class="field", id="configure_device">
        <%= f.label  "Label1" %>
        <%= f.text_field :Form_Param1 %>
        <%= f.label "Label2" %>
        <%= f.text_field :Form_Param2 %>
        <%= f.label "Label3" %>
        <%= f.text_field :Form_Param3 %>

&lt; ...更多text_fields here ...&gt;

    </div>
    <div class="actions">
        <%= button_to 'Apply Configs', :remote=>"true" %>
    </div>
<% end %>

我的Ruby脚本的结构如下:

def apply_configs (param1, param2, param3, ..., etc.)
    <... ruby code that executes based on the values passed in param1, param2, param3 ...>
end

我分别测试了这个Ruby脚本,如果收到正确的输入参数param1,param2,param3,..等等,它就可以正常工作。

问题是如何在单击“Apply Configs”按钮时执行此Ruby脚本,如何将参数Form_Param1,Form_Param2,Form_Param3,...等传递给它,以映射到param1,param2, param3,...等等?

1 个答案:

答案 0 :(得分:0)

您必须从params收到,不需要arguments

def apply_configs
   form_param1 = params[:test][:Form_Param1]
   form_param2 = params[:test][:Form_Param2]

   // similarly

    <... ruby code that executes based on the values passed in param1, param2, param3 ...>
end