PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] How to access attributes in a partial of a nested rails form?

 May 18, 2022     forms, nested-forms, partial, ruby-on-rails-3     No comments   

Issue

I want to use the boolean attribute is_white from my inner_object to switch between html code in the the partial _inner_object_form_fields. This is my attempt.

<%= form_for @outer_object do |f| %>
  <%= f.fields_for :inner_object do |builder| %>
    <%= render :partial => "inner_object_form_fields", :locals =>  { :f => builder } %>
  <% end %>
<% end %>

This is my attempt of the partial _inner_object_form_fields.

<% if f.is_white == true %>
  <%= f.label(:name, "White") %>
<% else %>
  <%= f.label(:name, "Black") %>
<% end %>

This is the migration file of InnerObjects.

class InnerObjects < ActiveRecord::Migration
  def self.up
    create_table :inner_objects do |t|
      t.string "name"
      t.boolean "is_white", :default => true
      t.timestamps
    end
  end
  def self.down
    drop_table :inner_objects
  end
end

I found a similar question but could not retrieve an answer for me. The question is: How can access the attribut is_white? My example does NOT work.


Solution

Try

<% if f.object.is_white == true %>

Seem to remember you could access the object this way (not 100% sure though ;)



Answered By - oliverbarnes
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing