html - 为什么我的 HTML 模板不能在我的 Ruby on Rails Controller 中显示图像/外部 css 定义?

标签 html css ruby-on-rails ruby

一个又一个问题哈哈,但我离完成我的元素太近了!!

所以我的 Ruby on Rails 程序已经完成并开始工作了。现在我正在尝试将它集成到 HTML 模板中..

我终于可以正确加载图形和模板,但仅限于 index.html.erb 页面,因为这是起始页面。现在我的问题是我的 Controller 文件中定义为“result”的页面无法正确显示。看起来这可能是一个简单的修复,但不确定如何。根据 result 的格式,看起来内联 css 样式正常工作,但我的 style.css 中的图像和样式现在没有为模板加载(索引除外)。

我把images目录作为public的子目录。我也有公开的 style.css。

我在 View 中有一个名为“customerdisplay.html.erb”的页面,其中包含我的模板的 HTML 代码。该文件中的一个 div 标签是我的外部样式表中的自定义 div,但是当我使用该标签时,我还将对齐设置为居中。居中有效,但外部样式表中的其他所有内容均无效。

我将添加 Controller 代码和 View 代码以供引用。


我的问题:

如何让外部样式表和图像为我的 Controller 文件中的“def result”工作?


myRuby_controller.rb

class MyRubyController < ApplicationController
  def index
# the first user screen is displayed:
# index.html.erb in views/myRuby directory
    # This controller is executed
  end

def result
    # when the user presses submit, result is called
@data = params[:data] # The data text field 
    @field = params[:field] # the request
    # check for nondigits in the data
   
    if @data =~ /\D/ 
       # if a nondigit, invalid.html.erb is displayed
       render :action => 'invalid' 
    else  # valid data 
    case @field   # check the request
        when  "cu" then  # customer data request
           # Verify that customer is in the customer table
           if Customer.exists?(@data) 
             # exists, display custdisply.html.erb
             render :action => 'custdisplay'
           else
             # does not, display notfound.html.erb
             render :action => 'notfound'
           end
        when "sr" then   # sales rep request
            if Customer.exists?(@data)
                         # exists, display salesrepdisplay.html.erb
                             render :action => 'salesrepdisplay'
                       else
                         # does not, display notfound.html.erb
                             render :action => 'notfound'
                       end

        when "or" then  #orders request
            if Customer.exists?(@data)
                         # exists, display orderdisplay.html.erb
                             render :action => 'orderdisplay'
                       else
                         # does not, display notfound.html.erb
                             render :action => 'notfound'
                       end

        when "p" then  #parts request
                        if Customer.exists?(@data)
                         # exists, display partsdisplay.html.erb
                             render :action => 'partsdisplay'
                       else
                         # does not, display notfound.html.erb
                             render :action => 'notfound'
                       end

    end  # end case

     end # end if valid data        

   end
   def custdisplay
   end
   def salesrepdisplay
   end
   def orderdisplay
   end
   def partsdisplay
   end
   def notfound
   end
   def invalid
   end
end 

views/customerdisplay.html.erb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
    <link rel="stylesheet" type="text/css" href="style.css">
<title>The Justin Geis Data Access Program</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
<div id="logo"><img src="images/logo.png"></div>
<div id="container">
    <div id="header">
    <!--blank.. could add stuff if wanted.. --> 
    </div>
        
        <div id="content" align="center">
        <h1>Spartan Hardware Data Access</h1>
        <%  # get the customer data from the customer table
@custdata = Customer.find(@data)
# copy each field into a variable for display
@id = @custdata.id
@ln = @custdata.last_name
@fn = @custdata.first_name
@bl = @custdata.balance;
@bl = sprintf("%7.2f",@bl) # format as currency
@cl = @custdata.credit_limit
@cl = sprintf("%7.2f",@cl) # format as currency
@sr = @custdata.sales_rep

%>
<h1>customer data for customer: <%=@data %></h1>
<table border=1>
        <tr>
          <td>ID</td>
          <td>Last name</td>
          <td>First name</td>
          <td>Balance</td>
          <td>Credit limit</td>
          <td>Sales rep</td>
        </tr>
        <tr>
          <td><%=@id%></td>
          <td><%=@ln%></td>
          <td><%=@fn%></td>
          <td><%=@bl%></td>
          <td><%=@cl%></td>
          <td><%=@sr%></td>
        </tr>
      </table>
    </div>
    <div id="footer"><!--nothing right now --></div>
    
    </div> 
  </body>
</html>

如果您有任何问题,请告诉我。谢谢!

最佳答案

您的 View 正在另一个 View 中呈现,称为布局,通常在文件中 app/views/layouts/application.html.erb .这就是为什么你的 <head>不会工作,因为它在 <body> 中.

2个选项:

  1. 按照 Rails 的方式进行操作并从布局中链接您的内容,或者使用 yield在从 content_for 产生标签的布局中在你的里面 salesrepdisplay看法。 http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for
  2. 代替render :action => 'salesrepdisplay' , 使用 render :action => 'salesrepdisplay', :layout => nil

关于html - 为什么我的 HTML 模板不能在我的 Ruby on Rails Controller 中显示图像/外部 css 定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19966107/

相关文章:

html - 在 CSS 中指定图像大小是否允许浏览器在下载完成之前进行布局?

javascript - 如何在dom中制作一些特定值的字符串?

html - 网站布局在 Firefox 浏览器中分崩离析

html - 为什么包装器元素会自动增加?

ruby-on-rails - 通过迁移在 ruby​​-on-rails 3 中创建表?

jquery - 使用 Selenium 时验证 DOM 操作

javascript - 当其中一名类(class)成员悬停时突出显示所有类(class)

ruby-on-rails - 在 rails ActiveRecord 中对自定义列进行类型转换?

javascript - Acts_As_Votable Ajax 请求

javascript - Bootstrap collapsible 第一次后不工作