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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:00:53+00:00 2026-06-14T21:00:53+00:00

What is the best approach here? I’m trying to clean up some code and

  • 0

What is the best approach here? I’m trying to clean up some code and I’m wondering if the controller is the best place for this variety of logic:

if user_signed_in?
  if current_user.try(:admin?)
    @docs = Doc.chronologic.page(params[:page]).per(5)
    @orders = Order.chronologic.page(params[:page]).per(5)
  else
    @docs = Doc.chronologic.where(:user_id => current_user.ftp, :retired => "active").page(params[:page]).per(5)
    @orders = Order.chronologic.where(:user => current_user.ftp).page(params[:page]).per(5)
  end
  respond_to do |format|
    format.html
    format.json { render json: @docs }
  end
else 
  redirect_to new_user_session_path
end

If there’s a better location for it, where would it be?

Thanks!

Edit: it’s far worse for methods like pdf which has line after line of instructions for Prawn, but I can’t seem to get send_data to work from the model.

  • 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-14T21:00:54+00:00Added an answer on June 14, 2026 at 9:00 pm

    This is basically what mu said, but here’s my take.

    In your app controller:

    def require_logged_in
      redirect_to new_user_session_path unless user_signed_in?
    end
    

    In your controller

    before_filter :require_logged_in
    
    def some_action
      @docs = Doc.chronologic.for_user(current_user).page(params[:page]).per(5)
      @orders = Order.chronologic.for_user(current_user).page(params[:page]).per(5)
      respond_to do |format|
        format.html
        format.json { render json: @docs }
      end
    end
    

    In your Doc model

    scope :for_user, lambda do |user|
      where(:user_id => user.ftp, :retired => "active") unless user.admin?
    end
    

    And something similar in your Order model.

    Per your edit, definitely don’t do send_data from your model.

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

Sidebar

Related Questions

I'm wondering what the best approach to take here is. I've got a form
Not sure if this is the best approach, however here are my thoughts I
What is the best approach to add code for detecting an iPad Safari user.
I am wondering what my best approach to do what I want to achieve.
New to rails so not sure what the best approach is here. I want
What's the best approach to import multiple lines from a text_area in a form?
What is the best approach to handle authentication on a bunch of Javascript actions
What is the best approach for mocking out a static class in c#. For
What is the best approach to render a large number of 2D graphical elements
What is a best approach to make a function or set of statements thread

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.