ruby-on-rails – 在我的新Rails应用程序中工作/显示的图像
更新:o.k.这很奇怪,在浏览器中,我可以访问localhost:3000 / assets / images / rails.png中的图像,但是当我将该路径放入seeds.rb文件然后加载页面时,它显示它正在尝试查找/assets/rails.png中的图像,即它正在跳过图像文件夹…任何想法?
是否可以将rails配置到某个位置以仅查看两个文件夹? 我正在使用一本名为Agile web Development with Rails的书来学习如何使用框架,但似乎有一些细微的差别.它为rails 3.0和3.1提供了代码(我正在使用后者)但它并不总是按预期工作.到目前为止,我们已经为Products类创建了scaffolding,并使用seeds.rb将一些数据放入sqlite3数据库中.每个“产品”的assets / images文件夹中都有图像,但它没有显示. 图像位于app / assets / images文件夹中 我将向您展示以下文件,所有这些文件都以某种方式处理图像 1.app/views/products/_form.html.erb 更新:在日志文件中,它表示图像存在路由错误. Started GET "/assets/wd4d.jpg" for 127.0.0.1 at Tue Sep 27 11:01:43 -0400 2011 Served asset /wd4d.jpg - 404 Not Found (3ms) ActionController::RoutingError (No route matches [GET] "/assets/wd4d.jpg"): Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms) Started GET "/images/ruby.jpg" for 127.0.0.1 at Tue Sep 27 11:01:43 -0400 2011 ActionController::RoutingError (No route matches [GET] "/images/ruby.jpg"): 应用程序/视图/产品/ _form.html.erb <%= form_for(@product) do |f| %> <% if @product.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@product.errors.count,"error") %> prohibited this product from being saved:</h2> <ul> <% @product.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :description %><br /> <%= f.text_area :description,:rows => 6 %> </div> <div class="field"> <%= f.label :image_url %><br /> <%= f.text_field :image_url %> </div> <div class="field"> <%= f.label :price %><br /> <%= f.text_field :price %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> 2.app/views/products/index.html.erb <h1>Listing products</h1> <table> <% @products.each do |product| %> <tr class="<%= cycle('list_line_odd','list_line_even') %>"> <td> <%= image_tag(product.image_url,:class => 'list_image') %> </td> <td class="list_description"> <dl> <dt><%= product.title %></dt> <dd><%= truncate(strip_tags(product.description),:truncate => 80) %></dd> </dl> </td> <td class="list_actions"> <%= link_to 'Show',product %><br/> <%= link_to 'Edit',edit_product_path(product) %><br/> <%= link_to 'Destroy',product,:confirm => 'Are you sure?',:method => :delete %> </td> </tr> <% end %> </table> <br /> <%= link_to 'New product',new_product_path %> > 3. db / seeds.rb(注意我在这里尝试了图片网址) Product.delete_all . . . Product.create(:title =>’Programming Ruby 1.9′, . . . Product.create(:title =>’Rails Test Prescriptions’, 解决方法
好的,所以我一直在完成这本书,你必须在2个地方做出改变.
在种子文件中,只需引用jpg的名称.由于您使用的是rails 3.1,因此资产/ images /中的任何内容都可以通过它的名称引用. 您还必须对视图进行更改.在这里下载本书的源代码http://pragprog.com/titles/rails4/source_code并查看他们对于产品#index视图的库2的内容.这个版本让他们变得非常草率. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |