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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:22:25+00:00 2026-05-13T17:22:25+00:00

I have a very common situation and a solution, but I would like to

  • 0

I have a very common situation and a solution, but I would like to ask the Rails experts out there if it can be improved.

I have a very typical RESTful controller where the user supplies some of the object attributes upon creation. There is a thing model, a ThingsController and various views, including new, create, and a _form partial.

A thing has two attributes,

  1. a color, which is set when they hit a link to create it (i.e. a “Create Red Thing” link that encodes the ID of red as a URL parameter)
  2. a description, which is entered by the user in the form view

I’m happy with the approach for dealing with an attribute like the description that that a user specifies in the form, but less confident about the way that I handle an attribute that is passed through via the URL parameters associated with the first click.

This is what I am doing right now (note that I have omitted error checking to simplify matters). First, my new and create methods in the controller are as follows:

def new
  @thing = Thing.new
  @thing.color = Color. find(params[:color])
end

def create
  @thing = Thing.new(params[:thing])
  @thing.color = Color. find(params[:color])

  if  @thing.save   
    flash[:notice] = "Successfully created thing."
    redirect_to somewhere_url
  else
    render :action => 'new'
  end
end

The new view just invokes the _form partial, which looks as follows:

<% form_for @thing do |f| %>
  <%= f.error_messages %>

  <%= hidden_field_tag "color", @thing.color.id %>

  <%= f.label :description %>
  <%= f.text_area :description %>
  <%= f.submit "Submit" %>
<% end %>

It seems a little messy to be passing the color ID through to the create method as a URL parameter by putting a hidden field in the form. Does this look reasonable, or is there another approach that would be better?

  • 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-13T17:22:26+00:00Added an answer on May 13, 2026 at 5:22 pm

    Normally in this situation, I would put a hidden field in the form, which will hold the color_id. The nice thing about this way is that you can get by with just setting the color of the object before you render the form. So your controller changes to:

    def new
      @thing = Thing.new
      @thing.color = Color. find(params[:color])  
    end
    
    def create
      @thing = Thing.new(params[:thing])
      if  @thing.save   
        flash[:notice] = "Successfully created thing."
        redirect_to somewhere_url
      else
        render :action => 'new'
      end
    end
    

    and your form will change to

    <% form_for @thing do |f| %>
      <%= f.error_messages %>
    
      <%= f.hidden_field :color_id %>
    
      <%= f.label :description %>
      <%= f.text_area :description %>
      <%= f.submit "Submit" %>
    <% end %>
    

    The color is then passed through both forms, and you only need to retrieve the color in the first form. (Don’t forget to add validations your Thing model to make sure it has a valid color though).

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

Sidebar

Related Questions

No related questions found

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.