我的应用程序有一个选择框供用户选择"场地".正如您所料,此选择框位于表单中.我还在页面的某个位置通过AJAX创建了一个新的场地.创建新场地后,我想更新场地选择框以反映这一点.
我的解决方案是将选择框放在部分中,并从控制器中的create动作渲染部分.
<div id="venue_select" style="clear: both;">
<%= render :partial => 'venue/venue_select_box' %>
</div>
Run Code Online (Sandbox Code Playgroud)
部分看起来像这样:
<%= f.collection_select :venue_id, @user_venues, :id, :name, :prompt => 'Select a venue' %>
Run Code Online (Sandbox Code Playgroud)
其中f是表格参考:
<% form_for :shows do |f| %>
Run Code Online (Sandbox Code Playgroud)
问题是f在partial中未定义,所以我得到一个错误.一种解决方案是包含整个表单,但我觉得这不应该是必要的,因为我没有更新整个表单.关于如何解决这个问题的任何想法?
我试图弄清楚在其中生成一些html和嵌套内容的最简洁方法.使用HAML.基本上我想做的事情如下:
= block_large
This is some nested content
Run Code Online (Sandbox Code Playgroud)
这应该产生:
<div class="block large">
<img src="block_large_carat.gif" class="block_large_carat">
This is some nested content
</div>
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何实现这一目标.谐音?帮手?我对如何嵌套我想要的任何内容感到困惑.试图保持我的HAML DRY并且不想一遍又一遍地明确声明图像标签.
如何正确缩进代码?
应用程序/视图/布局/ shared.html.haml:
= render :partial => "shared/head"
= yield
= render :partial => "shared/footer"
Run Code Online (Sandbox Code Playgroud)
应用程序/视图/共享/ _head.html.haml:
!!!XML
!!!1.1
%html{"xml:lang" => "pl", :xmlns => "http://www.w3.org/1999/xhtml"}
%head
%title
some title
%body
.container
Run Code Online (Sandbox Code Playgroud)
应用程序/视图/共享/ index.html.haml:
%p
Hello World!
Run Code Online (Sandbox Code Playgroud)
应用程序/视图/共享/ _footer.html.haml:
.footer
Some copyright text
Run Code Online (Sandbox Code Playgroud)
呈现的HTML输出:
<!DOCTYPE html>
<html xml:lang='pl' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>
some title
</title>
</head>
<body>
<div class='container'></div>
</body>
</html>
<p>
Hello World!
</p>
<div id='footer'>
Some copyright text
</div>
Run Code Online (Sandbox Code Playgroud) 所以这是一个反复出现的问题,并没有在SO上找到另一个例子,所以这里是:
渲染Jade模板'variableName' undefined时-if(variableName),即使在模板中使用也是如此.
示例(我将其用作"信息"flash消息的部分内容):
-if(info)
- if(info.length){
ul
-info.forEach(function(info){
li= info
-})
-}
Run Code Online (Sandbox Code Playgroud)
如果没有flash/info消息,则返回'info'未定义,而不是不呈现任何内容.有谁知道我做错了什么?
我知道typeof(variable) != 'undefined提到的选项.如果我想做一些像-if (typeof(req.session.user) != 'undefined')我必须要做的事3嵌套`if(typeof(req)!='undefined'.这是我唯一的选择吗?
我有一个"index.html.erb"文件,其中包含以下内容:
<%=渲染@users%>
这将呈现"_user.html.erb"并输出一个按钮,用于对每个用户执行特定操作:
<%= link_to"action",action_path(user),:id =>"#{user.id} _action",:remote => true)%>
我已经设置了我的用户控制器,通过查看"action.js.erb"来响应AJAX请求.
为了在partial中的特定用户上执行javascript方法,我想知道我的部分实例变量(例如user.id)如何在js.erb文件中传递或访问,例如:
$("#{@user.id}_action").toggle();
Run Code Online (Sandbox Code Playgroud) 因此,当用户成功上传文件时,我只是想在我的一条Flash消息中添加一些社交网络共享链接(facebook,g +,addthis等).我将所有代码都放在一个部分,所以我想我可以渲染它,但我只是得到一堆"/ n"和html_safe不起作用.
这是我的部分:
<div class="share_buttons">
<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
<a class="addthis_button_facebook"></a>
<a class="addthis_button_twitter"></a>
<a class="addthis_button_stumbleupon"></a>
</div>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e8b938653ac0cd3"></script>
</div>
<script type="text/javascript">
reddit_target='tattoos'
</script>
<script type="text/javascript" src="http://www.reddit.com/static/button/button1.js"></script>
<% content_for :jquery do %>
<%= javascript_tag do %>
$(document).ready(function() {
$(".share_buttons a").live("click", function() {
$.ajax({
url: '/index/share?id=<%= @tattoo.id%>',
type: 'post'
});
return false;
});
});
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我尝试了这个:
flash[:success] = "Thanks, feel free to share that out! #{render :partial=> /shared/social_buttons}"
可能重复:部分中的
Flash消息(Rails 3)
我正在做Michael Hartl的Railstutorial并列出7.26将Flash消息添加到应用程序布局:
<!DOCTYPE html>
<html>
.
.
.
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
.
.
.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这很好用.
但是,我尝试通过在我的部分文件夹中创建_flash.html.erb来清理此代码...
<% flash.each do |key,value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<!-- <div class="alert alert-<%= key %>"><%= value %></div> -->
<% end %> …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails partials ruby-on-rails-3 railstutorial.org
我试图使用Mustache部分渲染我的嵌套(可以是多级)JSON.它只渲染到第二级,它不是第三级和更高级.根据定义,partials可用于递归呈现.我做错了吗?或者有没有其他方法来实现相同的胡子?
模板:
<script id="product-list" type="x-tmpl-mustache">
<ul class='products'>
{{#product}}
<li class='product'> {{ productName }} </li>
{{> recurse }}
{{/product}}
{{^product}}
<li class='empty'> No products Available </li>
{{/product}}
</ul>
</script>
<script id="recursive-list" type="x-tmpl-mustache">
<ul class='products'>
{{#product}}
<li class='product'> {{ productName }} </li>
{{/product}}
</ul>
</script>
Run Code Online (Sandbox Code Playgroud)
数据:
var data = {
product: [{
productName: "Category1",
product: [{
productName: "Windows"
}, {
productName: "Windows Mobile"
}]
}, {
productName: "Category2",
product: [{
productName: "SubCategory 1",
product: [{
productName: "Nexus 4"
}, { …Run Code Online (Sandbox Code Playgroud) 我有一个Rails 2.3.5应用程序,我试图从模型中呈现几个部分(我知道,我知道 - 我不应该).我这样做的原因是我将Comet服务器(APE)集成到我的Rails应用程序中,需要根据Model的事件(例如after_create)推出更新.
我试过这样做:
ActionView::Base.new(Rails::Configuration.new.view_path).render(:partial => "pages/show", :locals => {:page => self})
Run Code Online (Sandbox Code Playgroud)
这允许我渲染不是用户助手的简单部分,但是如果我尝试在我的部分中使用link_to,我会收到一条错误说明:
undefined method `url_for' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
我确保传递给"project_path(project)"的对象不是nil.我也试过包括:
include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter
Run Code Online (Sandbox Code Playgroud)
在包含进行上述"render"调用的方法的Module中.
有谁知道如何解决这个问题?
谢谢
我正在使用AngularJs和模板系统.我想为每个模板添加特定的内联javascript脚本,为选定的选项卡添加警告框(主页|列表|设置)
Html呈现: 但是添加了ng-scope,并且在更改选项卡时没有任何警报.
<script type="text/javascript" class="ng-scope">alert("home")</script>
Run Code Online (Sandbox Code Playgroud)
我在这里提供示例:
或者在这里
带有alert("template1")的plunkr示例存在于template1.html中,但呈现为
<script type="text/javascript" class="ng-scope">alert("template1")</script>
Run Code Online (Sandbox Code Playgroud)