I want to get focus to the text field on mouseover. How to do this? Now my code lookd like this
<div class='label_input'>
<%= f.label :username %>
<%= f.text_field :username %>
<div class='clearfix'></div>
</div>
Expecting similar to the HTML code
<input type="text" onmouseover="this.focus()"></input>
You could pass an onmouseover to text_field (docs).
Sometimes I just skip the erb and write the html
You can try:
<div class='label_input'>
<%= f.label :username %>
<input id="user_name" name="user[name]" size="30" type="text" onmouseover="this.focus()"></input>
<div class='clearfix'></div>
</div>
I find that in some cases the rails view helpers get more cumbersome than html. So, use what's most easy.
This way is really helpfull when you update select boxes with javascript.
<div class='label_input'>
<%= f.label :username %>
<%= f.text_field :username, onmouseover: "this.focus()" %>
<div class='clearfix'></div>
</div>
Related
Question? How do I get my form.submit button to use a specific route? Specifically, the user can fill out this form from any page, and it will submit to a desired controller.
ruby -v 2.3.0
rails 5.0
This form is a feedback form for users to submit feedback. The way it works is, a little icon is available to click so the user can fill out and submit this form from ANY page. The Problem is, unless the user is on the homepage (local/customers), for example, they're on post/13, the form tries to add its URL route on top of the example and I get a "no route matches" ...post/13/customers/questionaire.
This is my route.rb
post 'customers/questionaire' => 'customers#questionaire'
This is the form view
<%= form_for :anything, url: "customers/questionaire" ,multiple:
true do |form| %>
<div><%= form.label :email, 'E-mail:' %>
<%= form.text_field :email , placeholder: 'JohnDoe#yahoo.com' %>
</div>
<div><%= form.label :feedback, 'Type of feedback:' %>
<%= form.text_field :feedback, placeholder: 'Problem, Bug, Idea...' %>
</div>
<div><%= form.label :notes, 'Notes: (Required)' %>
<%= form.text_field :notes, class: 'notes', id: 'notes', placeholder: "Your Feedback" %>
</div>
<%= form.submit "Submit", class: "btn1", id: "button", disabled: true %>
<% end %>
I think you need something like this. Do http://localhost:3000/routes. You will get all the routes in your app
<%= form_with scope: :post, url: customers_questionaire_path do |form| %>
<%= form.text_field :title %>
<% end %>
May be this for lower rails version :)
<%= form_for :customer, scope: :post, url: customers_questionaire_path do |form| %>
<%= form.text_field :title %>
<%= form.submit %>
<% end %>
What matters is the action attribute of the form HTML element, or the formaction attribute of a button or input element.¹
In Rails it's defined like so:²
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
Which yields:
<form accept-charset="UTF-8" action="/search" method="get">
<input name="utf8" type="hidden" value="✓" />
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
<input name="commit" type="submit" value="Search" />
</form>
This will send the form data to the /search route.
With this approach, each form of your page will use a different route as target for form processing. You could alternatively use the same route and treat multiple use cases inside a single route, but that's not what you're asking.
Alternatively (or in addition to) you can also use the formaction attribute in buttons and inputs, in which case you override the form element's action attribute:³
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<%= submit_tag("Search On Rails", formaction: search_on_rails) %>
<% end %>
Which yields:
<form accept-charset="UTF-8" action="/search" method="get">
<input name="utf8" type="hidden" value="✓" />
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
<input name="commit" type="submit" value="Search" />
<input name="commit" type="submit" value="Search On Rails" formaction="/search_on_rails" />
</form>
It's as if the first submit button had a formaction="/search" because it's ommited, therefore the action="/search" in the <form> is used.
Other approaches in this question's answers and this one.
For your use case, you'll have to make sure the route /customers/questionaire it's consistent in any URI level (absolute, not dynamic). I lack this particular knowledge in Rails to provide a failsafe solution for this case, although it seems it works as expected in the current default behavior.
So the mistake in your code is using url: with a relative URI when you really want an action: in this line:
<%= form_for :anything, url: "customers/questionaire" ,multiple: true do |form| %>
Use instead:
<%= form_for :anything, url: {action: "customers/questionaire"}, multiple: true do |form| %>
See this question and this one.
In addition to this, instead of hardcoding routes / urls in the code, there's the url_for helper: https://api.rubyonrails.org/v5.1.7/classes/ActionDispatch/Routing/UrlFor.html
I've just started learning ruby on rails recently.
I've login form like this,
<div class="row">
<div class="col-md-12">
<div class="col-md-4">
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email], :class => 'form-control' %>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-4">
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag :password, :class =>'form-control' %>
</div>
</div>
</div>
</div>
I'm just trying to add bootstrap class form-control to email and password field but some thing went wrong, class value appeared at input field of password.
And I've got view like this,
The question might be stupid, any help suggestion is appreciated.
According to the documentation, the second parameter of password_field_tag method is value, so you should set it in order to set third parameter, where you can set the class:
<%= password_field_tag :password, nil, class: 'form-control' %>
Good morning. I'm a newbie in Ruby and I'm stuck in a little problem.
In a form, I want to make a selection of categories. Categories can be inserted by the users, so the selection have to be dynamic.
I've tried like this:
<div class="input-field">
<select class="multiple">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
</select>
</div>
It works, but on chrome console, look like this:
<div class="input-field">
<select class="multiple"></select>
<option value="2">Pastasciutta</option>
<option value="3">Vegetariano</option>
</div>
The category ID was correctly used as value and the list is correctly shown. But, as you see, the "option" is out of the "select" tag. Can you explain me why? And how can I solve this issue?
That list is only shown as a list, but I want to flag and unflag x categories.
My version of Ruby -> 2.3.1
----EDIT----
_form.html.erb
<%= form_for(recipe) do |f| %>
<% if recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
<ul>
<% recipe.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :time %>
<%= f.number_field :time %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :recipe %>
<%= f.text_area :recipe %>
</div>
<div class="input-field">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]}, {}, class: "multiple" %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And that's what is rendered:
http://prnt.sc/cfqmp6
The line:
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
Is already rendering the <select> tag part of the element, you are duplicating this element by placing the above line inside another <select>. Change this:
<select class="multiple">
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]} %>
</select>
To this:
<%= f.select :category, Category.all.collect {|x| [x.name, x.id]}, {}, class: "multiple" %>
The last two arguments are the options and html_options to the select tag. See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
For some reason when executing this code, the radio buttons that get created aren't mutually exclusive in getting picked. How do I make it so that users can only pick 1 radio button? This code is essentially going through a variety of addresses and allowing a user to pick an address for their order.
<div class="select-address select-address-row" style="display:none">
<% #order.user.addresses.each do |address, index| %>
<%= form_for #order, remote: true, :html => { :id => 'address-form-'+address.id.to_s} do |a| %>
<div class="col-xs-4 select-address-col">
<div class="enterprise-buy-address-box address-<%= address.id %>">
<%= address.shipping_name %><br>
<%= address.line_1 %><br>
<%= address.city %>, <%= address.state %> <%= address.zipcode %><br>
<% isDefault = address.shipping_name == #defaultAddress.shipping_name ? true : false %>
<%= a.radio_button :pickedAddress, 'Address', :checked => isDefault %>
<%= label :pickedAddress, '' %>
<div class="submit-address">
<%= button_tag(type: 'submit', class: "btn btn-xs btn-default address-custom-button-select address-"+address.id.to_s+"-button") do %>
select
<% end %>
</div>
</div>
</div>
<%= a.hidden_field :address_id, :value => address.id %>
<% end %>
<script>
$('.address-<%=address.id%>-button').click(function() {
$('.default-address').empty();
var selectedAddress = $('.address-<%=address.id%>').clone();
selectedAddress.find('.submit-address').remove();
$('.default-address').append(selectedAddress);
$('default-address').addClass("enterprise-buy-address-box");
$('.default-address').show();
$('.select-address').toggle();
$('.change-address-link').toggle();
});
</script>
<% end %>
</div>
Your form_for tag is inside of the loop for the addresses. Even if the name attribute is the same for all of your input type="radio" elements, they will not be mutually exclusive across different form elements. Move your form_for tag outside of the loop so that the radio inputs belong to the same form.
See the following snippet for demonstration:
<p>These won't work</p>
<form>
<input type="radio" name="foo">
</form>
<form>
<input type="radio" name="foo">
</form>
<p>These do work</p>
<form>
<input type="radio" name="bar">
<input type="radio" name="bar">
</form>
I'm using Devise for user registration. It automatically gives me the form for a new session:
<%= form_for(resource_name, resource, :url => session_path(resource_name)) do |f| %>
.
.
.
<% end %>
I have the email and password label and fields like so:
<div class = "field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class = "field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
But I'd like to add a class the the text_field and password_field so that I can style the boxes to make them look better.
<%= f.text_field :email, :class => "whatever" %>
did not work, so I looked in the Rails API and found a text_field method and did this:
<%= text_field(:user, :email, :class => "whatever" ) %>
which leaves out the "f" called in "form_for...do |f|". Even so, I'm pretty sure it worked because when I "View Source", the output ("id" and "name") matches:
<div class = "field">
<label for="user_email">Email</label><br />
<input id="user_email" name="user[email]" size="30" type="text" value="" />
</div>
<div class = "field">
<label for="user_email">Email</label><br />
<input class="whatever" id="user_email" name="user[email]" size="30" type="text" value="" />
</div>
This all seems great that it works and the source looks like it's fine, but I just want to get some confirmation from some more experienced developers to make sure that this definitely works and that there is nothing behind the scenes that is wrong before I make the change and have problems later.
Something else is wrong in your code.
<%= f.text_field :email, :class => "whatever" %>
Does what you want: you don't need to specify the user model, because the form builder (the f) knows which model it's dealing with.
For more info, read the introduction paragraph here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-text_field