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)
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
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…
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.