change post/:id in rails to show post/:restname

121 Views Asked by At

I'm still new to Rails and I have a question about routes.

Right now, I have resources from a post table as normal.

post_path   GET             /posts/:id(.:format)    posts#show
            PATCH           /posts/:id(.:format)    posts#update
            PUT             /posts/:id(.:format)    posts#update
            DELETE          /posts/:id(.:format)    posts#destroy

Is there anyway when I hit the show page, instead of the address bar saying:

http://localhost:3000/posts/3 

can I have it say:

http://localhost:3000/posts/restaurantName  ? 

:restname is a column in Users with a restaurant name.

I'm building a web app for restaurant workers and I'd like it to have the address end with the restauranteur's restaurant name, specifically Post.user.restname.

Thanks for the help

1

There are 1 best solutions below

0
On BEST ANSWER

My reputation wasn't high enough so I couldn't like both your comments but if I could, I would.

Because of the way my tables are set up, the information I needed was under users and I was looking to change the address under my posts with user information. In the end, the closest I could get was to use parameterize.

In my post controller:

45   @post = Post.new(post_params)
46   if @post.save 
47     @post.update :postname => @post.user.restname
48     redirect_to @post
49   else

In my post model:

13   def to_param
14     "#{id} #{postname}".parameterize
15   end