Here's my problem:
I have a web app, in which users can create posts.
User and post are created simultaneously - I extract the user's email from the post to create his user entry. (No password/login/registration etc required)
In my routes.rb file, I have posts nested with users (see attached)
Now, here is my question:
Where should the posts#new creation form be? Currently I have it at /posts/new but this is clearly wrong, I am getting a routing error.
Grateful for any feedback.
routes.rb
Mysalary::Application.routes.draw do
resources :users do
resources :posts
end
resources :profiles
resources :pages
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
I would add posts on it own, so to have both you would have:
routes.rb
resources :users do
resources :posts
end
resources :posts
You have posts only as a nested resource, so you would find it at /users/:user_id/posts/new
If you want to reach it at /posts/new, just un-nest resources :posts. You can also leave it nested and repeat it outside the nesting, then it would be reachable both ways.
Remember to run rake routes in the console.
Related
I have a blog with root
root 'posts#index'
And works best with example.com/ to example.com/posts
But what I want is something like this:
example.com/blog/posts/1.
I've tried creating blog Controller and add
resources :blog do
resources :posts
end
But this is making my routes to blog/:id/posts/:id
If you don't have the relationship between the post and the blog as you mentioned, rails gives you the freedom to declare routes as our own.
so, to make the route example.com/posts/1 to, example.com/blog/posts/1, just add a custom route at the last.
get '/blog/posts/:id', to: :show, controller: 'posts'
what this does is over rides the previous route and make this route final.
Now type rake routes and it will give the last route for you as,
GET /blog/posts/:id(.:format) posts#show
Now you can access using,
example.com/blog/posts/1
Reference for rails routing
Just to expand upon #Sravan answer. If you have multiple routes that will start with /blog/ you might want to check Rails guide on routing.
You can add something along the lines of
scope '/blog' do
resources :posts
resources :users
resources :images
end
Which will create corresponding routes under /blog/.
namespace :blog do
resources :posts
resources :users
resources :images
end
And your controller with namespace will look like this: Blog::PostsController
I currently have a default devise set up and wanting to put the user id's within my logged in routes and leave the others alone such as Welcome,FAQ, About us etc.
Such as
www.example.com/user-id/offers
My Current Routes File
devise_for :users
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :offers do
member do
put :tourcomplete
end
end
resources :categories, only: :show
root 'welcome#index'
get '/balance', to: 'balance#show', as: 'balance'
patch '/balance', to: 'balance#paypal', as: 'balance_paypal'
patch '/withdraw', to: 'balance#withdraw', as: 'balance_withdraw'
I can't find any docs on this and my previous answer to a similar question was very vague and not helpful.
Thanks
You will need to use a nested routes:
resources :users do
resources :offers do
member do
put :tourcomplete
end
end
end
so now your routes will be /users/:id/offers, run rake routes to check your routes.
if you want to exclude users then you will have to specify the route yourself:
match ':user_id/offers' => 'offers#index'
I am trying to setup a rails blog at the "website.com/blog" url
I already have my models and controller setup to work to where going to
website.com/posts
Gives me all my posts and going to
website.com/posts/1/
Shows me that post, etc, etc. What I want to happen is that when I go to
website.com/blog/
I should see the posts index (and the original URL should no longer work). Similarly I want to go to
website.com/blog/posts/1/
To see that post and so on and so forth.
Right now this is my routes file:
Rails.application.routes.draw do
namespace :blog do
resources :posts do
resources :comments
end
end
get "/blog", to: "posts#index"
end
When I go to "/blog/" I get a Routing Error saying "uninitialized constant Blog". Do I need to create a blog model and controller and migrate to complete this? I'd rather not since it's really just running the posts requests from that new URL. Am I going about this the wrong way?
I ended up finding the answer to my own question here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
Using this seems to work just fine:
scope '/blog' do
resources :posts do
resources :comments
end
end
get "/blog", to: "posts#index"
The answer ended up being found here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
As usual the solution was incredibly simple and made me feel like an idiot for not knowing what to do immediately:
scope '/blog' do
resources :posts do
resources :comments
end
end
get "/blog", to: "posts#index"
I'm searching a reason why rake routes doesn't match the index path of my nested resource.
Here is my code:
namespace :api do
resources :photos do
resource :comments
end
end
Here is the result of the command: rake routes | grep comment
batch_action_admin_user_comments POST /admin/user_comments/batch_action(.:format) admin/user_comments#batch_action
admin_user_comments GET /admin/user_comments(.:format) admin/user_comments#index
POST /admin/user_comments(.:format) admin/user_comments#create
new_admin_user_comment GET /admin/user_comments/new(.:format) admin/user_comments#new
edit_admin_user_comment GET /admin/user_comments/:id/edit(.:format) admin/user_comments#edit
admin_user_comment GET /admin/user_comments/:id(.:format) admin/user_comments#show
PATCH /admin/user_comments/:id(.:format) admin/user_comments#update
PUT /admin/user_comments/:id(.:format) admin/user_comments#update
DELETE /admin/user_comments/:id(.:format) admin/user_comments#destroy
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
api_photo_comments POST /api/photos/:photo_id/comments(.:format) api/comments#create
new_api_photo_comments GET /api/photos/:photo_id/comments/new(.:format) api/comments#new
edit_api_photo_comments GET /api/photos/:photo_id/comments/edit(.:format) api/comments#edit
GET /api/photos/:photo_id/comments(.:format) api/comments#show
PATCH /api/photos/:photo_id/comments(.:format) api/comments#update
PUT /api/photos/:photo_id/comments(.:format) api/comments#update
DELETE /api/photos/:photo_id/comments(.:format) api/comments#destroy
I tried to add only: [:create, :index] to my comments resource but only the create route is visible.
According to the documentation about nested-resources I don't understand what's happening.
Thank you for your help.
It's because you are using a singular resource (resource :comments)
From the docs:
Sometimes, you have a resource that clients always look up without
referencing an ID. For example, you would like /profile to always show
the profile of the currently logged in user. In this case, you can use
a singular resource to map /profile (rather than /profile/:id) to the
show action
You'll need to use the standard resources method to get this working (resource omits the index action):
#config/routes.rb
namespace :api do
resources :photos do
resources :comments
end
end
My mistake. A "S" was missing on my resource.
namespace :api do
resources :photos do
resources :comments
end
end
Now it works.
I'm getting this routing error on my site. I've went through other related questions and haven't found a solution yet.
No route matches {:action=>"show", :controller=>"businesses", :user_id=>3}
I have three models: user (contains login details), business (contains info related to users: one user has_one business), and business_hours (one business has many business_hours).
Here are my routes:
devise_for :users
get "home/index"
root :to => "home#index"
resources :users do
resources :businesses do
resources :business_hours
end
end
Edit:
I get the error just trying to access the home page (localhost:5000). I'm currently signed in. I have run rake routes and the route seems to be there:
user_business GET /users/:user_id/businesses/:id(.:format) businesses#show
the routes which you have mentioned is expecting a forth argument which is the id of the businesses,try to pass the id of businesses it will work
user_business GET /users/:user_id/businesses/:id(.:format)
ideally you need to use this for index
user_businesses GET /users/:user_id/businesses(.:format)
your question is not so clear otherwise i could have given a precise answer hope this helps