Question about routes.rb
Rails newbie here.
Can anyone please explain the difference to me between the following lines of code:
match '/' => 'posts#index'
and
match '/' => 'posts#index', :as => 'posts'
The reason I'm asking is because when I use the latter code, I can开发者_Go百科not create new posts :|
The latter is creating a named route. It creates a helper that you can call from your views, in this case, posts_path
& posts_url
.
That being said, I'm not sure how you are able to create new posts with either of those as you are not defining the posts#new
or posts#create
. Is there more to your routes file than these? Also, I'm not sure if it's a requirement or not, but you should pass your :as
option as a symbol, so :as => :posts
.
For reference, you can run rake routes
from console and see a list of all the routes that are defined in your application. You'll also see how they are named—that's the column all the way to the right—which you can then append _path
or _url
to.
精彩评论