0

I am creating a blogging application which uses active_admin for admin panel. When I try to comment on a post it is redirecting me to active_admin login page.

Comment Model:-

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :post, :counter_cache => true
  attr_accessible :text, :user_id, :post_id

  validate :text, :presence => true
end

Active Admin Comment Model:-

ActiveAdmin.register Comment, :as => "PostComment" do

end

Posts Controller:-

def show
    @post = Post.find(params[:id])
    @showPost = true
    respond_with do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
end

Comments Controller:-

class CommentsController < ApplicationController
    before_filter :auth_user

   def create
     @post = Post.find(params[:post_id])
     params[:comment][:user_id] = current_user.id
     @comment = @post.comments.create(params[:comment])
     redirect_to post_path(@post)
   end
end

Comment Form:-

<%= semantic_form_for ([@post, @post.comments.build]), :html => { :id => "comment-form", :class => "metta-form"} do |f| %>
    <h5 class="pull-left">Comments</h5>
  <div class="clearfix">
  </div>
    <div class="wrap-comment-form">
        <%= f.inputs do %>
            <%= f.input :text, :label => false, :input_html => { :class => "comment-text pull-left" } %>
        <% end %>
        <%= f.actions do %>
            <%= f.action :submit, :label => "Add Comment", :button_html => { :placeholder => "Enter Your Comment Here", :class => "btn btn-primary btn-metta pull-right" } %>
        <% end %>
    </div>
<% end %>

When I click on the add comment button it takes me to active admin login page. Can someone help me out here ??

class ApplicationController < ActionController::Base
  protect_from_forgery

  helper ApplicationHelper

  def after_sign_in_path_for(resource) 
    '/users/home'
    end
  def after_sign_up_path_for(resource)
    '/users/home'
  end
  def auth_user
    redirect_to new_user_path unless user_signed_in?
  end
end

1 Answer 1

0

Look here: before_filter :auth_user

So the reason you have been redirected to login page is you have not logged in yet.

4
  • I am logged in. The auth_user action is defined in applications controller. It prevents user from getting to that page. I am using this function in all the views except home page. Apr 17, 2013 at 15:57
  • I have added application controller in my question. Apr 17, 2013 at 16:03
  • The only logic to redirect to sign in page is that before_filter. If you've already signed in, you may want to check if the method user_singed_in? works as expected, to really return true for signed in user.
    – Billy Chan
    Apr 17, 2013 at 16:12
  • I have checked, and user_signed_in? works. Also the sign in page to which an unauthorized user is being redirected for other views is my own custom sign in page not the active_admin sign in page. This is happening only while trying to add new comment. Apr 17, 2013 at 16:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.