0

I'm creating a newsletter.

Each email contains a link for editing your subscription:

<%= edit_user_url(@user, :secret => @user.created_at.to_i) %>

:secret => @user.created_at.to_i prevents users from editing each others profiles.

def edit
  @user = user.find(params[:id])
  if params[:secret] == @user.created_at.to_i
    render 'edit'
  else
    redirect_to root_path
  end
end

It doesn't work - you're always redirected to root_path.

It works if I modify it like this:

def edit
  @user = user.find(params[:id])
  if params[:secret] == "1293894219"
  ...

1293894219 is the "created_at.to_i" for a particular user.

Do you have any ideas why?

1 Answer 1

2
if params[:secret] == @user.created_at.to_i.to_s

The parameter is a string not an integer.

Sign up to request clarification or add additional context in comments.

2 Comments

Big thank you to you sir :) I should learn more about this stuff
No problem! I remember it was big learning curve - I still get caught out by things like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.