Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:12:34+00:00 2026-06-14T17:12:34+00:00

I am using Devise with Rails for my user login. It all works great,

  • 0

I am using Devise with Rails for my user login. It all works great, however, i am unable to get a specific redirection working.

I am new to rails so the method in which i am doing things may not be correct.

Essentially, with an example, i am allowing users to like a post. However, they need to be signed in to do so. I have created an action in my controller called ‘like’ and the controller has the devise filter

before_filter :authenticate_user!, :except => [:index, :show]

entered thereby the sign in page is being shown. Once the user signs in i want to redirect them back to the post which they have liked with the ‘like’ action having been called.

my controller looks like

def like

  @post = Post.find(params[:id]) 
  @like = Like.new;
  @like.user_id = current_user.id;
  @like.post_id = params[:id];
  respond_to do |format|
    if @like.save
      format.html { redirect_to @post, notice: 'You have like this post' }
      format.json { head :no_content }
    else 
      format.html { redirect_to @post, notice: 'Sorry you like was not saved  } 
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end  
end 

Naturally i cannot hard code the path using after_sign_in_path_for

def after_sign_in_path_for(resource)    
  #path to somewhere
end

But i have read that i can use a session variable and perhaps call that in the above code. In which part of Rails could i write the session variable, as it would be too late in the controller action (as devise takes over before it hits the like action) and i cannot see how to set it in the view.

Also my link looks like

<%= link_to "Like", {:controller => :posts, :action => :like, :id => @post.id}, {:method => :post } %> |

PS the redirection when using creating a new ‘post’ works ok i.e. the user signs in and are redirected to the new post view.

Any help and tips would be greatly appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:12:35+00:00Added an answer on June 14, 2026 at 5:12 pm

    You are experiencing this because like action is specifically designed for POST. Therefore, you should make sure that user is signed in before you POST to that URL, and doing it with session is tricky:

    • You’d have to unprotect like method by excluding it from before_filter
    • Then check manually if user_signed_in? (mind you this is a helper method),
    • Then (if user is not signed in), stash what you are liking in session and redirect to sign in page with return URL
    • Upon user visiting this return URL (it will be a GET and not a POST), you would have to look up the session info and do what original Like was supposed to do (but by then user will be signed in).

    Seeing that all of this dance will end with a GET request, why not make Like action work on GET requests as well as pass parameters in the Query String in the first place? It will require 0 code changes and it will not expose you to a security threat since Like is protected by before_filter. You would just have to make sure that your Like links aren’t followed by search engines by using rel=”nofollow” on your <a> tags.

    For a related discussion, see redirect_to using POST in rails. There, one of the suggestions is to build and submit a form on the client via JavaScript. That would have to happen on that return URL view once user has authenticated. This might be the best compromise if you object to having your like action exposed as GET (which violates REST principles)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a new rails app (3.2.8) using Devise. All is setup properly,
So I am using Devise for user authentication in my rails app. I have
We have a web app running on Rails, using Devise and OmniAuth for user
I have a class User in Rails using Mongoid and Devise. I can't seem
I'm using Rails, iOS, and devise. I want to read a single user. The
I currently am using Rails' Devise plugin to handle login/authentication. I am using a
I am using Devise to manage authentication in a Rails 3.1 application. It works
I have a rails app that is using Devise, with a User model, no
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I'm using rails 3.2 and devise 2.0 and I'm quite new to Rails. Requirements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.