所以我是rails的新手,目前正在关于tutorialspoint的教程。到目前为止我得到的是一个控制器和一个相应的视图,它是一个erb文件。这是视图的代码:
<% if @books.blank? %>
<p>There are not any books currently in the system.</p>
<% else %>
<p>These are the current books in our system</p>
<ul id = "books">
<% @books.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %>
</ul>
<% end %>
<p><%= link_to "Add new Book", {:action => 'new' }%></p>
但是,当我尝试通过localhost:3000查看时,
rails server
命令在localhost:3000后台运行WEBrick服务器, 它一直指引我浏览器上的默认视图,该视图由服务器从以下路径呈现:
/Users/hassanali/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/templates/rails/welcome/index.html.erb
而不是我的rails应用程序文件夹的视图文件夹中的实际视图..这是
/Users/hassanali/Desktop/library/app/views/book/list.html.erb
我一直试图解决这个问题已经很久没有用了。有谁知道我能做什么?
答案 0 :(得分:4)
因为您没有为自己的应用定义root_path
。
在config/routes.rb
中定义root_path
,例如:
root 'book#list'
答案 1 :(得分:2)
你可以告诉Rails你想要什么&#39; /&#39;使用root controller#action
路由到。
例如,假设你有一个看起来像这样的控制器。
class BooksController < ActionController::Base
def index
@books = Books.all
end
def show
@book = Books.find(params[:id])
end
end
如果你想要&#39; /&#39;要转到index
方法,您可以在config/routes.rb
中执行以下操作。
Rails.application.routes.draw do
root 'books#index'
# Other routes here...
end
作为旁注,如果添加两个root
方法调用,它将使用routes文件中的最后一个。
http://guides.rubyonrails.org/routing.html#using-root
由于您引用了show
和new
操作,因此您也可能也想要这些路线。建议使用RESTful路由方案。 Rails提供了一个名为resources
的有用方法,它创建了CRUD资源所需的所有路由和辅助方法。
这可能如下所示。
Rails.application.routes.draw do
root 'books#index'
# A books resource
resources :books
end
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
我建议您阅读有关路由的Rails指南,以便了解您的选择。