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 7967563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:56:41+00:00 2026-06-04T06:56:41+00:00

I have a blog in my website where only authenticated users can post. If

  • 0

I have a blog in my website where only authenticated users can post. If an existing user that is not yet authenticated is typing a comment when he hits “create” he wil be redirected to the login view to authenticate and then redirected to the articles view. I would like to improve the functionality of my blog by rendering the view where the user was creating the comment so that he doesn’t have to type in his comment again.

I am rather new to programming and to ruby so I am not sure how to approach this problem. Any suggestions are welcomed. Thank you in advance.

My website can be found at https://github.com/MariusLucianPop/mariuslp-

Bellow are what I think are the important parts of the code. Let me know if you would like me to update anything.

comments_controller.rb

before_filter :confirm_logged_in, :only => [:create]


def create
  article_id = params[:comment].delete(:article_id)
  @comment = Comment.new(params[:comment])
  @comment.article_id = article_id
  @comment.posted_by = session[:username]
  @article = Article.find(article_id)
  if @comment.save
    redirect_to article_path(@article)
  else
    render "articles/show"    
  end
end

protected 

def confirm_logged_in
  unless session[:user_id]
    flash[:notice]="Please login before posting a comment."
    redirect_to login_path
    return false
  else
    return true
  end
end

visitors_controller.rb

def login
  #atempting to log in
  authenticated_user = Visitor.authenticate(params[:username],params[:password])
    if authenticated_user 
      session[:user_id]=authenticated_user.id
      session[:username]=authenticated_user.username
          flash[:notice] = "You are now logged in."
          redirect_to articles_path
      end
    else
      if !params[:username].blank? # only rendering the notice if the user tried to login at least once
        flash[:notice] = "Invalid username/password combination. Please try again"
      end
      render "login"
    end
end

routes.rb

  root :to => "static_pages#index"

  get "static_pages/work_in_progress"

  get "categories/new"
  match "work" => "static_pages#work_in_progress"

  match "login" => "visitors#login" # not rest
  match "logout" =>"visitors#logout" # not rest

  resources :articles do 
    resources :comments
  end

 resources :tags, :taggings, :visitors, :categories

 match 'contact' => 'contact#new', :as => 'contact', :via => :get
 match 'contact' => 'contact#create', :as => 'contact', :via => :post
  • 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-04T06:56:42+00:00Added an answer on June 4, 2026 at 6:56 am

    A quick approach\KIS

    app / views / articles / _comment_form.html.erb

    <% if confirm_logged_in  %> # helper method to detect if current is logged in
    
      <%= form_for [@article,@article.comments.new] do |f|%>
        <%= f.hidden_field :article_id%>
    
        <%= f.label :body %><br />
        <%= f.text_area :body, :cols => 50, :rows => 6 %><br />
    
        <%= f.submit%>
      <%end%>
    
    <% else %>
    
      <%= link_to "Login to add a comment", login_path %>
    
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a blog that has a redirect loop, and I can't understand htaccess
I'm creating a Django powered website that will have numerous applications (Blog, Shop, Portfolio,
I have a static website with files like index.php, blog.php, contact.php etc How can
I have a PHP based website that uses coupon codes. I can restrict search
Two questions only: I have a website including a section of Blog in wordpress.
I have 1 blog say xyz.blogspot.com ,now am uploading my own website,so I want
I have been asked to add a blog for a website built in Drupal
I have a website which slides to a section called blog in my HTML.
I have a drupal website and i am using wordpress for blog.My goal is
I have a blog installed in www.foo.com/wp/ and would like all requests that go

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.