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"
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 wanted to do like http://domain:3000/users/id/edit/details. I've already create a new action under app>views>users>details.html.erb. Also in UsersController i put:
def details
....
end
when I run rake routes, I got missing Helper Path/Url on the left side for GET, /users/:id/edit/details(.:format), Users#Details.
Also in routes.rb, I've:
resources :users
I try to insert:
match "users/:id/edit/details", to: 'users#details', via: 'get'
but stil didnt solve my problem. Please help me!
In routes, you'd want to do this:
resources :users do
get :details
end
In this, you'd have the advance form. The url for this will be
/users/:id/details/
Check rake routes to see what's in there.
You may need another update method (the form post action) to handle the advanced form
What you are looking for is called nested resources
resources :users do
resources :details
end
Take a look at http://guides.rubyonrails.org/routing.html#nested-resources for more info
I am learning rails and routing has me wanting to jump off the roof.
I am confused on how to go about routing my activation at this point.
I have the following currently in my user routing:
resources :users, only: [:new,:create,:show]. Now I want a route to Users#activate like this www.app.com/users/activate/:a_long_token. Now I know I can just simply do a match '/activate/:auth_token', to: 'users#activate but I am not sure whether this is convention. I was reading this guide on user authentication but it seems its routing is rails 2. Can I do something to add the route mentioned above by simply adding something to the resource itself. By that I mean doing something like (I know this won't work)
resource :users do
member do
get :activate
end
end
rails3 guide
http://guides.rubyonrails.org/
http://guides.rubyonrails.org/routing.html
resources :users do
collection do
get "activate/:a_long_token" => "users#activate", as: :activate
end
end
rake routes outputs this
activate_users GET /users/activate/:a_long_token(.:format) users#activate
longtime reader & first time poster, so I'd appreciate it if you went easy on me.
Recently started teaching myself RoR, and have been hacking away at a personal project/website to get the hang of things. Here's my problem:
I'm using the Simple Navigation gem to generate links. Inside navigation.rb I'm trying to call:
primary.item :home, 'Home', home_path
...where home is a view and controller that displays my front page:
home > index.html.erb (just contains a bunch of standard HTML, but let me know if it'd be useful to include)
and controllers > home_controller.rb:
class HomeController < ApplicationController
def index
#posts = Post.all
end
end
I'm getting this error, though:
Routing Error
No route matches {:action=>"show", :controller=>"home"}
Try running rake routes for more information on available routes.
... so I run rake routes, and can definitely see "home#show" in there.
My routes.rb file, as well, has this in it:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index"
resources :posts
resources :home
So I'm a little baffled, and I'm sure it's because of my inexperience or general inability to understand what I'm doing, but I'd really appreciate some help as it's more or less a road block that I haven't been able to overcome.
Appreciate it!
Jay
It is because of the resources; if you are not using the resources remove resources :home
This could be your routes:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index", :as => "home"
resources :posts
See how I removed the resources :home. In home/index the :as represents an alias, so you can use the alias as a method, adding "path" at the end of the name.
Check this guide about routes and resources: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default
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.