Rails/Devise - Add ruby code in a locale file? - ruby-on-rails

I would like to know if there is a way to add ruby code in a locale file. For example:
devise:
failure:
unconfirmed: 'You have to confirm your account before continuing. <%= link to "send confirmation instructions?", user_confirmation_path %>'
Thanks for your help!

The way I have solved this in the past is:
devise:
failure:
unconfirmed: 'You have to confirm your account before continuing. {{confirm_link}}.'
confirm_link_text: "send confirmation instructions?"
Then where you need the output:
t('devise.failure.unconfirmed',
:confirm_link => link_to(
t('devise.failure.confirm_link_text'),
user_confirmation_path)
)

Related

Devise edit_password_url missing /

I've write a RoR application and I use Devise for some user management.
I've already configure my application to send email for password recovery and I use the following code in my reset_password_instruction.html.erb:
<p><%= link_to 'Modify password', edit_password_url(#resource, reset_password_token: #token) %></p>
In my config environment files I have defined: config.action_mailer.default_url_options = { :host => 'app_url.com' }.
When the application send a recovery email, it attach an url like this:
app_url.comuser insted of app_url.com/user.
What did I miss?
From the rails console, I see the url like it is suppposed to be.
<p><a href=3D"http://app_url.com/users/password/edit?reset_password_=token=3D47da8a08bd3d4asdfadfasdfa3eb9422388467adccfe095f058ca2a3352b12"=>Modify password</a></p>

how to solve forgot password when working on local but not working on heroku for devise in rails 4

I am using rails 4 and ruby 2. I have implemented login using devise. My problem is forgot password is not working on heroku but working on local. After receiving the mail when i clicked on "Change my password" link, it redirects to https://www.heroku.com/ page. How can I solve this issue? please help me if any one have any idea.
Codes:
reset_password_instructions.html.erb:
<p>Hello <%= #resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(#resource, reset_password_token: #resource.reset_password_token) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
Check if you configured your server's url in your config/application.rb:
config.action_mailer.default_url_options = { host: 'your.example.com' }
Find more details in the Rails Guide about Mailers.
In you config/environments/production.rb file, you should have a looking like
config.action_mailer.default_url_options = { :host => "http://eee.com" }
It defines the url used by action mailer when resolving url using the Rails url helpers. So If you set this value or change it, it should change the result.

Rails Tutorial, Hartl - ch. 10 - password/activate - Email link not redirecting to Heroku Site

The links that are sent in the password reset and account activation are being sent in the email, but when I click on them, they are not being redirected to my Heroku app and the browser gets stuck at "about:blank". Am I missing something? They look like this:
Reset password
Here is my app/views/user_mailer/password_reset.html.erb
<h1>Password reset</h1>
<p>To reset your password click the link below:</p>
<%= link_to "Reset password", edit_password_reset_url(#user.reset_token,
email: #user.email) %>
<p>This link will expire in two hours.</p>
<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>
Looks like the URL that you pass over to SendGrid is http://<your+heroku+app>.herokuapp.com/password_resets/..., so you'd need to make sure you specify your app's name instead of having http://<your+heroku+app>. If you're using SMTP, check to see if you have your config.action_mailer.default_url_options (in config/production.rb) setup properly:
config.action_mailer.default_url_options = { :host => 'http://appname.herokuapp.com' }
did same error and felt so LOL when I realized the solution:
check your config/environments/production.rb Listing 10.56
update host = '<your+heroku+app>.herokuapp.com' with your APP's name (in my case was host = 'twitterdave.herokuapp.com' )
Let me know if it helps

Devise "Confirmation token is invalid" when user signs up

Using Rails 4 and Devise 3.1.0 on my web app. I wrote a Cucumber test to test user sign up; it fails when the "confirm my account" link is clicked from the e-mail.
Scenario: User signs up with valid data # features/users/sign_up.feature:9
When I sign up with valid user data # features/step_definitions/user_steps.rb:87
Then I should receive an email # features/step_definitions/email_steps.rb:51
When I open the email # features/step_definitions/email_steps.rb:76
Then I should see the email delivered from "no-reply#mysite.com" # features/step_definitions/email_steps.rb:116
And I should see "You can confirm your account email through the link below:" in the email body # features/step_definitions/email_steps.rb:108
When I follow "Confirm my account" in the email # features/step_definitions/email_steps.rb:178
Then I should be signed in # features/step_definitions/user_steps.rb:142
expected to find text "Logout" in "...Confirmation token is invalid..." (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/user_steps.rb:143:in `/^I should be signed in$
This error is reproducible when I sign up manually through the web server as well, so it doesn't appear to be a Cucumber issue.
I would like:
The user to be able to one-click confirm their account through this e-mail's link
Have the user stay signed in after confirming their account
I have setup:
The latest Devise code, from GitHub (3.1.0, ref 041fcf90807df5efded5fdcd53ced80544e7430f)
A User class that implements confirmable
Using the 'default' confirmation controller (I have not defined my own custom one.)
I have read these posts:
Devise confirmation_token is invalid
Devise 3.1: Now with more secure defaults
GitHub Issue - Devise confirmation_token invalid
And have tried:
Setting config.allow_insecure_tokens_lookup = true in my Devise initializer, which throws an 'unknown method' error on startup. Plus it sounds like this is only supposed to be a temporary fix, so I'd like to avoid using it.
Purged my DB and started from scratch (so no old tokens are present)
Update:
Checking the confirmation token stored on the User after registering. The emails token matches the DBs token. According to the posts above, the new Devise behavior says not supposed to, and that instead it is should generate a second token based on the e-mail's token. This is suspicious. Running User.confirm_by_token('[EMAIL_CONFIRMATION_TOKEN]') returns a User who has errors set "#messages={:confirmation_token=>["is invalid"]}", which appears to be the source of the issue.
Mismatching tokens seems to be the heart of the issue; running the following code in console to manually change the User's confirmation_token causes confirmation to succeed:
new_token = Devise.token_generator.digest(User, :confirmation_token, '[EMAIL_TOKEN]')
u = User.first
u.confirmation_token = new_token
u.save
User.confirm_by_token('[EMAIL_TOKEN]') # Succeeds
So why is it saving the wrong confirmation token to the DB in the first place? I am using a custom registration controller... maybe there's something in it that causes it to be set incorrectly?
routes.rb
devise_for :users,
:path => '',
:path_names => {
:sign_in => 'login',
:sign_out => 'logout',
:sign_up => 'register'
},
:controllers => {
:registrations => "users/registrations",
:sessions => "users/sessions"
}
users/registrations_controller.rb:
class Users::RegistrationsController < Devise::RegistrationsController
def create
# Custom code to fix DateTime issue
Utils::convert_params_date_select params[:user][:profile_attributes], :birthday, nil, true
super
end
def sign_up_params
# TODO: Still need to fix this. Strong parameters with nested attributes not working.
# Permitting all is a security hazard.
params.require(:user).permit!
#params.require(:user).permit(:email, :password, :password_confirmation, :profile_attributes)
end
private :sign_up_params
end
So upgrading to Devise 3.1.0 left some 'cruft' in a view that I hadn't touched in a while.
According to this blog post, you need to change your Devise mailer to use #token instead of the old #resource.confirmation_token.
Find this in app/views/<user>/mailer/confirmation_instructions.html.erb and change it to something like:
<p>Welcome <%= #resource.email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(#resource, :confirmation_token => #token) %></p>
This should fix any token-based confirmation problems you're having. This is likely to fix any unlock or reset password token problems as well.
A friend of mine just found this question and emailed me asking if I had figured this out, which reminded me that I never submitted my own answer, so here goes :)
I ended up resetting the token & using send to get the raw token. It's ugly, but it works in a punch for devise (3.5.1).
26 it "should auto create org" do
27 email = FG.generate :email
28 visit new_user_registration_path
29 fill_in :user_name, with: 'Ryan Angilly'
30 fill_in :user_user_provided_email, with: email
31 fill_in :user_password, with: '1234567890'
32
33 expect do
34 click_button 'Continue'
35 end.to change { Organization.count }.by(1)
36
37 expect(page.current_path).to eq(confirmation_required_path)
38 u = User.where(email: email).first
39 u.send :generate_confirmation_token
40 email_token = u.instance_variable_get(:#raw_confirmation_token)
41 u.save!
42 os = u.organizations
43 expect(os.size).to eq(1)
44 visit user_confirmation_path(confirmation_token: email_token)
45 o = os.first
46
47 u.reload
48 expect(u.confirmed?)
49 expect(page.current_url).to eq(organization_getting_started_url(o))
50 end
As of devise 3.5.2, the confirmation token is no longer digested during the confirmation process. This means that the token in the email will match the token in the database.
I was still having trouble with confirmations after figuring this out, but in my case it turned out to be a bug I introduced when I overrode find_first_by_auth_conditions. By fixing the bug I introduced in that method, I fixed my errors with confirmation.

Cucumber devise sign in step failing (rails 3.2, cucumber, capybara, rspec)

Everytime i run the default devise user_step test " sign in" it fails.
Strange thing is the error i get
Scenario: User signs in successfully # features/users/sign_in.feature:12
Given I exist as a user # features/step_definitions/user_steps.rb:58
And I am not logged in # features/step_definitions/user_steps.rb:49
When I sign in with valid credentials # features/step_definitions/user_steps.rb:72
Then show me the page # features/step_definitions/user_steps.rb:152
And I see a successful sign in message # features/step_definitions/user_steps.rb:156
expected to find text "Signed in successfully." in "Mywebsite Follow us How it works More Login × **Invalid email or password**. Login * Email Password Remember me Not a member yet? Sign Up Now!Forgot your password?Didn't receive confirmation instructions? Rails Tutorial " (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/user_steps.rb:157:in `/^I see a successful sign in message$/'
features/users/sign_in.feature:17:in `And I see a successful sign in message'
As you see capybara/cucumber tries to connect but gets "Invalid email or password"
So I used a trick seen on SO and added to see what capybara really was getting
Then /^show me the page$/ do
save_and_open_page
end
It tries to use the credentials I put on top of my file user_steps.rb:
def create_visitor
#visitor ||= { :name => "Testy McUserton", :email => "example#example.com",
:password => "please", :password_confirmation => "please" }
end
But it gets me "invalid (i can see example#example.com written in the email field on the capyabra page so it understands that i want this to be the email.
I'm lost. Why doesn't it work ?
You need to create the user in the database first. User.create(name: "Testy McUserton", email: "example#example.com", password: "please", :password_confirmation: "please"). Then you can login with the credentials. You can also use factorygirl link

Resources