簡體   English   中英

Rails 4中的Application.html.erb

[英]Application.html.erb in rails 4

我是Rails 4.0.2的新手。 我正在使用Rails版本4和ruby 2的應用程序。在視圖中,application.html.erb文件不起作用。 它不會在所有視圖文件上復制。

這是我在application.html.erb文件中的代碼:

<!DOCTYPE html>
<html>
<head>
  <title>Myapp</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>

我有一個名為xyz_controller.rb的控制器,該控制器具有以下代碼:

class xyzController << ApplicationController



end

application_controller.erb文件具有以下代碼:

class ApplicationController < ApplicationController

       def login

       end
end

login.erb文件包含普通的html代碼。

提前致謝。

您的應用程序控制器應如下所示:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

您的XYZ控制器應如下所示:

class XyzController < ApplicationController
  def action
    #here you define instance variables used in your view for this action
  end
  ...
end

...因此,例如,您的login操作將在此處定義。

您的視圖應命名為action.html.erb,其中action與您的控制器操作匹配,並且該文件應位於app / views / xyz中。

Rails的口頭禪是約定勝於配置。 這意味着,如果(非常緊密地)遵循Rails的命名約定等,一切都會變得容易得多。如果您還沒有話,我強烈建議您像Hartl這樣的優秀教程來學習。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM