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 cannot 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#neworposts#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:asoption as a symbol, so:as => :posts.For reference, you can run
rake routesfrom 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_pathor_urlto.