I am developing a personal website in ruby on rails.
I have a form being dynamically generated as such:
<div>
<%= #request_type %>
<h1>Login Form</h1>
<% if #error == true %>
<h1>Error: Login Information Invalid</h1>
<% end %>
<%= form_for :user, url: {action: "login"}, method: :post do |f| %>
<p> E-mail: <br /> <%= f.text_field :email %></p>
<p> Password: <br /> <%= f.password_field :password %></p>
<p><%= submit_tag "Log In!", :disable_with => "Logging in..." %></p>
<% end %>
</div>
The #request_type is being set as request.request_method(), for debugging purposes for now.
The HTML generated for the form specifically looks like the following:
<form accept-charset="UTF-8" action="/login" method="post">
<!-- authentication token here -->
<p> E-mail: <br /> <input id="user_email" name="user[email]" type="text" /></p>
<p> Password: <br /> <input id="user_password" name="user[password]" type="password" /></p>
<p><input data-disable-with="Logging in..." name="commit" type="submit" value="Log In!" /></p>
</form>
However, any time I submit it, the headers are set with the GET method, instead of POST as declared in the form element (see method="post").
The R.O.R. might have nothing to do with what's going on... I am using Chrome to test my application. Any thoughts about why this is happening? I absolutely need the request to use POST.
If there's any other information that might be useful to solving this, please ask, and I will provide it.
Edit: Relevant routes:
login POST /login(.:format) user#login
GET /login(.:format) user#login'
I see no reason to post routes that are not associated with the "login" action.
and = submit_tag should be = f.submit_tag
Also replace:
<%= form_for :user, url: {action: "login"}, method: :post do |f| %>
With:
<%= form_for :user, url: login_path, method: :post do |f| %>
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
With rails 5.1.5
I have this in a view:
<%= form_for(:date_filter, method: 'get') do |f| %>
<%= f.date_field(:travel_date , value: #travel_date) %>
<%= f.submit 'Time Travel now' %>
<% end %>
The genrated html is:
<form action="/games" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
<input value="2018-03-16" type="date" name="date_filter[travel_date]" id="date_filter_travel_date">
<input type="submit" name="commit" value="Time Travel now" data-disable-with="Time Travel now">
</form>
Everythins is working correctly but when I press submit I have this url
http://localhost:3000/games?utf8=%E2%9C%93&date_filter%5Btravel_date%5D=2018-03-16&commit=Time+Travel+now
Why utf8 and commit=Time+Travel+now are there?
How to remove it?
Not sure why you need to remove these two params from your form. These are creaed by rails from_for and submit tag.
utf-8 here to support Internet Explorer 5 and encourage it to use UTF-8 for all forms. For more clarification you can look at here.
If to still want to remove utf-8 params, you need to create your own html form without using rails form_for helper.
Removing commit params is more easier. Just change your submit tag from:
<%= f.submit 'Time Travel now' %>
to
<%= f.submit 'Time Travel now', :name => nil %>
I use this code to allow the user to send a mail to another user:
<%= button_to("Contact me","mailto:#{#formation.usr.email}?subject=#{#formation.name}", class: "fiche__detail__contact") %>
Which gives me the following html:
<form action="mailto:user#gmail.com?subject=SomeName" class="button_to" method="post">
<div>
<input class="fiche__detail__contact" type="submit" value="Me contacter" />
<input name="authenticity_token" type="hidden" value="cO0XDBcG4j0IfmDJV56sdYSfoLeV9NmhTd+bJu/ku+U=" />
</div>
</form>
And this king of email:
I tried to set the body of the mail using &body but it doesn't work. Also, set :authenticity_token => false is not working either.
How can I remove the token?
Just use mail_to:
<%= mail_to #formation.usr.email, "Contact me", :subject => #formation.name, class: "fiche__detail__contact" %>
I am reading the guide on form helpers http://guides.rubyonrails.org/form_helpers.html but I'm not sure whether I actually need to write my webpages this way..
I have an HTML form like this written in new.html.erb
<form action="/posts" method="post">
<label for="title">Test</label>
<input id="title"/>
<input type="submit" value="submit"/>
</form>
(The posts page is from the Getting Started guide)
When I submit the request, the server throws an ActionController::InvalidAuthenticityToken exception.
So I changed it to
<%= form_for :post, url: {action: "create"} do |f| %>
<label for="title">Test</label>
<input id="title"/>
<input type="submit" value="submit"/>
<% end %>
And now I have the same form, except the server now accepts the request.
Are form helpers the proper way to develop forms in Rails views?
Generally, yes.
"Rails way" would have you rewrite this as:
<%= form_for Post.new do |f| %>
<%= f.label :title, "Test" %>
<%= f.input :title %>
<%= f.submit %>
<% end %>
Your attempt to use the straight HTML tag was prevented by the CSRF protection that Rails uses on all of its non-GET requests. You must use the Rails form tags or remove CSRF protection to avoid that error.
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