Newbie question - route - show posts from scaffold on blog controller page *solved*

I just started learning rails and mediumsmart so please help me out. (I did the rails guide get started tutorial). rails 5.2

I have a posts scaffold and a blog controller (and friendly_id). I want the post to show on blog/title-of-post but I only get posts/title-of-post. I want the new, edit and destroy functionality to stay on the posts page which is behind basic authenticate.

here is a paste of my routes.rb, rake routes output, posts and blog controller.rb: pastebin

I found this on stackoverflow which is the exact same problem but the solution

resources :posts, path: 'blog'

moves the crud over to blog as well: https://stackoverflow.com/questions/30350279/changing-the-route-from-posts-article-to-blog-article-in-rails

thank you :slight_smile:

Add an additional route to the same action. So keep your resources :posts but add:

get 'blog/:id', to: 'posts#show'

Now your blog posts are available at both blog/title-of-post and posts/title-of-post. If you want to remove the later you can exclude the show action from the resource with:

resources :posts, except: :show

If you do that you will need to update your CRUD stuff to point to that custom route since it’s now the only one to the show action. Probably make a named route to help with that.

The routing guide can give you a lot more info on all this. See:

I have get 'blog/:id', to: 'posts#show' in the routes.rb and if I type blog/title-of-post in the browser url field - the post shows. BUT - on the blog page where the index of posts is shown - when I click on the title of a post preview it routes to posts/title-of-post url …

is the solution to what I want here in the rails routing from the outside in? [1.2 Generating Paths and URLs from Code](https://1.2 Generating Paths and URLs from Code)

if I add:

get '/posts/:id', to: 'blog#show', as: 'post'

it still routes to `posts/title-of-post` from the blog index page ...

ok, now it works as in not having the posts/title-of-post url - gives routing error
 - what I need I guess is the proper link in the blog/index.html.erb - at the moment it is this:

<%= link_to post.title, post %>

Yea the index still directing to your standard show route is what I mean when I said:

If you do that you will need to update your CRUD stuff to point to that custom route

If you give it a named route it should be more like:

get 'blog/:id', to: 'posts#show`, as: :blog

Then in your index instead of having:

<%= link_to post.title, post %>

You want:

<%= link_to post.title, blog_path(post) %>

I named it blog instead of post as your resources will generate other routes that will be named with post and we don’t want to conflict with that.

Personally I wouldn’t bother with all that. I think it’s ok that your CRUD directs to posts/:id while the public pages you want to expose are blog/:id. I.E. having two routes to the same page is not that big of a deal. But if you wanted to remove it above would be how I would do it.

I get : Invalid route name, already in use: ‘blog’ You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming.

I have now:

get 'blog', to: 'blog#index' 

and

  get 'blog/:id', to: 'posts#show', as: :blog

in the routes file - what am I missing? thank you so much for taking the time, if you ever need a quick photoshop or vector logo work, let me give back

Yea if your adding that index path it will name it blog I think because the path is just the literal string blog. Also I’m assuming your controller is named PostsController so the first route should be posts#index not blog#index.

Try instead:

get 'blog', to: 'posts#index'
get 'blog/:id', to: 'posts#show', as: :blog_post

Then when you want to link to the specific blog post you can do:

<%= link_to post.title, blog_post_path(post) %>

If you want to link to your index:

<%= link_to 'All posts', blog_path %>

now it shows the posts page with all crud links under site.com/blog - same as it would with:

resources :posts, path: 'blog'

ignoring my views/blog/index.html.erb and yes I have a posts.controller.rb ( the post is a generated scaffold - the blog is a generated controller with index and show) its in the paste I posted: routes.rb, rake routes, post and blog controller.rb

I didn’t realize you have both a blog controller and a posts controller. They seem to overlap in purpose. If you are doing that then you probably want your routes to be:

get 'blog', to: 'blog#index'
get 'blog/:id', to: 'blog#show', as: :blog_post
resources :posts

In your blog template files you want to link as:

<%= link_to post.title, blog_post_path(post) %>
<%= link_to 'All posts', blog_path %>

In the posts template files you want to link using standard routes:

<%= link_to post.title, post %>
<%= link_to 'All posts', posts_path %>
1 Like

now it shows the blog index I made. great. but the post link just changes the current url to /blog.initial-post

Screenshot 2021-08-18 at 18.37.13

this is the blog/index.html.erb: Screenshot 2021-08-18 at 18.40.17

If you are using the routes I gave blog_path would link to the list of all posts while blog_post_path would link to a specific post. So I think you want:

<%= link_to post.title, blog_post_path(post) %>
1 Like

YES! - my typo. You are the man. it works and creates the blog/initial-post url - please - it says NoMethodError in Blog#show and undefined method title’ for nil:NilClass`

I have this in the blog/show.html.erb: Screenshot 2021-08-18 at 18.51.39

Screenshot 2021-08-18 at 18.57.46

I found it - error was in the blog controller show method - posts instead of post

Thank you Eric