I am using has_secure_password method, how to make change password form and controller action?
I think it must look like:
<%= form_for #parent, :url => update_password_path(#parent) do |f| %>
<%= f.label :old_password %><br />
<%= f.text_field :old_password %><br />
<%= f.label :new_password %><br />
<%= f.text_field :password %><br />
<%= f.label :password_confirmation %><br />
<%= f.text_field :password_confirmation %><br />
<%= f.submit %>
<% end %>
I'm still not sure how to do this the 'correct' by the standards way, but yeah, you would want a form like that. The controller action would check if the old password is valid by doing
user = User.find(session[:user_id]) if session[:user_id]
if user and user.authenticate(params[:old_password])
if params[:password] == params[:password_confirmation]
user.password_confirmation = BCrypt::Password.create(params[:password])
respond_to do |format|
format.html # reset_password_success.html.erb
end
else
redirect_to reset_password_url, notice: "Incorrect Password."
end
else
redirect_to reset_password_url, notice: "Incorrect Password."
end
Make sure you filter the passwords, just like they're filtered when a new user registers.
I don't know if your form will work or not. I find that if I'm stuck on how to create a form the right way, or in a hurry, I just hack it together using regular old HTML or use it as a crutch. Decide this based on your time constraints and past experience.
If the form is working correctly, but there is an error in handling the request, make sure that you really do access the params using params[:old_password] and such. It might be nested inside of another params object like params[:user][:old_password]. You can find out how the parameters are being sent by looking at your rails console, or by looking at the logs. If this is hosted on Heroku, you'll need to ask Heroku for it's logs => https://devcenter.heroku.com/articles/logging
And you can also figure it out by listening to network traffic (dev tools on major browsers or using Fiddler => http://www.fiddler2.com/fiddler2/)
This code is assuming that you're going off of the Agile Web Development with Rails (4th ed.) book. If not, then some adjustments will have to be made.
Related
I tried the ckeditor and it works fine when adding or making a new post but the problem is when I try and edit the post.
it doesn't load properly and it doesnt show anything like the code , or even a spec, it just shows the pre default ckeditor list and it doesn't even show any data coming from the database.
any help would really be great, and info would be awesome.
I have made it into a form and then rendering it so that I can just call the function like so.
This is the _form
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text, :value => 'Default value', :id => 'sometext' %>
</p>
Now this is in my edit
<%= render 'form' %>
I tried adding the raw into it like so
<%= raw render 'form' %>
or even into the _form but it doesn't seem to show properly
after tinkering and trying the usual thing the codes show it.
<p>
<%= f.label :text %><br>
<%= f.text_field :text %>
</p>
when I try it and add this now it shows here , but the ckeditor doesn't show properly.
Just give this:
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text %>
</p>
I hope you are calling this partial inside a form_for.
Update the answer,
Just remove
:value => 'Default value',
I'm developing an application that uses the devise gem to provide authentication for the user model. A while ago I decided to implement an Admin class also using devise, instead of putting an admin:boolean onto the existing user model. After I did this, for some reason the standard user sign up and update account buttons stopped working.
The only thing I changed on the standard devise user forms was to add a link to 'admin login' on the sign in view (which still worked) to the devise admin sign in page. Both the sign in pages worked.
Now here's the thing, both the sign up and update account buttons worked after a page refresh.
I have since removed the Admin class and resorted to using a boolean on the User model, but the problem persists. Additionally, the problem is present when I run locally in dev mode, on a staging production environment on heroku and on our AWS Ubuntu testing instance.
I have been pulling my hair out for like three weeks over this, has anyone experienced a similar error?
Help. Please.
Using:
Rails 4.1.1
Devise 3.2.4
twitter-bootstrap-rails 2.2.8 (not using bootstrap themes on devise views though)
Code:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :username %><br />
<%= f.text_field :username, :autofocus => true %></div>
<div><%= f.label :firstname %><br />
<%= f.text_field :fname %></div>
<div><%= f.label :lastname %><br />
<%= f.text_field :lname %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :mobile %><br />
<%= f.number_field :mobile %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<p>You must read and accept My Company's <%= link_to "Terms and Conditions", '/home/terms' %> to register.</p>
<%= f.label :tos_acceptance, "I accept" %>
<%= check_box( :user, :tos_acceptance, {}, true, false )%></div><br>
<div><%= f.submit "Sign up" %></div>
<%= render "devise/shared/links" %>
<% end %>
NB: I'm certain this is a view error. I've also inspected the elements using chrome console, there is apparently no difference between the non refreshed button (doesn't work) and the refreshed button (which works). :/
Fixed this, looks like the standard devise views were conflicting with bootstrap3 (or something).
Using the gem 'rails_layout' and running
$ rails generate layout:devise bootstrap3
rebuilt the view files, all I needed to do was add my custom fields back in and it worked perfectly.
So I am working through the Michael Hartl tut and this app works perfectly on the localhost but the moment I deploy to heroku it wont create a user when i submit the information. In fact it just sits there as if I just clicked on an empty screen, no error message nor a rediret. I looked at the heroku logs and there are no exceptions that I can see being logged. I tried updating the controller behavior but i get the same result. This is frustrating.
my form looks like this:
<div class="main-form">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages'%>
<%= f.label :name %>
<%= f.text_field :name, class: "active" %><br/>
<%= f.label :email %>
<%= f.text_field :email %><br/>
<%= f.label :password %>
<%= f.password_field :password %><br/>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %><br/>
</div>
<div class="actions">
<%= f.submit "create my account", class: "btn btn-lg btn-default" %>
</div>
<% end %>
my controller looks like:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def create
#title = "welcome"
#user = User.new(user_params)
if #user.password_confirmation.empty? == false
#user.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
i have also tried setting up my create method like:
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
render 'new'
end
end
neither of these methods worked? Any advice would be welcome.
If you inspect the page it looks like your submit button is outside of the form definition:
I'm trying to prevent a form from being "double posted", when the user either clicks twice or hits submit twice.
I've seen a couple posts on this, but they haven't hit this issue per se. I can't seem to get the below to stop double-posts, and I'm getting the feeling it's related to the remote => true (using ajax to show the content on the page).
Below is my form:
<%= form_for([#posts, #comment], :remote => true) do |f| %>
<%= f.text_field :comment %>
<%= f.submit "Submit", class: "btn btn-large btn-primary", :style => 'display: none;', :disable_with => '' %>
<% end %>
Any recommendations would be great. Thank you!
Use the disable_with option
<%= submit_tag :submit, :id => 'submit_button', :value => "Create!", disable_with: "Creating..." %>
The other answer didn't work for me — I believe it was from the Rails 2 era. According to the docs, the disable_with attribute should be added within a data attribute, like so:
<%= submit_tag "Complete sale", data: { disable_with: "Please wait..." } %>
I'm following along with the Rails Guides - Getting Started tutorial. It makes a basic Post model, and a Comment model that belongs to Post.
I have added a simple validation to the Comment model, and it works, but I can't figure out how to get form errors to display if I fill it out wrong.
Here is my comment.rb model
class Comment < ActiveRecord::Base
validates :body, presence: true
belongs_to :post
end
Here is the original form for adding a comment, it's in posts/show.html.erb
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And the original create action in comments_controller.rb
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment])
redirect_to post_path(#post)
end
end
I've tried quite a few things, but it all feels like fumbling around in the dark. Can someone point me in the right direction please?
Take a look at the dynamic_form gem - this used to be part of Rail itself but was extracted a while back. With it, you can display errors inline like this:
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
<%= f.error_message_on :commenter %>