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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:01:22+00:00 2026-05-17T00:01:22+00:00

I want to find a nice way to check my objects before i display

  • 0

I want to find a nice way to check my objects before i display them in the view so I wont get errors.

This is my controler

 @user = User.find_by_username(params[:username])
 @profile = @user.profile 
 @questions = @user.questions

and this is my view

 <% unless @profile.blank? %><%= link_to 'Edit Profile', :controller => 'profiles', :action => 'edit' %><% end %>


     <% unless @user.blank? %>
        Username:<%= @user.username %><br />
    Member Since:<%= @user.created_at.strftime("%d %B %Y")  %><br />
    <% end %>

  <% unless @profile.blank? %>
    First Name: <%= @profile.first_name %><br />
    Last Name: <%= @profile.last_name %><br /><br />
    About: <%= @profile.body %><br /><br />
    Location: <%= @profile.location %><br />
    Birthday: <%= @profile.birthday.strftime("%d %B %Y") %><br />
    <% end %>

As you can see, I’m using more than one checking of each kind ( check unless @profile.blank? ) and I think there will be a better way to do that.

Is there any Rails way to do something smarter than the one I’ve come up with ?

  • 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-05-17T00:01:23+00:00Added an answer on May 17, 2026 at 12:01 am

    As I see, there is no way you can skip this @.blank? validation as you dont want to display the records if they are empty but I have few suggestions

    1 – Make following sections as partials

    <% unless @user.blank? %>
        Username:<%= @user.username %><br />
        Member Since:<%= @user.created_at.strftime("%d %B %Y")  %><br />
    <% end %>
    

    and

    <% unless @profile.blank? %>
      First Name: <%= @profile.first_name %><br />
      Last Name: <%= @profile.last_name %><br /><br />
      About: <%= @profile.body %><br /><br />
      Location: <%= @profile.location %><br />
      Birthday: <%= @profile.birthday.strftime("%d %B %Y") %><br />
    <% end %>
    

    it will keep your view cleaner and will give you the flexibility of using them in anyware in your app

    2 – take the below line

    <% unless @profile.blank? %><%= link_to 'Edit Profile', :controller => 'profiles', :action=> 'edit' %><% end %>
    

    inside the profile display as its more appropriate

    cheers

    sameera

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

Sidebar

Related Questions

I want to find a way to develop database projects quickly in Visual Studio.
I want to find a sql command or something that can do this where
I want to find most optimal way to iterate values in key in python.
I'm trying to find a nice way to compare two strings when reading line
I want to find all occurences of BBB in a string and substitute them
I am trying to find a way to get a particular UIView given a
Hello I was wondering if there is a nice way to find out what
I have a really simple problem, but I don't find a nice way how
I want find all Saturdays and Sundays in A given month. How can I
I want to find records on a combination of created_on >= some date AND

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.