在我的Rails 3应用程序中,我使用Ajax来获取格式化的HTML:
$.get("/my/load_page?page=5", function(data) {
alert(data);
});
class MyController < ApplicationController
def load_page
render :js => get_page(params[:page].to_i)
end
end
Run Code Online (Sandbox Code Playgroud)
get_page使用该content_tag方法,也应该可用app/views/my/index.html.erb.
由于get_page使用了许多其他方法,我将所有功能封装在:
# lib/page_renderer.rb
module PageRenderer
...
def get_page
...
end
...
end
Run Code Online (Sandbox Code Playgroud)
并包括它:
# config/environment.rb
require 'page_renderer'
# app/controllers/my_controller.rb
class MyController < ApplicationController
include PageRenderer
helper_method :get_page
end
Run Code Online (Sandbox Code Playgroud)
但是,由于该content_tag方法不可用app/controllers/my_controller.rb,我收到以下错误:
undefined method `content_tag' for #<LoungeController:0x21486f0>
Run Code Online (Sandbox Code Playgroud)
所以,我试图添加:
module PageRenderer
include ActionView::Helpers::TagHelper
...
end
Run Code Online (Sandbox Code Playgroud)
但后来我得到了:
undefined method `output_buffer=' for #<LoungeController:0x21bded0>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ? …
嗨,我有一个noob问题,我想创建以下HTML结果:
<a href="/controller/action" class="button-big layer">TEXT<span class="arrow-big"></span></a>
Run Code Online (Sandbox Code Playgroud)
在上面的HTML中,我希望通过css将带有span-class的文本设置为图像样式.
当我尝试以下实现时,结果只反映了所需实现的一部分:
<%= link_to "TEXT", controller_path, :class => "button-big layer" %>
Run Code Online (Sandbox Code Playgroud)
结果是:
<a href="/controller/action" class="button-big layer">TEXT</a>
Run Code Online (Sandbox Code Playgroud)
和
<%= link_to(content_tag(:span, "", :class => "arrow-big"), controller_path, :class => "button-big layer") %>
Run Code Online (Sandbox Code Playgroud)
结果是:
<a href="/controller/action" class="button-big layer"><span class="arrow-big"></span></a>
Run Code Online (Sandbox Code Playgroud)
有谁知道怎么做?
我试图在嵌套表单中使用Rails 3 content_tag方法复制"删除"按钮图标,并且不引人注意地使用jQuery(或者至少我希望如此).
使用Firebug进行检查时生成的html如下.
<a class="btn btn-danger" href="#">
<i class="icon-trash icon-white"></i>
Delete
</a>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下内容生成带图标的按钮但不能添加"删除成分",也不会获得href的"#".
这是我的代码中的成分部分:
<%= link_to content_tag(:a, content_tag(:i, '', class: "icon-trash icon-white"), class: "btn btn-danger remove_fields") %>
Run Code Online (Sandbox Code Playgroud)
这会产生:
<a class="btn btn-danger remove_fields">
<i class=icon-trash icon-white"></i>
</a>
Run Code Online (Sandbox Code Playgroud)
这是基于 Api dock - content_tag的信息
它有以下代码示例:
content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
# => <div class="strong"><p>Hello world!</p></div>
Run Code Online (Sandbox Code Playgroud)
注意我可以使用link_to块来处理这个问题,但是想知道这是否可以在没有do..end的情况下在一行中完成,更重要的是在content_for方法中.
<%= link_to('#', class: "btn btn-danger remove_fields") do %>
<i class: "icon-trash icon-white"></i>
Delete
<% end %>
Run Code Online (Sandbox Code Playgroud) 如何使用 content_tag 帮助程序获得以下 html 输出?
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
这是我目前所拥有的
content_tag(:ul) do
a=*(1..5)
a.each do |step_number|
content_tag(:li, class: "step") do
puts step_number
end
end
end
Run Code Online (Sandbox Code Playgroud)
感谢 Peter's Loop & output content_tags 在下面的帮助链接中的content_tag 中,我遗漏concat了这些li项目
content_tag :ul do
items.collect do |step_number|
concat(content_tag(:li, step_number, class: "step"))
end
end
Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习聚合物.这是我的聚合物元素的通用版本:
<polymer-element name="my-element">
<template>
<style>
:host {
position:absolute;
width: 200px;
height: 100px;
background-color: green;
}
</style>
<p>my element</p>
<content id="first-in"></content>
<content id="second-in"></content>
</template>
<script>
Polymer('my-element', {
domReady: function() {
alert(this.children[0].getAttribute('title'));
//this returns the value I want to observe
}
});
</script>
<polymer-element>
Run Code Online (Sandbox Code Playgroud)
内容标记都填充了另一个自定义元素(再次略微简化):
<polymer-element name="in-element" attributes="title">
<template>
<style>
...
</style>
<p>{{title}}</p>
</template>
<script>
Polymer('in-element', {
title: 'me'
});
</script>
<polymer-element>
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是当(任何)元素(放入内容标记中)中的title属性发生更改时(通过单击事件或其他任何内容)调用my元素中的函数.我不知道如何使用观察或如果我需要使用mutationObserver等来访问它.如何完成/是否可能?
我想使用content_tag呈现下面显示的结构,其中集合是祖先对象.
<ul>
<li>
<a>Fruits</a>
<ul>
<li>
<a>Apple</a>
</li>
<li>
<a>Orange</a>
</li>
</ul>
</li>
<li>
<a>Colours</a>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个日历表来显示公司每个员工的每日可用性.我认为我需要的是一个标题,其中包含每月的日期,然后是每个工作人员的一行(具有与天数一样多的单元格).每个单元格的不同背景颜色将代表不同的可用性状态,并且工作人员会更改,因此我希望动态生成所有这些.
经过网上研究并以Ryan Bates的Railscast#213(日历修订版)作为我的向导,我到目前为止设法生成了我的表格的标题,显示了每个单元格中每月的日期.
到目前为止,这是我的代码:
module ReceptionHelper
def reception(date = Date.today, &block)
Reception.new(self, date, block).table
end
class Reception < Struct.new(:view, :date, :callback)
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
daysHeader
end
end
def daysHeader
last_day = date.end_of_month.strftime("%d").to_i
(0..last_day).to_a.map { |day| content_tag :th, day }.join.html_safe
end
end
Run Code Online (Sandbox Code Playgroud)
现在,问题和困惑开始于此:
正如我所发现的那样,删除&block并删除:callback{...}功能.我不能说我理解为什么.谁有一个很好的解释?
为什么以上工作,但我不能使用(..).each do和content_tag下面的块?
(0..last_day).to_a.each do |day|
content_tag :th do
day
end
end
Run Code Online (Sandbox Code Playgroud)在我努力为每个员工显示行时,我选择了这个:
def table
content_tag :table, class: "calendar" do …Run Code Online (Sandbox Code Playgroud)我正在尝试创建一个 Rails FormBuilder 来使用引导样式设置表单样式:
<div class="form-group has-error">
<label for="exampleInputEmail1">Email address</label>
<div class="input-group>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
<span class="input-group-addon glyphicon glyphicon-user"></span>
</div>
<p class="help-block>can't be blank</p>
</div>
Run Code Online (Sandbox Code Playgroud)
当我只有一个 div 标签时,我就能让表单生成器工作(不包括传统引导表单的输入组)。我的问题是,我无法使用类为“input-group”的 div 获取嵌套的 content_tag ”才能正常工作。我尝试添加元素并将 content_tag 包装在捕获中,但无济于事。
class LargeFormBuilder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::TextHelper
attr_accessor :output_buffer
%w(text_field text_area email_field password_field).each do |form_method|
define_method(form_method) do |*args|
attribute = args[0]
options = args[1] || {}
options[:label] ||= attribute
options[:class] ||= "form-control input-lg"
label_text ||= options.delete(:label).to_s.titleize
content_tag(:div, class: "form-group #{'has-error' …Run Code Online (Sandbox Code Playgroud)