All threads / Forum Series Part 2: Routes Discussion
Ask A Question


You’re not receiving notifications from this thread.

Forum Series Part 2: Routes Discussion

Chris, I don't have the view folders for forum_threads. Did you manually create those? Or did I miss a step?

I did actually. I ended up deciding to leave out a few pieces because they were ending up making the episodes really long without a lot of useful/interesting things.

Instead, I decided to put everything up on the Github repo so it you can still review those parts!

Hello! I have lots of questions :P I'm trying to nest 3 routes Model1Ctrl > Model2Ctrl > Model3Ctrl and wish to access a model3Ctrl each loop on Model1Ctrl show page. How should i do it? I'm losing it, please help :P

<3 Gorails!

What do you mean by each loop?

I'm sure i'm building something wrong, i'm making a Course > Sections > pdfs nesting, and i'm trying to get an @course.sections.pdfs.each to show all the pdfs on the course show page. Still figuring it out. :)

You can't quite do it that way. You can say a Course has_many :pdfs, through: :sections and then say @course.pdfs instead. That way you can handle that joins properly and display them all on the page. :)

Gonna try it right away. Thanks! I really appreciate it :)

It worked, but wasn't what i expected :P. I want to present a table, where pdf's are separated by the section title.
I get my sections correctly, but only the pdf's from the first section are showing in every section table, as the loop for pdfs is in an instance of a section. Am i trying to achieve something that isn't possible, or just doing it all wrong? :(

In that case, you want to do two loops. Something like this:

<% @course.sections.each do |section| %>
<div><%= section.title %></div>
<% section.pdfs.each do |pdf| %>
<%= link_to pdf.title, pdf %>
<% end %>
<% end %>

I love you!!!! <3 That was exactly what i wanted, i need to train more my eyes. Every time i tried, i forgot to pass the pdf variable. Thanks, made my day! :D

I got the pdf link, but the route is something weird [localhost:3000/pdfs/#<pdf:0x007f9bfdbe7b20>]
You've been of great help, i'm sorry to bother you.

Glad you got it working! :) You may need to change the link_to to go to a different url. My example assumed you weren't doing nested resources for PDFs. I'd guess you probably want to use the nested routes here for it.

Kohl Kohlbrenner ·

this is off topic, but I am struggling with a problem. How do I use form_for to create a new instance of a class with namespaced routes? I have :

resources :recipes do
resources :comments, only: [:new, :create], module :recipes
resource :like, module :recipes

form_for(@recipe, @comment) doesn't work, in addition to other variations. I am stumped.

You're really close. To create a form for a new comment on a recipe, you would do "form_for([@recipe, @comment])"

To create a new like on a recipe comment, you would do "<%= form_for([@recipe, @comment, @like]) do |f| %>"

Kohl Kohlbrenner ·


Thanks for the response. When I implement your suggestion, the follow shows in my logs.

FYI My comment form is inside the Recipes show page. Inside that action I have:
def show
@recipe = Recipe.find(params[:id])
@comment = //also tried

Inside Recipes::Comments Controller

def create
@comment = comment_params
@comment.user_id =
redirect_to recipes_path
redirect_to @recipe, alert: "Unable to save"

Started POST "/recipes/1/comments" for ::1 at 2015-03-07 22:44:10 -0500

Processing by Recipes::CommentsController#create as HTML

Parameters: {"utf8"=>"✓", "authenticity_token"=>"3y5pQzo2JK2AfhaY/OXLFUB4iHZIjykhnAN4X2i5DAn1wXnUJkCum8SSxZibZu3D9MfmvuS4Jn+VyWv7aBPsPg==", "comment"=>{"body"=>"help me"}, "commit"=>"Submit", "recipe_id"=>"1"}

Recipe Load ...SQL query....
User Load ...SQL query.....
Begin Transaction
Rollback Transaction

Looks like it's working correctly to me but validations caused it not to save. Double check all of those are passing.

Kohl Kohlbrenner ·

You're right! But, what did you see that made you identify the problem as a validation error? The rollback transaction? I set a min length on the comment, but the "too_short" message I wrote for it wasn't showing up. I can't believe I spent so much time on this and I can't thank you enough for your receptiveness!

Yeah the rollbacks only really happen when validations have failed to pass. That's something you'll pick up along the way and it helps point out what's wrong.

Best tip for debugging something like this is to make sure you've got the "byebug" gem installed and stick "byebug" into the controller before the save and then run the save manually from inside the byebug prompt. You'll get immediate output there letting you know if it worked or not and that can help save hours of debugging. :)


This is embarrassing but here is my problem. After clicking the button to reply with a post in a thread, instead of getting a url like
I get

and then the error says:
ActionController::RoutingError at /forum_threads/4/forum_posts
uninitialized constant ForumThreads

Any hint? Below the forum_posts_controller.rb file

Thanks for the great screencasts :)

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread

def create
@forum_post = forum_post_params
@forum_post.user = current_user
redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{}"), notice: "exito en postear"
redirect_to @forum_thread, alert: "no se pudo postear"
def set_forum_thread
@forum_thread = ForumThread.find(params[:forum_thread_id])
def forum_post_params

http://localhost:3000/forum_threads/4/forum_posts is the URL you want to POST to when creating a new forum post. This is what the form's action will point to so that part looks correct.

Your code inside the controller looks correct as well and should redirect back to the forum thread path.

"uninitialized constant ForumThreads" sounds like maybe your module on the controller specified might be with a different spelling? Was it singular :forum_thread in your routes? That would mean you need to do class ForumThread::ForumPostsController if so.

I'm getting an error "COULD NOT FIND USER WITH 'ID'=". I'm not using the Devise Gem but I have ":set_user" method defined in my application controller as "@user = User.find(params[:user_id])" instead of ":authenticate_user!". I have the nested routes just like the video. I don't understand what's wrong here. I appreciate any help.

Well i made a new method called "require_same_user" in my application_controller and defined it as
if !logged_in?
flash[:danger] = "You must be logged in to do that."
redirect_to root_path

That seemed to fix it.

Why touch the controller files instead 'rails g controller forum_threads' ?
which would save as some trouble with creating the view files also...

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 68,326+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.