Brittany
Brittany

Reputation: 1439

Change Simple_Form Submit Button Action?

I've created the following SimpleForm for my Rails application. I want the submit button to take the data selected in each of the fields, and carry it over to the next screen. Does anyone know how I can change the property of the SimpleForm submit button? Right now it just seems to create something? I'm a little confused by what it's default action is.

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

Upvotes: 1

Views: 4699

Answers (2)

Sravan
Sravan

Reputation: 18647

simple form for action can be changed to the action(use 'url' option), we want for example,

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

In your controller, you will get the params that were send through the form,

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

Now create a your_action.html.erb and you will get values there.

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

Thats it.

Upvotes: 2

Apoorv
Apoorv

Reputation: 1379

Replace Your form with the following

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

And in routes Add the following lines

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

Upvotes: 1

Related Questions