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

  • Home
  • SEARCH
  • 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 8637409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:28:26+00:00 2026-06-12T10:28:26+00:00

Rails 2.3.5 If below I’m using either a default session value or (with priority)

  • 0

Rails 2.3.5

If below I’m using either a default session value or (with priority) a form parameter, is there a way to write the code below to avoid a nil error?

<%= f.text_field :deliver_to_addr, :size => 100, :maxlength => 100, :value => params[:request][:delivery_to_address] || session[:address]  %>

I worked around the problem by putting a logic block in the controller to come out with a single variable to hold what should be selected (below), but is there a simple way to get around this problem so I don’t need a block of code every time I need to use a default value (session[:address] in this case)?

    if !params[:request].blank?
      if !params[:request][:delivery_to_address].blank?
        @delivery_addr_to_select = params[:request][:delivery_to_address]
      else
        @delivery_addr_to_select = session[:address]
      end
    else
      @delivery_addr_to_select = session[:address]
    end

<%= f.text_field :deliver_to_addr, :size => 100, :maxlength => 100, :value => @delivery_addr_to_select %>

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-12T10:28:27+00:00Added an answer on June 12, 2026 at 10:28 am

    You can use the build in try-method:

    @delivery_addr_to_select = params[:request].try(:[], :delivery_to_address) || session[:address]
    

    This will try to invoke the [] hash method with :delivery_to_address if the result is nil it will take the session[:address].

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

Sidebar

Related Questions

I created a form in Rails 3.2 using the format below, but when I
The code below showed a login form for rails 2.3.8 thru 3.0.10 but with
I have just begun Rails 3. I have generated the below code using the
I am using ruby on rails. Below given code works. However I was wondering
I have a rails module included for reference below that I have included using
Rails has no way of creating foreign keys in migrations (there are plugins to
Below I listed some code from simple Rails application. The test listed below fails
I have the code sequence below in a rails controller action. Before the IF,
Am using below specified gem acts-as-taggable-on(2.2.2) will_paginate(3.0.2) rails(3.0.3) ruby 1.8.7 i tried to use
I'm getting very ugly SQL queries with Rails code like one listed below: Facility.includes(:type,

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.