Calling function from html.erb

Good night everyone,

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

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.

Alfredo.

On 3 March 2014 19:57, Alfredo B. [email protected] wrote:

<%= 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?.

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

Colin

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.GitHub - abarrero90/TravelTime: TravelTime

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.

Alfredo.

El martes, 4 de marzo de 2014 23:03:18 UTC+8, Carlos Figueiredo
escribió:

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) %>

About how to call a method from a view (which is a different question),
just call it:

<% method_name %>

The method must be in the right scope. As an example you can call
methods
defined in the helper files.
If you define instance variables (variables starting with @) in the
controller, you can use them from the relative view (and call their
public
methods):

class UsersController < ApplicationController
def index
@user = User.first
end
end

app/views/users/index.html.erb

User name: <%= @user.full_name %>

ok?

2014-04-10 6:38 GMT+02:00 Alfredo B. [email protected]:

Any idea of the issues?.

Thanks a lot.

El martes, 4 de marzo de 2014 03:57:19 UTC+8, Alfredo B. escribió:

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.

On 10 April 2014 08:19, Alfredo B. [email protected] wrote:

              <% end %>

In the user controller I have declare this : photos = Photo.order(:title),

That should be @photos.

Colin

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.

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 %>

2014-04-10 9:19 GMT+02:00 Alfredo B. [email protected]:

On 10 April 2014 08:48, Alfredo B. [email protected] wrote:

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.

Colin