【问题标题】:Without an action in controller how does rails execute the view?如果控制器中没有操作,rails 是如何执行视图的?
【发布时间】:2015-11-23 20:12:29
【问题描述】:

我刚开始学习 Rails。我对以下部分有疑问。

控制器:

book_controller.rb

 class BookController < ApplicationController
 end

查看:

list.html.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 %>

路由器:

get 'list' => 'book#list'

当我转到localhost:3000/list 时,它会显示list.html.erb 的内容。当我在 Controller 中没有 list 操作时,会发生什么情况?我怎么理解错了?

【问题讨论】:

  • Rails 自动处理...您的路线知道哪个控制器要执行哪个操作.. 以及必须从那里显示哪个默认视图.. 如果所有约定都在那里,事情将在没有任何如您所见,打扰了。

标签: ruby-on-rails model-view-controller


【解决方案1】:

Ruby MVC 结构已集成,因此当您创建变量 @varibale 时,它已经在视图中可用。

【讨论】:

    【解决方案2】:

    如果在 book 控制器中定义了 @books,则执行此代码不会有问题,因为 list.html.erb 已经在 books 文件夹中定义并且路由设置为

    get 'list' => 'book#list'
    

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      相关资源
      最近更新 更多