在两个不同的部分 想传递两个变量
In two different partial Want to pass two variable
我有 show.html.erb
用户文件
在这里,我将部分文件的详细信息添加为 _details.html.erb
在详细页面我有部分 _form.html.erb
现在我在 show.html.erb
中编写了 this.Have 部分文件代码
user
@user_entry
<%= render :partial => "users/details" ,:locals => {:user => user } %>
在_detail.html.erb
中有部分文件代码
<%= render :partial => "users/form" %>
我有这两个 user & @user_entry
我想要 @user_entry
变量在 form
部分我怎么能?
有知道的帮帮我
谢谢。
试试这个。
在你的 show.html.erb
User
@user_entry
<%= render :partial => "users/details" ,:locals => {:user => user,:user_entry => @user_entry } %>
然后尝试访问您 user_entry
中的变量 _details.html.erb
文件。
实例变量(前面带有 @
符号的变量)在渲染调用之前定义时在所有部分中可用。
当您传递 locals
时,它们可作为局部变量使用,而不是 "forwarded" 后续部分。
控制器:
class FooController < ApplicationController
def index
@foos = Foo.all
end
end
foos/index.html.erb
Template: we have <%= @foos.count %>
<%= render('partial', locals: {foos: @foos, other: 'a value here'}) %>
foos/_partial.html.erb
Partial: We have <%= foos.count %> and <%= other %>
<%= render('subpartial', locals: {foos: foos, other: 'another value here'}) %>
foos/_subpartial.html.erb
Subpartial: We have <%= foos.count %> and <%= other %>
我有 show.html.erb
用户文件
在这里,我将部分文件的详细信息添加为 _details.html.erb
在详细页面我有部分 _form.html.erb
现在我在 show.html.erb
user
@user_entry
<%= render :partial => "users/details" ,:locals => {:user => user } %>
在_detail.html.erb
<%= render :partial => "users/form" %>
我有这两个 user & @user_entry
我想要 @user_entry
变量在 form
部分我怎么能?
有知道的帮帮我
谢谢。
试试这个。
在你的 show.html.erb
User
@user_entry
<%= render :partial => "users/details" ,:locals => {:user => user,:user_entry => @user_entry } %>
然后尝试访问您 user_entry
中的变量 _details.html.erb
文件。
实例变量(前面带有 @
符号的变量)在渲染调用之前定义时在所有部分中可用。
当您传递 locals
时,它们可作为局部变量使用,而不是 "forwarded" 后续部分。
控制器:
class FooController < ApplicationController
def index
@foos = Foo.all
end
end
foos/index.html.erb
Template: we have <%= @foos.count %>
<%= render('partial', locals: {foos: @foos, other: 'a value here'}) %>
foos/_partial.html.erb
Partial: We have <%= foos.count %> and <%= other %>
<%= render('subpartial', locals: {foos: foos, other: 'another value here'}) %>
foos/_subpartial.html.erb
Subpartial: We have <%= foos.count %> and <%= other %>