Lesson 7, Step 5: undefined method `name'

I created a form using the fields of the destination object using form_for by following the example in the linked exercise, but I’m getting an error message:

undefined method `name’ for #<ActionView::Helpers::FormBuilder:0x000000034477a0>

In app/views/destinations/edit.html.erb it should have access to the destination object. If this is creating a form using the properties of that object, why is it talking about a method? I have to admit, this is going right over my head. I feel like I’m just following instructions without gaining much understanding of what’s actually happening in the code.

Here’s my code:

<%= form_for(@destination) do |d| %>
  <div class="field"> 
    <%= d.name :name %><br> 
    <%= d.image :image %><br>
    <%= d.description :description %>
<% end %>

What’s wrong with my code and what is it actually doing?

Thank you.

Hi Vince,

<%= d.name %>

will return a string containing d's name. So you don’t need to add the additional :name to it.
Fixing that will solve most of your problem, then you need to use the rails <%= img_tag %> to show the image, because d's image is stored in the database as a string containing the URL of the actual image. So <%= d.image %> will just render out something like:


Throwing an error :blush:
I’m not enough of a Rails expert yet to know what Rails thinks :name is supposed to be doing (and what it’s not), though, sorry :confused:

Unfortunately, removing :name didn’t help. I get the same error message. I thought that .name retrieved the string and the part with the colon before it gave the form field it’s HTML name and/or id properties. So, this is what I thought <%= d.name :name %> might look like in HTML:

<input type="text" name="name" id="name" value="d.name string value">

Of course, that’s just a guess. I think this web search result confirms my assumption, but I’m not certain I correctly understand what I’m reading.

It seems odd to me that this class doesn’t refer to any documentation for the ERB tags we’re using like form_for and image_tag.

Regarding image_tag, it doesn’t say to display the image and I haven’t seen a reference to image_tag in any of the previous lessons. When it said to display an image, I got a pass with <img src="<%= obj.image %>">. It only says to create a form from the fields of the destination object.

I tried it anyway, but it still didn’t get past the “Undefined method `name’…” error.

On an actual page, it’d be a good idea to display the image along with a browse button for uploading a new image to replace it. Something like this:

<img src="here.jpg" alt="here">
<input type="file" id="new_image">


… but I don’t have a clue how to handle that in Rails.

If I click on “Run” enough times, I get a “Get Code” option and this is what it uses:

<%= image_tag @destination.image %>

<%= form_for(@destination) do |f| %>
  <%= f.text_field :name %>
  <%= f.text_field :description %>
  <%= f.submit "Update", :class => "btn" %>
<% end %> 

So, it’s not like <%= something.each do |f| %> where the part after the dot refers to a property of the object I’m iterating over. It looks like when you use form_for the part after the dot tells Rails what kind of field to generate and the part after the colon is where we use the name of the object’s property. Looking closer at the linked exercise, that matches and I wasn’t paying close enough attention.

In the error message, the method it was referring to was a method on the form builder object, created by form_for and named between the vertical bars.

It also says something like that in the documentation I found, although reading that and understanding it are two different things (for me).

I sort of feel like an old blind man skiing up a hill with a lot of trees… I’m hitting a lot of obstacles and I don’t quite understand them, but I guess I’ll get where I’m going eventually. I’m definitely learning along the way :grin:

Thank you.

1 Like

I didn’t even notice your code was in a form_for, sorry about showing you how to do the wrong thing - I’ve been running low on sleep recently.

I think you could do that with something like this:

<%= image_tag @image %> <!-- @image is an image url-->

<%= form_for @something do |f| %>
  <%= f.file_field :picture %>
  <%= f.submit %>
<% end %>

See more on file upload stuff here

Haha, I like that description :slight_smile:
I’m sure that you’ll learn Rails well enough, though - most people don’t put as much effort into their learning as you are.

1 Like