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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:27:55+00:00 2026-05-24T11:27:55+00:00

Let’s say I have on my controller: @parts = Part.all and on the view

  • 0

Let’s say I have on my controller:

@parts = Part.all

and on the view form:

<%= f.collection_select(:part_id, @parts, :id, :title, { :prompt => true } %>

It’s working for the new/create actions, edit action works too but I would like to know how to get the selected value for doing a find on another model, since it’s a nested form.

Or the only way is get it using some javascript?

Doing:

<%= debug f.object.part %>

I get this:

--- !ruby/object:Part
attributes:
id: 1
title: Pearl 02
part_type_id: 36
created_at: 2011-07-28 07:52:09.000000000Z
updated_at: 2011-07-28 08:34:02.000000000Z
set: !!null 
price: 3.53
 changed_attributes: {}
 previously_changed: {}
 attributes_cache: {}
 marked_for_destruction: false
 destroyed: false
 readonly: false
 new_record: false

Which the only attribute I want for now is “price”.

Have tried to access the value doing:

<%= f.object.part.price %>

but then it returns an error:

undefined method `price' for nil:NilClass

the trace shows me something related to an helper method I have also.

app/views/items/_item_part_fields.html.erb:7:in `_app_views_items__item_part_fields_html_erb___1683960156823546996_2175548540_683257483947746572'
app/helpers/application_helper.rb:46:in `block in link_to_add_fields'
app/helpers/application_helper.rb:45:in `link_to_add_fields'

the helper method that causes this error:

def link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
end
  link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" )
end  

Removing the link to this helper method and calling f.object.part.price works.

What you think?

  • 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-24T11:27:55+00:00Added an answer on May 24, 2026 at 11:27 am
    <% f.object.part_id = XXX %>
    <%= f.collection_select(:part_id, @parts, :id, :title, { :prompt => true } %>
    

    Or better to set this stuff in controller (since it is nested form I can’t write solution with controller)

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Let say I have my view that I use it as a toggle button.
Let's say that I have a html form (actually I have an editor -
Let's say I have a simple form in the administration of my Rails 2.x
Let's say I have a large amount of files, say 20GB worth, all encrypted
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate

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.