Calling function from html.erb

Good night everyone,

My name is Alfredo Barrero and I’m getting started with RoR. I have been learning with “Agile Web Development”.

Now I’m trying to do my own web application. I have a problem and I don’t know what I’m doing wrong, could you please give me a hand?.

This is what I’m trying to do:

<%= button_to ‘Add Photo’ , home_add_path(user_id: user) %>

I have this line on “routes.rb” => get ‘/home/add’ => ‘photos#add’, but the browser gives me this error

undefined local variable or method `user' for #<#<Class:0x007fe075485dc0>:0x007fe0738e8d38>

The question is, how can I call a method from a html.erb?.

Thank you so much, best regards.


Your book should tell you how to setup data in a controller for access
from a view, using @variables.


Put your code on github. It could be a good oportunity to you train git, and for us, is an easier way to know what the context of your problem…

First, I recommend to read that:

Second… try plan better your routes. It’s not related with your currently problem at all… but do you agree that it’s a bit odd to add a home for a photo? Does it not look natural to add a photo to an user?

so… looks more natural a route like :user/photo/add translated to user_photo_add(@user)

Good morning everyone.

Sorry for beign late, I have been traveling. I’m in Singapore actually.

I have been working on Git these past days, and here is the code on github.

I have two questions already:

  1. Could you check if this line is OK?. “users/user.html.erb” ==> <%= button_to ‘Add Photo’ , home_add_path(user_id: @user) %>
  2. I just add the gem “paperclip”. I want this gem to upload all the photos that the user will have on his page on my server.

Thank you so much.


Hi Alfredo,

the error is that the user variable doesn’t exist, maybe you mean @user?

You can try without the user variable and it should work:

<%= button_to 'Add Photo' , home_add_path(user_id: 1) %>

Ok yeah get it.

Another issue that I have had is the following:

In the users/show.html.erb I’m using the following lines:

<% @photos.each do |product| %>

                  <%= @photo.user_id %>

              <% end %>

In the user controller I have declare this : photos = Photo.order(:title), but the browser gives me this error:

undefined method `each' for nil:NilClass in the following line ->  <% @photos.each do |product| %>

Could you please tell me what I'm doing wrong?.

Thank you and best regards.

That should be @photos.


I suggest you to study a little about ruby before starting with rails. This also is a “problem” about variable scope.

To use a variable in the view, it must be an instance variable, in your case:

@photos = Photo.order(:title)

and in the view you have 2 errors: |products| should be |photo| and inside the loop you must use the photo variable (which have a different value every cycle of the loop).

<% @photos.each do |photo| %>
<%= photo.user_id %>
<% end %>

Yeah sorry when I copy the text I didn’t select the @. But with that the issue stil there…

If I use

<% @photo.each do |photo| %>
<%= photo.user_id %>
<% end %>

Should work? It should iterate arround all the elements that Photos has?.

Thanks & best regards.

Yeah sorry when I copy the text I didn't select the @. But with that the
issue stil there...

You should *always* copy/paste code when asking questions, otherwise
it just causes others to waste their time.

If I use

  <% @photo.each do |photo| %>

That should be @photos not @photo, or are you retyping rather than
copy/paste again?

If you have used @photos in the view then first check development.log
where you should see the sql query being performed to pick up the
photos. If that looks ok then you can put some diagnostic code in the
controller after setting up @photos to check whether it is setup ok.
If you use puts in the controller it will appear in the server
terminal window.