渲染传递局部变量的模板时出现问题
我正在 Rails 3 上运行 Ruby,我想渲染一个传递局部变量的模板 (show.html.erb
)。
在 RAILS_ROOT/views/users/show.html.erb 中,我还有
Name: <%= @user.name %>
Surname: <%= @user.surname %>
一个页面控制器来处理页面,在 application_controller.rb 中,有一个 @ 的实例当前用户。页面称为 user
,因此在 RAILS_ROOT/views/pages/user.html.erb
中我有
<%= render :template => "users/show", :locals => { :user => @current_user } %>
上面的代码不起作用(我收到此错误:Pages#user 中的 RuntimeError,Called id for nil,这会被错误地设置为 4 - 如果您确实想要 nil 的 id,请使用 object_id
),但这有效:
<%= render :template => "users/show", :locals => { :user => @user = @current_user } %>
我认为这不是一个好方法 “覆盖”@user
变量。这是因为,例如,如果我需要在上述“render”语句之后调用 @user
,它将不再起作用。
那么,渲染 show.html.erb
的解决方案是什么?
我也尝试过,
<%= render :template => "users/show", :locals => { @user => @current_user } %>
<%= render :template => "users/show", :locals => { :object => @current_user, :as => @user }
但这些不起作用。
更新
如果在pages_controller.rb
中我把它放在
def user
@user ||= @current_user
end
工作中并且在视图文件中你可以使用
<%= render :template => "users/show" %>
无论如何,我发现我有这个错误(有关更多信息,请参阅下面):
ActionController::RoutingError in Pages#user
No route matches {:action=>"destroy", :controller=>"users"}
错误是由位于从 show.html.erb
加载的部分中的此 form 语句生成的:
<%= form_for(@user, :url => user_path) do |f| %>
...
<% end %>
I am running Ruby on Rails 3 and I would like to render a template (show.html.erb
) passing a local variable.
In RAILS_ROOT/views/users/show.html.erb
I have
Name: <%= @user.name %>
Surname: <%= @user.surname %>
I have also a page controller to handle pages and in the application_controller.rb
an istance of @current_user
. A page is called user
, so in RAILS_ROOT/views/pages/user.html.erb
I have
<%= render :template => "users/show", :locals => { :user => @current_user } %>
The above code doesn't work (I get this error: RuntimeError in Pages#user, Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
) but this works:
<%= render :template => "users/show", :locals => { :user => @user = @current_user } %>
I think it is not a good approach to "overwrite" the @user
variable. This is because, for example, if I need to recall @user
after the above 'render' statement it will don't work anymore.
So, what is a solution in order to render show.html.erb
?
I tryed also
<%= render :template => "users/show", :locals => { @user => @current_user } %>
<%= render :template => "users/show", :locals => { :object => @current_user, :as => @user }
but those don't work.
UPDATE
If in pages_controller.rb
I put this
def user
@user ||= @current_user
end
it will work and in the view files you can just use
<%= render :template => "users/show" %>
Anyway, I discoverd that I have this error (see below for more info):
ActionController::RoutingError in Pages#user
No route matches {:action=>"destroy", :controller=>"users"}
The error is generated from this form statement located in a partial loaded from show.html.erb
:
<%= form_for(@user, :url => user_path) do |f| %>
...
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在模板中,
局部变量是局部的,因此不需要
@
来引用它们。@user502052
您可以从控制器显式渲染视图。
当您不在控制器中执行渲染时,框架会使用默认参数为您执行渲染。当你这样做时,你可以指定不同的模板文件,传递局部变量,渲染部分:任何
render
函数支持的东西。and in template
Local variables are local, so you don't need
@
to refer them.@user502052
You can render view explicitly from your controller.
When you don't execute
render
in controller, framework does that for you with default parameters. When you do, you can specify different template file, pass local variables, render a partial: anythingrender
function supports.以防万一您不渲染部分,并且不想使用 :template 选项,这也可以在 Rails 4 中通过以下方式完成:
Just in case you are NOT rendering a partial, and do not want to use :template option, this can also be done in Rails 4 with:
Url:
这将调用pages_controller上的用户方法。你有这样的:
Rails 创建一个实例变量@users,默认情况下将尝试在 app/views/pages/user.html.erb 处渲染视图模板。如果这不是你想要的,你必须在控制器中这样说:
现在将使用名为 user 的本地变量而不是默认的 app/views/pages/user 来渲染 app/views/users/show.html.erb 。 html.erb。
目前,您正在等待,直到进入视图模板,然后要求渲染另一个视图模板。一旦进入视图模板,您应该只需要渲染部分模板:
希望这有助于澄清。
Url:
this will call the user method on pages_controller. You have this:
Rails creates an instance variable @users and by default will try to render a view template at app/views/pages/user.html.erb. If that is not what you want, you have to say so in the controller:
This will now render app/views/users/show.html.erb with a local variable called user instead of the default app/views/pages/user.html.erb.
Currently you are waiting until you are inside a view template and then asking to render another view template. Once you are in a view template you should only need to render partial templates:
Hopefully that helps clarify.