Ask A Question

Notifications

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?

Reply

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! https://github.com/excid3/g...

Reply

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!

Reply

What do you mean by each loop?

Reply
Kohl Kohlbrenner 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
end

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

Reply

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

Reply

Hello!

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
http://localhost:3000/forum_threads/4#forum_post_16
I get
http://localhost:3000/forum_threads/4/forum_posts

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_thread.forum_posts.new forum_post_params
@forum_post.user = current_user
if @forum_post.save
redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id.to_s}"), notice: "exito en postear"
else
redirect_to @forum_thread, alert: "no se pudo postear"
end
end
private
def set_forum_thread
@forum_thread = ForumThread.find(params[:forum_thread_id])
end
def forum_post_params
params.require(:forum_post).permit(:body)
end
end

Reply

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.

Reply

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.

Reply

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
end

That seemed to fix it.

Reply

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

Reply
Join the discussion
Create an account Log in

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

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

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