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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:45:21+00:00 2026-05-17T23:45:21+00:00

Hey guys, I’m having trouble understanding a Rails construct. I’m using Rails 3 but

  • 0

Hey guys, I’m having trouble understanding a Rails construct. I’m using Rails 3 but I doubt this is specific to this version.

I have a model, Goal that has_many :commits and naturally, a Commit model that belongs_to :goal. I created the proper migration so that commits_table.references :goal.

I am actually going through the Rails Getting Started guide, except in that article they use a Post and Comment respectively.

Now that I’ve described the situation I can express my confusion. On the Goal‘s show view, I have embedded a form to create a new Commit which is ‘attached’ to the currently viewed Goal. This works fine and all. However, I am having trouble understanding why we do this

<%= form_for([@goal, @goal.commits.build]) do |f| %>

Shouldn’t it be form_for(@commit)? I understand why we want the @goal, to provide some context since the commit is a nested resource. However, in the actual generated source, the form is appropriately named as commit, that is, the fields are named commit_blah. How did Rails know this? I understand that there’s this whole system of “trust and magic” and all, but I mean at least at the high level, what from this code hinted to Rails that I wanted a commit?

I looked at the documentation for form_for and it seems like one of the parameters could be the action to take for the form. I imagine that in this case, that’s what the @goal.commits.build parameter is for? To designate the action to take? Is this how Rails deduces that I want a commit? Would this also explain why this form is handled by the Commit controller even though this code is in the Goal’s view?

Also, why are these parameters passed as an array ([])? In Ruby, will the method still just take it as two separate parameters, or is there a reason why this was passed this way?

Finally, rails generate automatically gave me some error showing code in my other _form.html.erb partials:

<% if @commit.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@commit.errors.count, "error") %> prohibited this commit from being saved:</h2>

    <ul>
    <% @commit.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

If I want to add this to this embedded form, how would I do so? I guess my question is, what would I use in place of @commit?

Thanks. I’m just trying to get my head around these new concepts.

  • 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-17T23:45:21+00:00Added an answer on May 17, 2026 at 11:45 pm

    If you go back to the documentation and click ‘show source’, you’ll see

    def form_for(record_or_name_or_array, *args, &proc)
      ...
      case record_or_name_or_array
      when String, Symbol
        ...
      when Array
        object = record_or_name_or_array.last
        object_name = options[:as] || ActiveModel::Naming.singular(object)
        apply_form_for_options!(record_or_name_or_array, options)
        args.unshift object
      else
        ...
      end
      ...
      output << fields_for(object_name, *(args << options), &proc)
      ...
    

    For form_for, the first parameter can be a record, name or an array. In your case, you pass it an array. The code then determines the ‘object’ as the last member of that array, which is your @goal.commits.build object. The object name is determined from the ActiveModel::Naming.singular method.

    console > ActiveModel::Naming.singular(@goal.commits.build)
    => "commit"
    

    Then it generated the appropriate form fields using fields_for and ‘commit’.

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

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
Hey guys (and gals) I'm having a problem using Assembly GetExportedTypes() in .NET 4.0.
Hey guys am having a bit of problems with my jQuery, I have all
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;
Hey guys here is my question. im using JCombobox. if a student have CB
Hey guys, I'm using smarty and php I am trying to make this function
hey guys i have a restful xml service where client passes current version of
Hey guys, I'm having a bit of trouble with my ASP:RadioButtonList, searched Google and
hey guys i know this may sound stupid, but i am stuck with this
Hey guys plz help I am trying to convert this html to jade using

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.