渲染传递局部变量的模板时出现问题 [英] Trouble on rendering a template passing a local variable

查看:32
本文介绍了渲染传递局部变量的模板时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Ruby on Rails 3,我想渲染一个传递局部变量的模板 (show.html.erb).

I am running Ruby on Rails 3 and I would like to render a template (show.html.erb) passing a local variable.

RAILS_ROOT/views/users/show.html.erb我有

Name: <%= @user.name %>
Surname: <%= @user.surname %>

我还有一个页面控制器来处理页面,并且在 application_controller.rb 中有一个 @current_user 实例.一个页面被称为 user,所以在 RAILS_ROOT/views/pages/user.html.erb 我有

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 } %>

上面的代码不起作用(我得到这个错误:RuntimeError in Pages#user, Called id for nil,这会错误地为 4 -- 如果你真的想要 nil 的 id,请使用 object_id),但这有效:

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 } %>

我认为覆盖"@user 变量不是一个好方法.这是因为,例如,如果我需要在上述 'render' 语句之后调用 @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.

那么,为了呈现show.html.erb,有什么解决方案?

So, what is a solution in order to render 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

它会起作用,您可以在视图文件中使用

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"}

错误是从位于从 show.html.erb 加载的部分中的这个 form 语句生成的:

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 %>

推荐答案

:locals => { :user => @current_user }

在模板中

Name: <%= user.name %>

局部变量是局部的,所以你不需要@来引用它们.

Local variables are local, so you don't need @ to refer them.

@user502052
您可以从控制器显式渲染视图.

@user502052
You can render view explicitly from your controller.

render :template => "users/show", :locals => {...}

当您不在控制器中执行 render 时,框架会使用默认参数为您执行此操作.当你这样做时,你可以指定不同的模板文件,传递局部变量,渲染部分:render 函数支持的任何东西.

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: anything render function supports.

这篇关于渲染传递局部变量的模板时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆