0

一个接一个的问题哈哈,但我离完成我的项目太近了!!

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

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

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

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

我将添加控制器代码和视图代码以供参考。


我的问题:

如何让外部样式表和图像在我的控制器文件中为“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 

意见/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>

请让我知道,如果你有任何问题。谢谢!

4

1 回答 1

1

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

2个选项:

  1. 以 Rails 的方式进行操作并从布局中链接您的内容,或者在布局中使用从视图内部yield生成标签的布局。http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_forcontent_forsalesrepdisplay
  2. 而不是render :action => 'salesrepdisplay',使用render :action => 'salesrepdisplay', :layout => nil
于 2013-11-13T23:13:38.010 回答