There are a number of other Rails2 -> 3 routing questions but mine is less complex and is actually not giving me an error. So why the question? Well I want to make sure I am not using additional unnecessary code. Disclaimer: I am new to Ruby/Rails learning Rails3 via Simply Rails 2 book trying to adapt/figure out all the errors in hopes of getting a more in-depth understanding of the language.
So I have my first view, index.html.erb (in app/views/stories). When I initially went to view it at localhost:3000/stories like the book said I got an error No route matches "/stories"
after changing t开发者_C百科he url to localhost:3000/stories/index the page showed up perfectly.
A look at the routes.rb shows:
Shovell::Application.routes.draw do
get "stories/index"
I have now created a second view which is new.html.erb (in app/views/stories):
<% form_for @story do |f| %>
<p>
name:<br />
<%= f.text_field :name %>
</p>
<p>
link:<br />
<%= f.text_field :link %>
</p>
<p>
<%= submit_tag %>
</p>
<% end %>
This view wouldn't show up at any URL I tried. Looking at the server log I decided it was a routes thing. I changed the routes.rb to this:
Shovell::Application.routes.draw do
get "stories/index"
get "stories/new"
Now when I go to localhost:3000/stories/new the page works fine (albeit the method error that is part of the exercise in the book).
It doesn't seem right that I would have to manually enter every view into routes.rb, there must be a way to set root and let it recognize all the files there. Can I do that?
FYI http://edgeguides.rubyonrails.org/routing.html
root :to => "stories#index" # This means you render the root url by using stories controller and index action
resources :stories # The standard way to generate a resource (with index, new, edit, show, create, update, destroy actions automatically defined for you and supports some customization)
So for your case, actually the resources :stories
gave you the urls you need.
精彩评论