i was wondering how to pass an object to a partial, and then again to another partial?
im following the michael hartl book and this was an optional exercise but ive been super stuck.
if i have a..
<%= form_for(@user) do |f| %>
<%= render 'fields', object: f.object %>
<% end %>
from the book, it said to use a hash with the value equal to the object and key equal to the name of the variable we want in the partial. so that means in the _fields.html.erb, the key ‘object’ should exist and it should have the value of f.object
<%= render 'shared/error_messages', object: ?????? %>
<%= object.label :name %>
<%= object.text_field :name %>
<%= object.label :email %>
<%= object.text_field :email %>
but when i try retrieving the key, i get an error on the line
<%= object.label :name %>
it says
undefined method `label' for #<User:0x007f86000110c0>
why does it throw that? i thought i could reference the key as ‘object’.
also, ive been stuck on what to replace the ?????? with. to replicate the error above, i took out the ?????? but it will throw another error from the next partial saying ‘object’ is undefined. i cant use something like f.object because f no longer exists. how can i get the value to pass onto the next partial?
thanks a lot = )
Well, you’re passing in
object: f.object, andf.objectis aUser. So then you try to callobject.label, butUserdoesn’t have alabelmethod. If you pass inobject: f.object, f: fthen in your partial you can callf.label.