开发者

Question about routes.rb

开发者 https://www.devze.com 2023-03-14 12:28 出处:网络
Rails newbie here. Can anyone please explain the difference to me between the following lines of code:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消