devise password in confirm email - ruby-on-rails-3

# in views/device/registration/new.html.erb:
<% password = Devise.friendly_token.first(8) %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name, :locale => I18n.locale)) do |f| %>
<%= f.hidden_field :password, :value => password %>
<%= f.hidden_field :password_confirmation, :value => password %>
<%= f.email_field :email %>
<%= f.submit "sign up" %>
<% end %>
# in views/device/mailer/confirmation_instructions.html.erb:
<p>Your Password: <%# #ressource.password %></p>
i'm generating password for new user. user gets a confirmation email and this should be the correct place to put the generated password. (could be possible to send him the password after confirmation)
but in the email #resource.password is nil!
how to i send the given password in device confirmation email?
alternative could be to store pwd in db without encryption, but i didnt succeed on this as well.

This is a bit hacky but workable solution to the problem you have mentioned.
# in views/device/mailer/confirmation_instructions.html.erb:
<% pass = Devise.friendly_token.first(8) %>
<p>Your password is <%= pass %></p>
<% #resource.update_attributes( { :password => pass, :password_confirmation => pass }) %>
This way you set the password for your user and email them right away. You can keep the code in views/device/registration/new.html.erb unchanged as user creation will fail if you remove password and password_confirmation fields.

I had some problems with the solution proposed by Kulbir Saini, because updating the password in that way, makes some devise callback to change the token, making the the confirmation to fail.
To solve this, I have taken Kulbir's and modified some code to update the password, but avoiding any devise callback.
It isn't probably the best solution, but it does the trick.
<% pass = #resource.confirmation_token.first(8) %>
<p>Your password is: <%= pass %></p>
<% #resource.update_column(:encrypted_password, ::BCrypt::Password.create("#{pass}")) %>

Related

How to validate password with confirm password [Rails 3.2]

How to validate password with confirm password in rails 3.2
my code not work
you can tell where my error
I've tried many variations changing the code in the controller.
Password saves but not validated to the password confirm field and password field.
help me please, help me )))
views
<%= form_for :password, :url => { :action => "change_password" }, :id => #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.password_field :password %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save", :class => "button blue" %>
<% end %>
User Controller
def change_password
#page_title = "Changing Zetfon account password"
#user = current_user
if request.post?
#user.password = Digest::SHA1.hexdigest(params[:password][:password])
if #user.save
redirect_to :action => 'profile'
flash[:status] = "Your password was changed. Next time you sign in use your new password."
else
flash[:status] = _('Your password not changed')
render :action => "change_password"
end
end
end
User Model
validates_confirmation_of :password
attr_accessible :password_confirmation
attr_accessor :password
add the following line to your model
validates :password, confirmation: true
Is it too late to simply use has_secure_password? You can learn about it in this RailsCast:
http://railscasts.com/episodes/270-authentication-in-rails-3-1
I'm not sure why you have if request.post?. Isn't that already determined by your route?
According to the documentation for validates_confirmation_of, I think you might need to add:
validates_presence_of :password_confirmation, :if => :password_changed?
Here's the documentation:
http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_confirmation_of
The documentation seems to indicate that you don't need attr_accessible :password_confirmation either.
I hope that helps.

Update model with response from an API after submitting form that uses nested attributes

Forgive me if this has been answered. I spent a few hours searching for the answer to this both here and on Google.
I'm just getting started with Ruby on Rails. I'm trying to update a model with data from an API response after saving the data originally submitted via the form to two different models.
So basically I have a User model and a Character model. The sign up form collects data on the user and their character. In order to make an API request I have to at least get a few details about the character to pass in as part of the requested attributes (i.e. name, realm).
So I'm using a form_for with nested attributes:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.fields_for :characters do |builder| %>
<%= builder.label :realm %>
<%= builder.text_field :realm %>
<%= builder.label :name %>
<%= builder.text_field :name %>
<% end %>
<%= f.submit "Sign Up" %>
<% end %>
This successfully submits all of the user info submitted to the User model and the realm and name submitted to the Character model. No problems there. However, I have several more fields that I want t fill in for the Character records just created from the API that provides all of the character info. What is the most efficient way to do this so that after the form submits, it updates the record (or adds data prior to save the record) with the data coming back from the API.
I don't need help on the specific fields and data returned. I have that all working, I'm just looking for a way to make the API request and complete the record with all data immediately after the form submits.
Suggest to use callback (:after_create) .
Depending upon the API processing you can choose between them .
:after_save
:before_save
:before_create
With any callback just get the data from your API and fill other character info's.
Hope that helps . Good luck .

Extract just what is needed from params[:form]

Users can send a reply to feedback they received. Here is the form:
<%= form_for :feedback, :url => reply_feedback_path do |f| %>
<%= f.text_area :reply, :size => '66x7' %><br>
<%= f.submit "Reply" %>
<% end %>
Here is the controller:
#reply = params[:feedback]
UserMailer.reply2_comments(#to_whom, #from_whom, #reply).deliver
If someone types in 'yo' into the text box, what is passed to the mailer is ' {"reply"=>"yo"} '
I'm having trouble with the syntax to extract just the content that was typed.
Thanks.
It looks like you're passing a hash to the mailer, and you just want the value for the key "reply". So try:
#reply = params[:feedback] || {}
UserMailer.reply2_comments(#to_whom, #from_whom, #reply['reply']).deliver
The main thing I changed here was changing #reply to #reply['reply'] in the mailer call
(I also added a nil-check to the first line to make sure #reply['reply'] won't cause an error if they don't submit the form by normal means)

Rails 3, Form submitting to its controller

I'm new to Rails and I've just spent another hour Googling and not finding an example.
So I have a simple form that I need to submit to an API. So I tried submitting it to the API directly but got advice that I do it in my app so I can do something with the results. Anyway, here's my simple form:
<%= form_tag(:action => 'submit') do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => #referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Everything I'm seeing has forms that that use a model, I have no need to persist this data, just pass it on to the API.
So my thought was I just create an action in the home controller, where this page lives and have the action submit to it but I get a RoutingError and it's: No route matches {:action=>"submit", :controller=>"home"}
So what do I need to put in the Routes.rb? I tried:
namespace :home do
resources :submit
end
No Joy... I'm sure it's simple but I just can't find the right example.
I think that you should have a look at the ruby guides, it's very well explained (but I don't think it talks about API) and it will save you a lot of time in the future, I swear.
Not sure what you mean but I see some wired stuff, so I hope to be useful, but if you're following some tutorials from the net let us know the link.
Basically what I do is always to call an action of a controller (MVC), following this way you should have a controller (?? apis_controller ??) and call one action of it.
So you want to use form_tag instead of form_for because you're not addressing a model, therefor you want to get rid of f. and use suffix _tag (api).
<%= form_tag(send_api_path) do %>
<%= text_field_tag :email, "Your email address..." %>
<%= hidden_field_tag :ref_code, #referralid %>
<%= hidden_field_tag :api_name, 'your_api_name' %>
<%= submit_tag "Enter To Win" %>
<% end %>
Then, in your apis_controller.rb you create an action send where you send and manage your request.
#apis_controller.rb
def send
# TODO: your code here
end
Another thing is to set the routes, something like
#routes.rb
match 'apis/send' => 'apis#send', :as => :send_api
I'm sure this is not 100% working code, but it should be useful
How to call the api? I had I fast look and found this.
When you ask for help it's always good to attach the error you get, this makes it easier for people to understand the problem.

Encoding problem with rails 3 application with post request

How can it be that some members of the development team have no problem with sending Post request with Russian symbols from form, but other members - have? All members are using Ubuntu.
The error is: "There were problems with the following fields: Username should use only letters, numbers, spaces, and .-_# please."
model:
validates_presence_of :username, :email
validates_uniqueness_of :username
view:
<%= form_for #user do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.submit "Change" %>
<% end %>
Any suggestions?
Thanks!
That looks like a validation error, but your validations don't seem to include it. Are you sure you're looking at the right model file?