1

I am quite new to Ruby on Rails and wanted to place my custom CSS file into the Devise (Sign Up View). I placed my global CSS into the application.js (Asset Pipeline) and placed 2 Helper functions in my

helpers/application_helper.rb

  def javascript(*files)
    content_for(:head_javascript) { javascript_include_tag(*files) }
  end

  def stylesheet(*files)
    content_for(:head_stylesheet) { stylesheet_link_tag(*files) }
  end

and my views/layouts/application.html.erb

<!DOCTYPE html>
<%
   if (controller.controller_name == "sessions") && (controller.action_name == "new")
     javascript 'theme/signup'
     stylesheet 'theme/signup'
   end
%>

<html>
<head>
  <title>Mysite</title>
  <!--[if lt IE 9]>
  <script src="http://html5shim.googlecode.com/svn/trunk/html5.js" type="text/javascript"></script>
  <![endif]-->
  <%= yield(:head_stylesheet) %>
  <%= stylesheet_link_tag    "application",  :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= yield(:head_javascript) %>
  <%= csrf_meta_tags %>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<%= yield %>
</body>
</html>

Then I generated the Devise Views and adjusted my twitter bootstrap form rails generate devise:views

And now want to place my signup.css inside the Signup View, but how?

So I created the Devise Controllers, to use my helper methods:

bash <(curl -s https://raw.github.com/foohey/cdc/master/cdc.sh)

Now I am stuck, because I my helpers don´t work with:

<%
   if (controller.controller_name == "sessions") && (controller.action_name == "new")
     javascript 'theme/signup'
     stylesheet 'theme/signup'
   end
%>

Is there an easy way in rails to handle custom css files? I am new to the asset pipeline concept and it is a bit confusing.

Thanks for help.

4

3 回答 3

3

您听说过 content_for 吗?它对这样的事情非常有用。在你的布局里面添加:

<%= yield :head %>

在你的头部标签内。然后您可以在视图中编写以下内容(设计/会话/新):

<% content_for :head do %>
  javascript 'theme/signup'
  stylesheet 'theme/signup'
<% end %>

这将完美地将您的布局与单一视图问题分开,并且还应该解决您遇到的问题。

于 2013-09-09T12:58:34.723 回答
1

您可以将自定义 css 文件放在公共目录内的 css 目录中,并在<head>您希望自定义 css 文件成为您的视图的标签内执行此操作

<%= stylesheet_link_tag '/css/signup' %>

signup.css假设您放置 stylesheet_link_tag 的位置,则仅适用于注册视图

于 2013-09-09T13:10:12.243 回答
0

我想你想要这样的东西:--

<% if (controller.controller_name == "sessions") && (controller.action_name == "new")%>
   <%= stylesheet_link_tag "theme/signup"%>
    <%= javascript_include_tag "theme/signup" %>
<% end %>
于 2013-09-09T12:58:44.380 回答