I have some rails-bootstrap-form code in a header partial that gets rendered in the Rails 4 application layout:
<div class="navbar-form navbar-right">
<%= bootstrap_form_tag controller: 'devices', action: 'index', method: 'get' do |f| %>
<%= f.search_field :search, hide_label: true, placeholder: 'Search' %>
<%= f.submit "Submit", :name => nil %>
<% end %>
</div>
I want this search form to always perform a GET request on the devices controller's index action. The search doesn't actually link to the devices index url, it appends the search parameter to the current url like this: /devices/1?search=foo. Since it just appends to the current URL, the search works fine from /devices.
I thought that if I pass in controller: 'devices', action: 'index', method: 'get' to bootstrap_form_tag it would force the URL to rewrite, but I'm not having any luck.
I ended up stripping out bootstrap_form_tag and I went with a standard form_tag. I had to add the bootstrap form classes in manually.
<%= form_tag(devices_path, method: 'get', class: 'navbar-form navbar-right') do %>
<%= search_field_tag 'search', nil, class: 'form-control', placeholder: 'Search' %>
<%= submit_tag "Submit", class: 'btn btn-default', :name => nil %>
<% end %>
I thought the bootstrap-forms gem would save time, but it definitely didn't for the search.
Related
I have a form_tag form associated with a gem pg_search that works with a form_tag as below without ajax for the moment in pure html, yet when I try to make it work the same way with a form_for the submit does not pass.
Do you know why?
In passing do you know the equivalent in form_tag of the helper "url: search_path" which is commented in the form_for?
Thank you
1. Le form_tag
<%= form_tag new_product_association_path, method: :get do %>
<%= text_field_tag :query, params[:query], id: "text_field", class: "form-control", placeholder: "Find a product" %>
<%= submit_tag "Search", class: "btn btn-primary", id: "submit-search"%>
<% end %>
2. le form_for
<%= form_for product_associations_url, as: :get do |f| %>
<%= f.text_field params[:query], id: "text_field" %>
<%= f.submit "Search", class: "btn btn-primary", id: "submit-search" %>
<% end %>
Rails provides several ways to create HTML for a form.
form_tag: This generates the HTML in most raw form. You specify various attributes and resulting form is generated with no Rails magic.
form_for: This is Rails way of creating forms associated with a resource, e.g.: a product. You have to pass in the object which you want to create / update. So, the way you are writing form_for is incorrect. One more issue (typo) is instead of using as to specify method, you should use method.
Basic Usage of form_for:
In Controller:
#product = Product.new
In Views:
<%= form_for #product do |f| %>
...
<% end %>
The issue might be that the form_for helper expects a record as its first argument, not an URL. I suggest switching to form_with which also allows for URL usage.
Furthermore the text_field usage also isn't valid. You should still provide the name (:query) of the input field.
<%= form_with url: product_associations_url, as: :get do |f| %>
<%= f.text_field :query, value: params[:query], id: "text_field" %>
...
<% end %>
Example usage can also be found all over the Action View Form Helpers guide.
I have a form_tag that I'm using for a search. I need to pass not only the :term (what is being searched for), but also the :ques_num parameter. Currently, :ques_num is not being passed in.
<%= form_tag display_questions_path(:ques_num => 2), method: :get do %>
<%= text_field_tag :term, params[:term], placeholder: "Search Terms..." %>
<% end %>
This directs me to the address with the :term passed in, but not :ques_num. How would I go about doing this?
You should use hidden_field_tag instead:
<%= form_tag display_questions_path, method: :get do %>
<%= text_field_tag :term, params[:term], placeholder: "Search Terms..." %>
<%= hidden_field_tag :ques_num, 2 %>
<% end %>
I have passed a value into a search page via -
<%= link_to 'add', users_path(bookto: #book.id) %>
in the view and
#book = Book.find_by_id(params[:bookto])
in the receiving controller action.
I have a search form in my receiving (index) view
<%= form_tag users_path(params[bookto: #book.id]), method: 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
<%= "#{#book.id}, #{#book.title}" %>
<% if #users %>
<%= render partial: 'layouts/showusers', locals: {users: #users} %>
<% end %>
When I navigate through to the page
http://localhost:3000/users?bookto=1
The value of #book is passed properly. However, when I perform a search, the parameter is not being passed to
http://localhost:3000/users?utf8=✓&search=mysearch
I'm not passing the parameter through. I don't want to use this arbitrary parameter in the search, I just want it available to me to use once the search is complete. How do I achieve this? Do I need to add a search action to my controller?
Why don't just add a hidden field inside your search form like this
<%= form_tag users_path, method: 'get' do %>
<p>
<%= hidden_field_tag :bookto, #book.id %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
Because you would want to see bookid in your URL anyway, so this method is ok in your case.
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
At the time i have defined two search forms in my app:
How you can see the second form doesnt look so nice and disturbs my layout.
So my aim is to style the working form (second form) like the first form.
But i have my problems to define the html in the rails form:
First form:
<form class="navbar-search pull-left">
<input type="text" class="search-query" placeholder="Suche">
</form>
Second Form:
<%= form_tag patients_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
A good first start would be:
<%= form_tag patients_path, :method => 'get', {class: ["navbar-search", "pull-left"]} do %>
<%= text_field_tag :search, params[:search], class: "search-query" %>
<%= submit_tag "Search", :name => nil %>
<% end %>