Rails turn form to bootstrap form - ruby-on-rails

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

Related

Rails 4 Bootstrap Search Form in Navbar

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.

How to create a bootstrap paragraph 'help-block' for a input field in Rails 4

I´m using Bootstrap with Rail 4 to create a new form for a Model and I want to create a form_group like this:
<label for="model_name">Name</label>
<input class="form-control" id="model_name" name="model[name]" placeholder="Your name" type="text">
<p class="help-inline" id="model_name_inline">Help inline</p>
In my view I do this:
<%= form_for #model, url: {action: "create"} do |f| %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Your name" %>
<%= content_tag :p, "Help inline", class: "help-inline", id: :name %>
<% end %>
But I can´t do the "id" of the content_tag be like the label and input (in this case, 'model_name') adding "inline" to the id
How I can do this? Is there any kind of content_field for custom fields of a form? :-)
Thank you in advance.
UPDATE: Thanks to the comment I´ve updated the question with the correct id
I would extract that into a helper. For example
module ApplicationHelper
def inline_help(help_text, object_name, method)
content_tag :p, help_text, class: "help-inline", id: "#{object_name}_#{method}_inline"
end
end
You can then use it within your form like this
<%= form_for #model, url: {action: "create"} do |f| %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Your name" %>
<%= inline_help 'Help inline', f.object_name, :name %>
<% end %>
Hope this helps.

correct syntax for form_for url in rails

This is probably simple, I'm still coming to terms with rails syntax. What is the right syntax to pass the address_id in the url for form_for to a modified route?
This is the form - note the "address_id parameter"
<div class="one_fourth floatcenter">
<%= form_for address, :url => edit_address_path(:id => address.id), :method => :get do |f| %>
<%= content_tag(:button, :class => 'btn btn-inverse') do %> Edit Address
<% end %>
<% end %>
</div>
And this is the route I've configured:
get "edit_address/:id" => "member/addresses#edit"
Id is not being passed to the controller for some reason...
form_for address should be enough if address is a persisted object, but if it's not enough, then form_for address, url: edit_address_path(address) is what you want.
This is very simple. In place of url, you put your post method route:
<%= form_for(#post, url: super_posts_path) do |f| %>
...
<% end %>
You also call by action
<%= form_for #friend,:url=> { action: "create_friend"} do |f|%><br>
<%= f.label :u_from %>
<%= f.text_field :u_from %>
<%= f.label :u_to %>
<%= f.text_field :u_to %>
<%= f.submit%>
<% end %>

Unable to pass the params[:fields] hash to controller method in Ruby on Rails

routes.rb
-----------
resources :mail_settings
My form looks like this
the _form.html.erb
---------
<%= form_tag '/mail_settings' do %>
<div class="fieldBlock">
<%= label_tag :name %> <%= text_field_tag :name%> </div>
<div class="fieldBlock">
<%= label_tag :id%> <%= text_field_tag :id%> </div>
<div class="actions fieldBlock">
<%= submit_tag "Update Settings ", :class => "btn-large btn-success" %>
</div>
<% end %>
but I can access individual params like params[:name] without any problem, why is it not working when i try params[:mail_setting] ?
you mean why it is params[:name] and not params[:mail_setting][:name] ? If so, the reason is that you are using form_tag instead of just a form, and family of *_tag helpers [ i.e. text_field_tag ]. In that case you do not 'bind' form to the model - in general form_tag is much more flexible than form. However, You should be able to do something like
<%= text_field_tag "mail_setting[name]"%>
and you will get params[:mail_setting][:name]
Hope I guessed what you asked about!

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

Resources