更改Simple_Form提交按钮操作?

时间:2016-02-19 04:50:03

标签: ruby-on-rails ruby ruby-on-rails-4

我为我的Rails应用程序创建了以下SimpleForm。我希望提交按钮获取在每个字段中选择的数据,并将其传送到下一个屏幕。有谁知道如何更改SimpleForm提交按钮的属性?现在它似乎只是创造了一些东西?我对它的默认操作有点困惑。

<div class="infoheaders">1. Question
  <%= simple_form_for @category do |f| %> 
    <%= f.collection_select(:title, Category.all, :title, :title) %>
   <div class="grey-text-subheader">Device: (optional)</div>
    <%= f.check_box :Apple %> <%= f.label "Apple" %> &nbsp; &nbsp; 
    <%= f.check_box :iPhone %> <%= f.label "iPhone" %> &nbsp; &nbsp;  
    <%= f.check_box :iPad %> <%= f.label "iPad" %> &nbsp; &nbsp; 
    <%= f.check_box :Mac %> <%= f.label "Mac" %> &nbsp; &nbsp;  
    <%= f.check_box :Android %> <%= f.label "Android" %><br><br>
    <%= f.check_box :Samsung %> <%= f.label "Samsung" %> &nbsp; &nbsp;  
    <%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>&nbsp; &nbsp; 
    <%= f.check_box :Windows %> <%= f.label "Windows" %> &nbsp; &nbsp; 
    <%= f.check_box :Google %> <%= f.label "Google" %> 
    <%= f.button :submit, 'Submit' %>
  <% end %>
</div>

2 个答案:

答案 0 :(得分:2)

简单的动作形式可以改为动作(使用'url'选项),我们想要例如,

<div class="infoheaders">1. Question

<%= simple_form_for @category, :url => url_for(:action => 'your_action', :controller => 'your_controller'), :method => 'post' do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>

</div>
<div class="grey-text-subheader">Device: (optional)</div>

<%= f.check_box :Apple %> <%= f.label "Apple" %> &nbsp; &nbsp; 
<%= f.check_box :iPhone %> <%= f.label "iPhone" %> &nbsp; &nbsp;
<%= f.check_box :iPad %> <%= f.label "iPad" %> &nbsp; &nbsp; 
<%= f.check_box :Mac %> <%= f.label "Mac" %> &nbsp; &nbsp;  
<%= f.check_box :Android %> <%= f.label "Android" %>  
<br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %> &nbsp; &nbsp;  
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>&nbsp; &nbsp; 
<%= f.check_box :Windows %> <%= f.label "Windows" %> &nbsp; &nbsp;
 <%= f.check_box :Google %> <%= f.label "Google" %> 
<%= f.button :submit, 'Submit' %>
<% end %>

</div>

在您的控制器中,您将获得通过表单发送的参数

class Yourcontroller
  def your_action
  // render text: params and return false 
  if params[:category][:ipad].present?
    @checked = params[:category][:ipad]
  end  
  if params[:category][:ipad].present?
    @checked = params[:category][:android]
  end
  if params[:category][:ipad].present?
    @checked = params[:category][:samsung]
  end
  /* check the params and assign to the variables */
  // render text: params[:category] and return false
  end
end

现在创建一个your_action.html.erb,您将获得值。

<div>
 The checked valus is  <%= @checked %>
</div>

多数民众赞成。

答案 1 :(得分:1)

使用以下

替换您的表单
    <div class="infoheaders">1. Question
    <%=form_for @category, :url=>"/register", do |f| %> 
    <%= f.collection_select(:title, Category.all, :title, :title) %>
    <div class="grey-text-subheader">Device: (optional)</div>
    <%= f.check_box :Apple %> <%= f.label "Apple" %> &nbsp; &nbsp; 
    <%= f.check_box :iPhone %> <%= f.label "iPhone" %> &nbsp; &nbsp;  
    <%= f.check_box :iPad %> <%= f.label "iPad" %> &nbsp; &nbsp; 
    <%= f.check_box :Mac %> <%= f.label "Mac" %> &nbsp; &nbsp;  
    <%= f.check_box :Android %> <%= f.label "Android" %><br><br>
    <%= f.check_box :Samsung %> <%= f.label "Samsung" %> &nbsp; &nbsp;  
    <%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>&nbsp; &nbsp; 
    <%= f.check_box :Windows %> <%= f.label "Windows" %> &nbsp; &nbsp; 
    <%= f.check_box :Google %> <%= f.label "Google" %> 
    <%= f.button :submit, 'Submit' %>
    <% end %>
    </div>

在路线中添加以下行

get "/register"=> "controller#action"