Adding an ID or Class in Ruby on Rails? - ruby-on-rails

I've got the following code for a search form, but how would I add an ID or a class to the submit button?
<% form_tag '/wine/search/', :method => 'get' do %>
<%= label_tag "Search" %>
<%= text_field_tag :search_string, params[:search_string] %>
<%= submit_tag "Go" %>
<% end %>
Thanks

submit_tag "Go", :class => "some_class"
submit_tag "Go", :id=> "some_id"
From the Rails API FormTagHelper

Related

Rails routes on query string

I'm new to Ruby on rails. Now I have a search form in my homepage. When I enter something (like abc) in the form and submit, I would like the page to call my action info in my controller search. How should I configure the routes?
The current url is ~/info/?utf8=%E2%9C%93&location=berkeley&commit=submit
<div id='search_form'>
<%= form_tag('info/',method: "get") do %>
<%= text_field_tag('location', #location, :size => 30) %>
<%= submit_tag "submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
Add the following to your routes file:
get '/info', to: "search#info", as: 'search_info'
This gives you search_info_path and you can use it in your form_for declaration as follows:
<%= form_tag(search_info_path, method: "get") do %>
<%= form_tag some_path, method: "get" do %>
<%= text_field_tag('location', #location, :size => 30) %>
<%= submit_tag "submit", class: "btn btn-large btn-primary" %>
<% end %>
You can use path helper in you form_tag that leads to your controller.
i think this is wrong ('info/',method: "get")
run rake routes for look helper`s

Send parameter to controller on submit_tag rails

I have two submit_tag on my form and I would like to send a different parameter on each.
How can I do this?
My form view:
<%= form_tag(some_path, :method => "get") do %>
<%= text_field_tag :number %>
<%= text_field_tag :name %>
<%= submit_tag "Op01", class: "btn_search", my_parameter: 1 %>
<%= submit_tag "Op02", class: "btn_search", my_parameter: 2 %>
<% end %>
And on my controller:
#oper_type = params[:my_parameter]
But when I display the #oper_type it is always nil.
Thanks.
<%= submit_tag "Op01", class: "btn_search", value: 1 %>
<%= submit_tag "Op01", class: "btn_search", value: 2 %>
#oper_type = params[:commit] # 1 or 2
or a little simplier
<%= submit_tag "Op01", class: "btn_search" %>
<%= submit_tag "Op01", class: "btn_search" %>
#oper_type = params[:commit] # "Op01" or "Op02"
Is there a way to accomplish this without revealing the value to the user on the submit button? Say you want to have a different text on the button and a hidden value sent to the controller...

Take input in the view and pass it to the contoller

The following code in not rendering even the buttons:
<% form_tag :controller=> :create_new, :action=>:input do %>
<%=text_field_tag :my_input%>
<%=submit_tag "Send input"%>
<%end%>
the controller create_new has the following method:
def input
#my_input=params[:my_input]
end
the routes.rb has::
resources :create_new do
post :input, :on=>:collection
Use <%= form_tag instead of <% form_tag.
For rails 3 we have to use '=' symbol for form_tag and form_for. so change your code as follows..
<%=form_tag :controller=> :create_new, :action=>:input do %>
<%=text_field_tag :my_input%>
<%=submit_tag "Send input"%>
<%end%>
<%= form_tag input_create_new_path, :html_options => {:method => :post} do |f| %>
<%= f.text_field :my_input%>
<%= f.submit "Send input"%>
<%end%>

ruby on rails ignores my form_tag

I have written in my index.html.erb the following lines
<% form_tag illnesses_path, :method =>'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
expecting that it will write a form element but rails completrly neglected those line.
when I wrote
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
the items apeared (but no form element).
Why was the form element neglected
Change
<% form_tag illnesses_path, :method =>'get' do %>
to
<%= form_tag illnesses_path, :method =>'get' do %>.

Rails 3 - keep params after submit

I want to keep my params afterr i do submit.
In my Rails 3.2.0 application i have something like that:
http://0.0.0.0:3000/journals?class_code=11v&subject_name=Math
And i have form:
<%= form_for #lesson, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
....
<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>
<% end %>
I want to keep my params (class_code and subject_name) after f.submit. How can i do that?
To store all parameters in one field, you could use:
<%= hidden_field_tag :parameters, request.query_string %>
And then you can access them in controller, using:
parameters = parse_nested_query(params[:parameters])
hidden fields
....
<%= hidden_field_tag :class_code, params[:class_code] %>
<%= hidden_field_tag :subject_name, params[:subject_code] %>
<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>
but - if those are attributes of your model, then assign them in the new action of the controller
def new
#lesson = Lesson.new(:class_code => params[:class_code], :subject_code => params[:subject_code])
end
# in this case the view code is slightly different
<%= f.hidden_field :class_code %>
<%= f.hidden_field :subject_code %>

Resources