Ruby-on-rails 渲染传递局部变量的模板时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5020553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:26:53  来源:igfitidea点击:

Trouble on rendering a template passing a local variable

ruby-on-railsrubytemplatesruby-on-rails-3rendering

提问by user502052

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

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

In RAILS_ROOT/views/users/show.html.erbI have

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

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

I have also a page controller to handle pages and in the application_controller.rban istance of @current_user. A page is called user, so in RAILS_ROOT/views/pages/user.html.erbI have

我也有一个页面控制器来处理页面中和application_controller.rb的istance @current_user。一个页面被调用user,所以在RAILS_ROOT/views/pages/user.html.erb我有

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

上面的代码不起作用(我收到此错误RuntimeError in Pages#user, Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id:),但这有效:

<%= render :template => "users/show", :locals => { :user => @user = @current_user } %>

I think it is not a good approachto "overwrite" the @uservariable. This is because, for example, if I need to recall @userafter the above 'render' statement it will don't work anymore.

我认为这不是“覆盖”@user变量的好方法。这是因为,例如,如果我需要@user在上面的 'render' 语句之后回忆起来,它将不再起作用。

So, what is a solution in order to render show.html.erb?

那么,什么是渲染的解决方案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.rbI put this

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

The error is generated from this formstatement located in a partial loaded from show.html.erb:

错误是从位于部分加载的以下表单语句中生成的show.html.erb

<%= form_for(@user, :url => user_path) do |f| %>
  ...
<% end %>

回答by Nikita Rybak

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

and in template

并在模板中

Name: <%= user.name %>

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

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

@user502052
You can render view explicitly from your controller.

@user502052
您可以从控制器显式呈现视图。

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

When you don't execute renderin 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 renderfunction supports.

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

回答by Vlad

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:

以防万一您不渲染部分,并且不想使用 :template 选项,这也可以在 Rails 4 中完成:

render 'users/show', {user: @current_user}

回答by mikewilliamson

Url:

网址:

http://localhost/pages/user

this will call the user method on pages_controller. You have this:

这将调用 pages_controller 上的用户方法。你有这个:

def user
  @user ||= @current_user
end

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:

Rails 创建了一个实例变量@users,默认情况下会尝试在 app/views/pages/user.html.erb 中渲染一个视图模板。如果这不是你想要的,你必须在控制器中这么说:

def user
  @user ||= @current_user
  render :template => "users/show", :locals => { :user => @current_user }
end

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.

现在,这将使用名为 user 的局部变量呈现 app/views/users/show.html.erb,而不是默认的 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:

目前您正在等待,直到您进入视图模板,然后要求呈现另一个视图模板。进入视图模板后,您应该只需要呈现部分模板:

render :partial => 'users/show', :locals => {:user => @current_user}

Hopefully that helps clarify.

希望这有助于澄清。