I'm trying to implement the password reset functionality in an app, using Devise. I'm at a point where the "forgot password" link works (in that it sends the email), and the email contains the link to "Change my password".
The change password link works and sends you to a form (at the URL http://localhost:3000/users/password/edit?reset_password_token=c7cKb4hMhSLRs72n9dYj) asking for the new password (and to confirm it), but after typing in a new password and clicking "Chang my password", it redirects to localhost:3000/users/password and says "Please review the problems below:" and presents a from with one field for Email, and a button for "Send password reset instructions". This is, of course, the form for submitting your email to get password reset instructions, but it also has an error notice of "can't be blank", as if someone clicked the button with the field empty.
I used a tutorial to configure ActionMailer to send the email, which I thought was going to be the most difficult part of this process, but now I can't figure out why the form to change one's password is not working.
I'm very new to Rails, and this is my first time working with Devise and ActionMailer, so I apologize if I haven't included some needed information. Just let me know and I can add anything that helps.
Thanks.
Ok, so with the help of someone at a Rails meeting tonight, I found out what the problem was. It didn't really have anything to do with the controllers or routes, but rather I had caused the bug by incorrectly adding some code to the form. Here's the corrected code for my form:
<h2>Change your password</h2>
<%= simple_form_for(resource, :as => resource_name, :url => password_path(resource_name),
html: { method: :put, class: "pure-form pure-form-stacked" }) do |f| %>
<%= f.error_notification %>
<%= f.hidden_field :reset_password_token %>
<%= f.input :password, label: "New password", autofocus: true, required: true %>
<%= f.input :password_confirmation, label: "Confirm new password", required: true %>
<div><%= f.submit "Change my password", class: "pure-button" %></div>
<% end %>
<%= render "devise/shared/links" %>
My problem was that instead of including the class: "pure-form pure-form-stacked line as a second key/value pair into html: {}, I had a second html: {} line.
In case that's not clear, I had this:
html: { method: :put },
html: { class: "pure-form pure-form-stacked" }
instead of this:
html: { method: :put, class: "pure-form pure-form-stacked" }
and for some reason, in the first version, only the second html: {} was being submitted with the form, so it didn't get processed as a put request.
Related
I have a problem when trying to submit a form with a div (instead of a f.submit button)
<%= form_for #user, url: {action: "submit_user"}, html: {id: "user_submit", class: "form"}, :method => "post" do |f| %>
<%= f.text_field :first name, :required => true, :placeholder => 'first name', :autocomplete => :off %>
<%= f.text_field :last name, :required => true, :placeholder => 'last name', :autocomplete => :off %>
<div id="submituser" class="some very fancy css here" onclick = "document.forms['user_submit'].submit();">
<% end %>
The problem is that the form is sent, but without the validations (causing it to send blank first and last name if nothing is entered)
note that when using
<% f.submit "submit" %>
validations do work.
Thanks
I guess document.forms['user_submit'].submit(); does not trigger the submit event, so You have to launch validation manually. But what is the point of using div instead of button? You could style a button with some very fancy css here too. OR make a 'hidden' submit button and add onclick = "document.form.button.click()" to div :-)
Found my answer here:
Difference between handling $(“form”).submit or an click event?
And i quote #Giovanni Sferro:
The click callback is called only if the user actually click on the submit button. But there are cases in which you would submit the form automatically by javascript, in that case the click callback is not fired while the submit callback is. I would recommend to use always the submit for validation and intrinsic action of the form, while using the click callback for animation or other things related to the action of clicking on the button.
so my (working) code looks like this:
<div id="submituser" class="some very fancy css here" onclick = '$("#user_submit input[type=\"submit\"]").click();'>
I have a Ruby on Rails app.I am using Devise for authentication.I have set a 30 minutes timeout for the users which auto logs out the users on 30 minutes of browser inactivity.The problem I am facing is that when the session logs out on timeout after 30 minutes,it takes to the login screen and the browser saved email address and password shows up in the login text boxes.But on clicking the login button it doesn't logs me into the application.When the page reloads after the login fail,I can login as usual.
When i checked the problem further,I found that the email address and password values are nil even though it shows up on the browser screen.
The Login form code is given below:
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name,:role => "super_admin")) do |f| %>
<%= f.email_field :email, :placeholder => "EMAIL", :data => { :'placeholder' => 'EMAIL'}, :class => 'with_holder' %>
<%= f.password_field :password, :placeholder => "PASSWORD", :class => 'disablecopypaste with_holder' , :data => { :'placeholder' => 'PASSWORD'}%>
<% if devise_mapping.rememberable? -%>
<%= f.check_box :remember_me %>
<% end -%>
<a id="save_post" href="javascript:void(0);">LOG IN</a>
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to "FORGOT PASSWORD?", new_password_path(resource_name, :role => 'super_admin') %>
<% end -%>
Am I doing anything wrong here?it would be really helpful if anyone can help me with this problem
If you see Email and Password on your login form when reloading the page, it is the browser behavior, not your Devise. It could be because your input has autocomplete attribute and you have selected to Save Password on your browser. Try setting autocomplete=off on your input to see if it helps
At Last found out the issue..The issue was in submitting the form,the form was not being submitted in usual method.It as being submitted from a JS method which catches the click event of <a id="save_post" href="javascript:void(0);">LOG IN</a> and submits the POST action of its parent form.
The issue was that the form was not being Loaded at the time of the click and hence it was returning empty strings for the email and password fields.Once I changed the method of submitting the form to the usual way ie putting a form submit button
<%= f.submit "LOG IN", :id => "login_submit" %>
it works fine
As the button is also an element inside the form,it will only get loaded once other elemts inside the form get loaded,hence it wont return empty strings on clicking the button.
I am working on Ruby 2 and rails 4. I want to add html validation 'required' inside the form_tag of Rails.My codes are below.
<%= form_tag file_download_contacts_path, :method => :post, :id => 'contact-form', :class => 'special' do |f| %>
<div><%= label_tag :Name %><br />
<%= text_field_tag "contact[name]" , nil, placeholder: "Your Name" %></div>
<div><%= label_tag :Email %><br />
<%= email_field_tag "contact[email]" , nil, placeholder: "your#mail.com" %></div>
<div><%= label_tag :Mobile %><br />
<%= telephone_field_tag "contact[phone]" , nil, placeholder: "Your Contact No" %></div>
<div><br /><%= button_tag 'Submit' %></div>
<% end %>
click on submit button it directly goes to file_download_contacts_path page though the text areas are blank. How can I prevent this? In Html we can use 'required'. Please help me if any body can. Thank you.
Something like this should help you
$(document).ready(function () {
$(“#contact-form”).validate({
debug: true,
rules: {
“contact[email]“: {required: true, email: true},
“contact[name]“: {required: true, name: true},
“contact[phone]“: {required: true, phone: true}
}
});
});
Source
Rails cannot do any form validation before it's submitted, since the validation needs to happen in the client (and rails is of course server-side). You need to use javascript. If you google "javascript form validation" you will see a tremendous amount of information.
jQuery is a very popular javascript library which has various form validation plugins, google and have a look.
Note: Above answers are all correct and perfect with simple explanation, but in case if you want to show an actual error message to the user, (In case you want to a complex validation), then below answer is for you,
Here In my scenario I have two checkbox in my form, and I want to validate that at least one checkbox should be clicked, if not then I will show a error message to the user.
<%= form_tag( add_to_telegram_path, method: :post, id: 'add-to-telegram-form') do |t| %>
Select channel
<%= check_box_tag "channel_names[]", id: 'channel_1'%> Channel 1
<%= check_box_tag "channel_names[]", id: 'channel_2'%> Channel 2
<%= button_tag "Send", :class => 'btn btn-success', id: 'add-to-telegram-btn', onclick: "validTelegram()" %>
<%end%>
Then in JS,
function validTelegram() {
event.preventDefault();
var checkboxes = Array.from(document.getElementsByName("channel_names[]"));
if(checkboxes.reduce((acc, curr) => acc || curr.checked, false)){
$('#add-to-telegram-form').submit();
} else {
//swal('Error','Please select atleast one channel!','warning');
alert('Some error!')
}
}
I called a JS function on the submit click of form_tag, (If you are using sibmit_tag replace it with button_tag)
I have prevented default behaviour of the submit button
Then I started to get the elements by its Id/Class or by Name, and checked what ever I want to do in the JS, and then based on my validation Either I am calling submit() on the form ID, or showing the error message.
Note: For showing error message I have used Sweet alert, you can stick with the alert();, but in case If you are looking for better UI, then uncomment my swal() code in JS and comment out alert();
You can read more about Sweet Alert PopUp
i am trying create a form in a Ruby on Rails application, which will send the username and password from my database to Joomla Admin Login.
I can't find the right method the controller of the model needs.
A second thought is to use Javascript but i don't know the way. I found some code, but i would like some explanations about this. I should write it straight to html.erb file or somewhere else?
I tryed to do it by creating a form in my show.html.erb
<%= form_for myproject, url: myproject.admin_url, method: :get do |f| %>
<%= f.hidden_field :mod_login_username_lbl, name: myproject.project_user,
value::get%>
<%= f.hidden_field :mod_login_password_lbl, name: myproject.project_pwd,
value: :get %>
<%= f.button "Log in", :button_html => {:class => "btn primary",
:onclick =>"this.form.submit();" }%>
<% end %>
The resault is that, i can see the url at the browser that shows the username and the password but i want to know how to enter them to joomla form and submit to login.
http://www.myjoomla.gr/final_project/administrator/?utf8=%E2%9C%93&admin=get&123456=get&button=
I am using "get" method, because "put" did not do anything.
Thanks in advance for your help!
I currently am using Rails' "Devise" plugin to handle login/authentication. I am using a very basic form_for tag to handle the log-in form. Here is the code:
<div class="well-outer splash-signin-block">
<div class="well-inner">
<h2>Sign in</h2>
<div>
<%= form_for(resource, as: resource_name, url: user_session_path, html: {
class: "form-inline"
}) do |f| %>
<p><%= f.text_field :email, placeholder: "Email", autofocus: true %></p>
<p><%= f.password_field :password, placeholder: "Password" %></p>
<p>
<%= f.submit 'Sign in', class:"btn btn-primary btn-large" %>
<%= f.check_box :remember_me, class: "checkbox" %>
<%= f.label :remember_me %>
</p>
<% end %>
</div>
</div>
</div>
However, I would like to change that "Remember me" checkbox in to a button. The user experience would be that they could either click "Sign in" (the default action) or they could click "Remember me" to sign in persistently.
Behind the scenes, I imagine this would work by using a hidden field that sets the resource[remember_me] property to "1". However as a rails newbie I don't know how to go about coding this. I imagine it will need some javascript and that is fine, but googling has only turned up AJAX-y stuff and I don't think that's necessary. I won't shy away from it if it is necessary, though.
You would create a button with some id, then use javascript to bind to the click event of that button. When the button is clicked, you want to change the value of a hidden field. If you were using jQuery, it would be something like...
$('#id_of_button').click(function() {
$('#id_of_hidden_field').val("true");
});
Then you just check for that value in the controller like you normally would.
If you're not using jQuery it's not much more work, I just don't know it off the top of my head. Btw this code should be located in a .js file in your assets/javascript folder