For the guide that shows you how to set up a blog, I've gotten up to 5.7.
For this section it tells you how to show the article, but after following the steps I only get an error when attempting to run rails server.
It exactly says it is a syntax error in a routes line.
The line is "article get /articles/:id(.:format) articles#show" and the error is pointing to :id.
I'm not really sure what to do, I've looked at the routes section of the guide and I thought it was a valid line.
routes.rb contains
Rails.application.routes.draw do
get 'welcome/index'
resources :articles
root 'welcome#index'
article get /articles/:id(.:format) articles#show
end
The exact message I receive when I use the command rails server is:
blog/config/routes.rb:5: syntax error, unexpected ':', expecting keyword_end (SyntaxError) article get /articles/:id(.:format) article#show
The route you're trying to make is bad formatted, I could say you literally pasted the output from the rails routes command.
You need to specify firstly the HTTP verb, GET in this case, then the url which this will respond with, and then the controller and action.
From:
article get /articles/:id(.:format) articles#show
Try with:
get '/articles/:id', to: 'articles#show'
Based on what you've put in your description, it looks like you're at the very least missing a hash rocket (=>) in one of your routes. However, as you've added the resources :articles line, I think the line that's produced this error could probably be removed altogether as the resources keyword will generate common routes like these automatically (please see the Rails API for more details).
Your routes.rb file should probably just look like this:
Rails.application.routes.draw do
get 'welcome/index'
resources :articles
root 'welcome#index'
end
Alternatively, the route you're having trouble with could be rewritten as:
get "/articles/:id" => "articles#show"
or in the way described in the guide:
get '/articles/:id', to: 'articles#show'
If you remove this line:
article get /articles/:id(.:format) articles#show
it should get ride of the error.
This line is not formatted correctly and it's trying to accomplish what is already being accomplished with the resources :articles line.
Related
I am getting the following error only on occasion when I attempt to load up my Rails app in localhost.
Invalid route name, already in use: 'root' 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. For the latter, you can restrict the routes created with resources as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
For some reason, it only happens every now and again, and generally goes away after I refresh the page once. The file it is calling into question has this as the code (line causing the error indicated):
Rails.application.routes.draw do
get 'welcome/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
Rails.application.routes do
resources :articles
root 'welcome#index' #-->This is the line that causes the error
end
resources :articles do
resources :comments
end
end
I am really new to Rails, and I'm not sure what is causing the problem or if would even be a problem if I were to actually host this beginner project on the web. Also, if it helps, I am running Ruby 2.2.2 and Rails 4.2.1.
Thank you in advance for the help!
You have (Rails.application.routes) nested inside (Rails.application.routes.draw). Run rake routes and you will see that you have resources for articles twice. Furthermore, you have root 'welcome#index' nested inside and that is why you are getting the error. Your routes should look like this
Rails.application.routes.draw do
get 'welcome/index' => 'welcome#index'
root 'welcome#index'
resources :articles do
resources :comments
end
end
Note that the route of your application meaning(/) and (/welcome/index) both point to the welcome controller index action. You don't really need get 'welcome/index' => 'welcome#index' and you can delete that line and use root when ever you need the index action from the welcome controller.
Try using the "Full" syntax.
root :to=> "some#action"
You have two routes pointing to the same page index, get rid of your get route
get 'welcome/index'
Also your root route is nested that should be moved outside since the root route is the root of your entire app and not a specific resource
I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
What does this do, and where should I put it? I tried putting it in the "create" method that follows this form:
http://dixonc3-72812.use1.nitrousbox.com/posts/new
I also tried putting it in the "view" because I figured it was accepting the "POST" method. How do I proceed? New to Ruby on Rails and trying to figure out the kinks.
I'm trying to follow this guide:
http://guides.rubyonrails.org/getting_started.html#hello-rails-bang
and in section 5.7 it says to add this line.
post GET /posts/:id(.:format) posts#show
Section 5.7 doesn't tell you that. What it tells you is the following is output of rake routes for the show action:
post GET /posts/:id(.:format) posts#show
The output is presented in a tabular format where the four columns are (from left to right): Prefix, Verb, URI Pattern, Controller#Action.
In order to get this, you need to declare posts as a resource in config/routes.rb as:
resources :posts, only: [:show]
Now if you run rake routes in your terminal, you will see the line included from the guide.
The above line in config/routes.rb defines a route for the show action. You can remove only: [ :show ] option to have routes defined for all standard RESTful actions, i.e. in your config/routes.rb:
resources :posts
This is the routes for show action of posts controller, for create action you find something like
posts#create
Problem which my terminal shows when I try rails server!
/home/<user_name>/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load': /home/salmanalam/rails_projects/blog/config/routes.rb:5: syntax error, unexpected ':', expecting keyword_end (SyntaxError)
post GET /posts/:id(.:format) posts#show
This is my content of config/routes.rb
Blog::Application.routes.draw do
resources :posts
get "welcome/index"
root 'welcome#index'
post GET '/posts/:id(.:format)' 'posts#show'
You have some extraneous (non)code at the conclusion of your routes. The entirety of your routes.rb should read something like this, instead:
# config/routes.rb
Blog::Application.routes.draw do
resources :posts
get "welcome/index"
root "welcome#index"
end
First, you're missing the end declaration to your block. Then, the piece of your code that reads post GET '/posts/:id(.:format)' 'posts#show' is not actually a route, but part of the output if you ran rake routes from the command line. It says that there's a show route for the Post resource.
I'm not sure how it got in your code, but removing it should resolve the issue you're having.
can you please try this
namespace 'posts', :defaults => {:format => 'eg.json'} do
match '/posts/event/:id', :to=> "post#show", :method => :get
end
root :to=>"welcome#index"
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
I was reading Simply Rails by Patrick Lenz... maybe I missed something, it seems that whenever we put
map.resources :stories
in routes.rb
then immediately, the controller will have special convention and now Story is a RESTful resource? Maybe the author used the word resource but didn't mention that it is RESTful but they are the same thing?
Having that in routes means that you automatically get some standard routes that help you build a restful application. For example:
new_story GET /story/new(.:format) {:action=>"new", :controller=>"stories"}
edit_story GET /story/edit(.:format) {:action=>"edit", :controller=>"stories"}
story GET /story(.:format) {:action=>"show", :controller=>"stories"}
PUT /story(.:format) {:action=>"update", :controller=>"stories"}
DELETE /story(.:format) {:action=>"destroy", :controller=>"stories"}
POST /story(.:format) {:action=>"create", :controller=>"stories"}
Just having this one line in your routes file, gives you all these paths to use. You just have to make sure you provide the right functionality in new, edit, show, update, destroy and create actions of your stories controller and you will have a restful design.
In order to see what is available route-wise, you can go to your application folder and give the command:
rake routes
This is going to output all the paths available to you, based on what you have entered in your routes file.
BUT!!! If you have other actions in your controller they will NOT be found unless you introduce additional routes ABOVE that .resources line!
So if you have an action called turn_page in the stories controller you need to include a map.connect line before the map.resources line - as in this snippet:
map.connect 'stories/turn_page', :controller => 'stories', :action => 'turn_page'
map.resources :stories
Hope that helps someone! I got stuck for hours working on this one as all the examples are EITHER "regular" routes OR the REST set defined via the .resources statement!
Yes. Once you add that to your routes your Story controller will respond to the common REST verbs in the expected ways.