Calling function from html.erb

82 views
Skip to first unread message

Alfredo Barrero

unread,
Mar 3, 2014, 2:57:19 PM3/3/14
to rubyonra...@googlegroups.com
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.

Alfredo.

Colin Law

unread,
Mar 4, 2014, 4:04:11 AM3/4/14
to rubyonra...@googlegroups.com
Your book should tell you how to setup data in a controller for access
from a view, using @variables.

Colin

Carlos Figueiredo

unread,
Mar 4, 2014, 10:03:18 AM3/4/14
to rubyonra...@googlegroups.com
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: http://guides.rubyonrails.org/routing.html

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)




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtcO8P1JBumJ6mP7yhPHS7cytHB9h3-h2WR0ykHV3ZKug%40mail.gmail.com.

Alfredo Barrero

unread,
Mar 16, 2014, 12:17:46 AM3/16/14
to rubyonra...@googlegroups.com
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. https://github.com/abarrero90/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.
Message has been deleted

Tommaso Visconti

unread,
Apr 10, 2014, 3:11:10 AM4/10/14
to rubyonra...@googlegroups.com
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 Barrero <abarr...@gmail.com>:
Any idea of the issues?.

Thanks a lot.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Alfredo Barrero

unread,
Apr 10, 2014, 3:19:58 AM4/10/14
to rubyonra...@googlegroups.com
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.


Colin Law

unread,
Apr 10, 2014, 3:28:48 AM4/10/14
to rubyonra...@googlegroups.com
On 10 April 2014 08:19, Alfredo Barrero <abarr...@gmail.com> wrote:
> 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),

That should be @photos.

Colin

Tommaso Visconti

unread,
Apr 10, 2014, 3:40:18 AM4/10/14
to rubyonra...@googlegroups.com
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 %>



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Alfredo Barrero

unread,
Apr 10, 2014, 3:48:22 AM4/10/14
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Apr 10, 2014, 4:02:49 AM4/10/14
to rubyonra...@googlegroups.com
On 10 April 2014 08:48, Alfredo Barrero <abarr...@gmail.com> 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
Reply all
Reply to author
Forward
0 new messages