【问题标题】:Ruby on Rails - Ruby code in view file gets printedRuby on Rails - 打印视图文件中的 Ruby 代码
【发布时间】:2012-08-27 00:11:56
【问题描述】:

我在 Aptana Studio 3 中有一个 Rails 3 项目,其中有一个 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 %>
<p>
    <%= link_to "Add new Book", {:action => 'new' }%>
</p>

然后在嵌入式终端中,我运行 rails server,单击 Aptana 中的“使用 Firefox Server 运行”按钮,这将使用 Firefox 打开应用程序,并将我定向到此链接:http://127.0.0.1 :8020/library/app/views/book/book.html.erb

问题是我得到了这个输出:

<% if @books.blank? %>

There are not any books currently in the system.
<% else %>

These are the current books in our system

    <% @books.each do |c| %>
    <%= link_to c.title, {:action => 'show', :id => c.id} -%>
    <% end %> 

<% end %>

<%= link_to "Add new Book", {:action => 'new' }%> 

似乎 ruby​​ 代码没有被评估而是被打印出来,但是语法对我来说看起来没问题...有谁知道可能是什么问题?

【问题讨论】:

    标签: html ruby-on-rails ruby embedded-ruby


    【解决方案1】:

    Aptana 没有打开正确的页面。如果你只是使用默认服务器,那么你可能想打开localhost:3000

    更多信息:查看 url,它只是文件的路径,而不是书籍索引的 url。

    您的文件路径 (http://127.0.0.1:8020/library/app/views/book/book.html.erb) 也似乎很奇怪...

    首先,book 文件夹名称应该是复数 (app/views/books)。其次,您的视图代码似乎是图书索引页,所以它可能应该在app/views/books/index.html.erb

    【讨论】:

    • 然后我得到了标准的“Welcome to Rails”主页……但我在启动过程中也注意到了这个错误:ActionController::RoutingError (No route matches [GET] "/library")。该项目的名称是“库”,这可能是问题吗?
    • 删除/public中的index.html文件。
    • 此错误现在显示在页面上:No route matches [GET] "/"
    • 只需找到任何教程来设置 RoR Web 应用程序并重新开始。看来您只是跳过了太多步骤。
    • 我同意@oldergod...向我们展示您的路线文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多