Trying to Link from a User Type Directory Page to a User Type Gallery Page

I have two user types by 'identification_id', let's call them Red and Blue. Each user type has listings which all show on the home page. I created a shop page for each user type that shows all of their specific listings, linking to this shop page from the show page for each of their listings. I also created a gallery page for each user type that shows all static pictures that specific user type uploaded. I link to that user type gallery page also from the show page for their listings. I then created a global directory page by user type and have 2 links in my main navigation Red and Blue, so I can click Red directory and view a list of all Red users types with identification_id: "red". Same set up for Blue. Now I want to link each user in the Red Directory directly to their gallery page. In my directory_controller I have "def Red @users = User.where(identification_id: "1").order("created_at DESC")". I added to the view page "<%= link_to "View Gallery", gallery_path(@user.user.id) %>", but getting an error: "NoMethodError in Listings#red" with "undefined method `user' for nil:NilClass". When I add to the controller: "@user = User.find(params[:id])" I get the error: "Couldn't find User with 'id'=". I always get stuck at this point, isn't the @user defining the id which then links to the "identification_id" for this user and so the link should work to the gallery_path? I'm guessing I can't have 2 methods for Red for @user/@users? How do I define Red so that I get their "identificaton_id" to create the directory list and then also connect to the gallery page? Hoping this makes sense.

Posted over 4 years ago by Amy Peterson
Posted over 4 years ago by Alex Yang

I think you're dealing with a few concepts here:

1) You'll only be able to use variables that you've defined in your controller-action. In this case, you've defined @users so you'll be able to use that in your view page, but you won't be able to use a new variable like @user. To use it, you have to define it in your controller-action. And you can define both @user and @users in the same controller-action.

2) To create a route for a link, the format is to use {PATHNAME}_path({VARIABLE_1}, {VARIABLE_2}, ...), where {PATHNAME} is the name that shows up when you run 'rake routes' and {VARIABLE_X} is a parameter that's required in the URL format. For example, if your URL format is www.WEBSITE.com/users/:id, then 'id' will be a variable you need to include. So in this case, check your gallery path - does it require any variables? It probably does, but your syntax may be off. Instead of '@user.user.id', I'd guess it should be 'user.id' (assuming 'user' is the name you used in your loop).

Hope this helps!

1
Posted over 4 years ago by Amy Peterson

Excellent, thank you Alex! Got it working : )

1