Username in urls for nested routes - ruby-on-rails

I'm a new rails developer making an application where I have a user profile like: localhost:3000/posts routing to localhost:3000/Charles where Charles is the username.
So the code I'm using to do this looks like:
routes.rb
match "/:username" => "posts#index"
and then in my controller:
#user = User.find_by_username params[:username]
#search = Post.search do |query|
query.fulltext params[:search]
query.with(:user, #user.id)
end
this works great just for the post index, but I'd like to have posts/18 (for example) be routed to /username/posts/18.
So basically, I'm asking if there's a way to do something like this:
match "/:username" => "posts#index" do
resources :posts
end
Thanks for all help!

This
scope '/:username' do
resources :posts
end
produces what you want:
posts GET /:username/posts(.:format) posts#index
POST /:username/posts(.:format) posts#create
new_post GET /:username/posts/new(.:format) posts#new
edit_post GET /:username/posts/:id/edit(.:format) posts#edit
post GET /:username/posts/:id(.:format) posts#show
PATCH /:username/posts/:id(.:format) posts#update
PUT /:username/posts/:id(.:format) posts#update
DELETE /:username/posts/:id(.:format) posts#destroy

Try
scope ':username' do
resources :posts
end
bundle exec rake routes
posts GET /:username/posts(.:format) posts#index
POST /:username/posts(.:format) posts#create
new_post GET /:username/posts/new(.:format) posts#new
edit_post GET /:username/posts/:id/edit(.:format) posts#edit
post GET /:username/posts/:id(.:format) posts#show
PUT /:username/posts/:id(.:format) posts#update
DELETE /:username/posts/:id(.:format) posts#destroy

match "/:username/posts/:id" => "posts#show"
But that won't really help when you need to edits and such. So, try:
scope "/:username" do
resources :posts
end
That failing:
scope "/:username" do
get '' => "posts#index"
get 'posts/:id' => "posts#show"
end
Hopefully the first will work. Haven't tried it myself so forgive me, but I'm pretty sure that, in general, scope is what you want.

Related

Ruby on Rails get route

I'm totally new on Ruby, I need to create route get which will write dynamically text = "About Us"
about.html.erb code:
<h1><%= #title %></h1>
index.html.erb code:
<h1>Index</h1>
Routes code:
Rails.application.routes.draw do
root 'posts#index'
get 'about' => 'pages#about'
end
In the folder controllers I have defined pages_controller.rb and posts_controller.rb,pages with simple function about and posts with function index
In the folder views/pages I have about.html.erb and views/posts index.html.erb
The problem is when I run it, it writes message only from index and not from about.html.erb
Rake routes message:
$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / posts#index
about GET /about(.:format) pages#about
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
Since you want to call the about action and you named it about in your routes.rb file, you have to point your browser to:
localhost:3000/about
You also have to set your #title variable to something in the pages#about action.

How do you add prefixes to routes? Rails (4.2.5) routing confusion.

I have two controllers offers and posts.
In routes.rb I've got the following...
resources :offers
get "/posts" => "posts#index"
post "/posts" => "posts#create"
get "/posts/new" => "posts#new"
get "/posts/:id/edit" => "posts#edit"
get "/posts/:id" => "posts#show"
put "/posts/:id" => "posts#update"
patch "/posts/:id" => "posts#update"
delete "/posts/:id" => "posts#destroy"
It was my understanding that these two ways of doing the routing are identical in their operation. Or put another way resources :offers is a just shortcut for writing out each route.
My problem however is when I do a rake routes I get...
Prefix Verb URI Pattern Controller#Action
offers GET /offers(.:format) offers#index
POST /offers(.:format) offers#create
new_offer GET /offers/new(.:format) offers#new
edit_offer GET /offers/:id/edit(.:format) offers#edit
offer GET /offers/:id(.:format) offers#show
PATCH /offers/:id(.:format) offers#update
PUT /offers/:id(.:format) offers#update
DELETE /offers/:id(.:format) offers#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
posts_new GET /posts/new(.:format) posts#new
GET /posts/:id/edit(.:format) posts#edit
GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
PATCH /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
The 'shorthand' offers routes have a four prefixes assigned to them, whereas the longhand posts routes have only two.
So my questions are:
Why don't all the routes get given prefixes when written out longhand?
Is there any way of assigning a prefix to a route when writing them out longhand?
You can assign a prefix to a route when writing them out longhand by doing:
root 'pages#index', as: :home, where :home is your prefix.
Source:
Generating Paths and URLs from Code

How do I create a new route path to my newly generated action?

I have created a new action in Posts controller, However, it seems that I can not access it.
the action name is 'kill' , so when I type in my browser, localhost:3000/posts/kill
I get the following error : Couldn't find Pad with id=kill
Extracted source (around line #18
def show
#post_selected = Post.find(params[:id])
#posts = Post.all
end
In my routes.rb
resources :posts
match ':controller(:action(/:id))', :via => [:get, :post]
match "/pads/modifier" => "pads#modifier", :via => [:get]
and when I type rake routes , this is what i get
Prefix Verb URI Pattern Controller#Ac
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#welcome
GET|POST /:controller(:action(/:id))(.:format) :controller#:
posts_modifier GET /posts/modifier(.:format) posts#modifier
How do I generate a path to get my newly created action 'kill'?
=========================================================
Update #1 : Solved,
In addition to the answer below, I could make it happen with the following code
resources :posts, controller: 'posts' do
get 'posts/:action', to: 'posts#:action'
end
===========================================================
get 'posts/kill', to: 'posts#kill'

"undefined method `post_comments_path' for #<#<Class:0x4e6ec28>:0x4e6e3d0>" in rails 4.0.4

undefined method `post_comments_path' for #<#<Class:0x4e6ec28>:0x4e6e3d0>
I am getting this error while I am going to create a comment associated with the posts.
<%= form_for([#post, #post.comments.build]) do |f| %>
Above is the section of the form where i am getting the error..
My code in comments controller:
def create
#post = Post.find (params[:post_id])
#comment = #post.comments.create(params[:comments].permit(:commenter, :body))
redirect_to post_path(#post)
end
Please point out where I am wrong. I am a newbie to ruby on rails.
my routes.rb:
Blog::Application.routes.draw do
resources :posts
resources :comments
root to:"welcome#index"
get "welcome/index"
my rake routes:
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
root GET / welcome#index
welcome_index GET /welcome/index(.:format) welcome#index
My edited routes.rb ::
Blog::Application.routes.draw do
resources :post do
resources :comments
end
root to:"welcome#index"
get "welcome/index"
end
You try to use nested resources, but you didn't define it in routes.rb. You should have:
resources :posts do
resources :comments
end
And this should work.

Routing error in tutorial app converted from rails 2.x to 3.x

I'am working on sixrevision.com tutorial “How to Create a Blog from Scratch Using Ruby on Rails” . I try to convert It from rails 2.x to 3.x. When I run localhost:3000 I get this:
Routing Error
uninitialized constant PostController
Try running rake routes for more information on available routes.
Rake Routes shows me this:
posts GET /posts(.:format) posts#index {:has_many=>:comments}
POST /posts(.:format) posts#create {:has_many=>:comments}
new_post GET /posts/new(.:format) posts#new {:has_many=>:comments}
edit_post GET /posts/:id/edit(.:format) posts#edit {:has_many=>:comments}
post GET /posts/:id(.:format) posts#show {:has_many=>:comments}
PUT /posts/:id(.:format) posts#update {:has_many=>:comments}
DELETE /posts/:id(.:format) posts#destroy {:has_many=>:comments}
/:controller/:action/:id(.:format) :controller#:action
/:controller/:action/:id.:format :controller#:action
root / post#index
My routes.rb file:
Myblog::Application.routes.draw do
resources :posts, :has_many => :comments
match ':controller/:action/:id'
match ':controller/:action/:id.:format'
root :to => "post#index"
end
Anybody got any idea? Thanks for intrest & help!
In routes.rb file:
resources :posts do
resources :comments
end
In your routes.rb you should have:
root :to => 'posts#index'
because you do not have (probably) PostController but PostsController

Resources