我正在尝试生成包含一些HTML的JSON响应.因此,我有/app/views/foo/bar.json.erb:
{
someKey: 'some value',
someHTML: "<%= h render(:partial => '/foo/baz') -%>"
}
Run Code Online (Sandbox Code Playgroud)
我希望它渲染/app/views/foo/_baz.html.erb,但它只会渲染/app/views/foo/_baz.json.erb.传球:format => 'html'没有帮助.
例如,Ryan Bates的nifty_scaffolding就是这样做的
edit.html.erb
<%= render :partial => 'form' %>
Run Code Online (Sandbox Code Playgroud)
new.html.erb
<%= render :partial => 'form' %>
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for @some_object_defined_in_action %>
Run Code Online (Sandbox Code Playgroud)
隐藏的状态让我感到不舒服,所以我通常喜欢这样做
edit.html.erb
<%= render :partial => 'form', :locals => { :object => @my_object } %>
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for object %>
Run Code Online (Sandbox Code Playgroud)
那么哪个更好:a)有部分访问实例变量或b)传递部分所需的所有变量?
我一直在选择b)最近,但我确实遇到了一点点泡菜:
some_action.html.erb
<% @dad.sons.each do |a_son| %>
<%= render :partial => 'partial', :locals => { :son => a_son } %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
_partial.html.erb
The son's name is <%= son.name %>
The dad's name is …Run Code Online (Sandbox Code Playgroud) 我有一组经常使用的rails视图助手,并希望将它们打包成一个gem,这样我就可以在我的Gemfile中添加一行,并从我的视图中访问帮助器.
我在使用Bundler和Jeweler之前创建了宝石,但是,我并不是很清楚如何在gem中组织Rails视图助手,并将它们包含在rails中.
我将非常感谢任何指针,或链接到有关如何为Rails 3执行此操作的最新教程
谢谢
只是为了澄清:问题不在于"如何创造宝石".它的"如何在gem中打包视图助手,所以我可以在Rails中使用它们"
编辑2:我也同意下面的海报..轨道引擎对于这种(希望简单的)要求太过分了
这似乎是一个菜鸟问题,但简单的答案是在逃避我.我需要调用link_to一个ActionController方法来吐出一个HTML链接. ActionView::Helpers::UrlHelper.link_to调用url_for,但这会调用AV模块的版本而不是控制器的版本.我设法强迫这样做,通过推杆做我想要的
#FIXME there must be a better way to mixin link_to
alias_method :self_url_for, :url_for
include ActionView::Helpers::UrlHelper
alias_method :url_for, :self_url_for
Run Code Online (Sandbox Code Playgroud)
在控制器中.但是,我仍然不确定它为什么会起作用.有人可以解释方法范围并隐藏这里发生的事情吗?什么是更好的混合方式link_to(或者通常只包括模块中的一些方法),所以我可以在控制器中调用它(生成带有链接的flash字符串就是用例.)
请不要讲MVC - 如果有的话,link_to应该在一个单独的模块中url_for.从噪音的数量来看,很多人遇到这个看似微不足道的障碍,并最终浪费了一个小时做"Rails方式",真正想要的是一分钟黑客让我的应用程序现在工作.是否有"Rails方式"可能与助手一起做这件事?还是更好的红宝石方式?
在Rails 2.3.x中,您可以render_optional_error_file像这样覆盖:
# ApplicationController.rb
protected
def render_optional_error_file(status_code)
render :template => "errors/500", :status => 500, :layout => 'application'
end
Run Code Online (Sandbox Code Playgroud)
但是,Rails 3不再具有render_optional_error_file.相反,你需要覆盖rescue_action_in_public,你可以这样做:
# config/initializers/error_page.rb
module ActionDispatch
class ShowExceptions
protected
def rescue_action_in_public(exception)
status = status_code(exception).to_s
template = ActionView::Base.new(["#{Rails.root}/app/views"])
if ["404"].include?(status)
file = "/errors/404.html.erb"
else
file = "/errors/500.html.erb"
end
body = template.render(:file => file)
render(status, body)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这有效,但不使用应用程序的布局.但是,如果您指定布局路径,如下所示:
body = template.render(:file => file, :layout => "layouts/application") # line 15
Run Code Online (Sandbox Code Playgroud)
你得到一个Error during failsafe response: ActionView::Template::Error. …
error-handling exception-handling ruby-on-rails actionview ruby-on-rails-3
我有一个ImageView我附加到MenuItem它ActionView(项目出现在ActionBar).此视图的布局来自XML.我这样膨胀它:
ImageView actionView = (ImageView) layoutInflater.inflate(
R.layout.action_view_layout, null);
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常.然而; root在调用中传递null inflate()使Lint对我大喊:
避免将null作为视图根传递(需要解析膨胀布局的根元素上的布局参数)
在我的具体情况下,我似乎可以在没有root的情况下进行管理,但我宁愿让代码尽可能正确.问题是,我不确定哪个View应该用作根.这个答案说它应该是"围绕想要膨胀的视图对象的小部件".但这意味着什么呢?动作栏的那一个?活动?还有别的吗?
更新:阅读答案让我怀疑我正确的做法是:
ActionBar View对应的MenuItemViewGroup这似乎有效.任何人都可以确认或否认这是否应该做什么?
我想将form_for对象传递给部分:
<%= form_for @price do |f| %>
...
<%= render :partial => "price_page", :object => @price, :as => :f %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
f.radio_button
Run Code Online (Sandbox Code Playgroud)
带来错误:
undefined method `radio_button' for #<Price:0x3cb1ed0>
Run Code Online (Sandbox Code Playgroud)
我怎么能f像往常一样使用这部分?
我需要将HTML转换为纯文本.在ActionView的SanitizeHelper中有一个很好的功能,但我无法理解如何引用它并在一个简单的test.rb文件中使用它.
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
我想能打电话 strip_tags("<b>lol</b>") => "lol"
我想自定义添加ActionView到我的ActionBar.
我想添加常用的刷新按钮.(ImageButton,ProgressBar在里面FrameLayout)但如果我使用的ActionView onOptionsItemSelected()是从未被调用过的.
这是代码:
在我的Activity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.messages_actionbar, menu);
mRefreshView = (RefreshView) menu.findItem(R.id.messages_refresh).getActionView();
return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
messages_actionbarsrc:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/messages_refresh"
android:title="title"
android:icon="@drawable/icon"
android:showAsAction="always"
android:actionViewClass="com.blabla.RefreshView"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
RefreshView的代码:
public class RefreshView extends FrameLayout {
private ImageView mButton;
private ProgressBar mProgressBar;
private boolean mLoading;
public RefreshView(Context context) {
super(context, null);
initView(context);
}
public RefreshView(Context context, …Run Code Online (Sandbox Code Playgroud) 我有一个iframe,它呈现部分并且不是主应用程序布局或资产管道的一部分.
我想要包含一些样式表,但是我收到此错误:
ActionView::Template::Error (960sm.css isn't precompiled):
Run Code Online (Sandbox Code Playgroud)
Rails 3.1 Heroku