ruby 将局部变量从控制器传递给视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25730570/
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-06 06:34:12  来源:igfitidea点击:

Passing local variables to a view from controller

rubyruby-on-rails-3ruby-on-rails-4viewcontroller

提问by user3442206

I am not able for some reason to pass local variables to the show view...

由于某种原因,我无法将局部变量传递给显示视图...

In my controller i have simply:

在我的控制器中,我只有:

def show
  render template: "books/show", :resource => "Some text"
end

In my view i print the following:

在我看来,我打印以下内容:

<h1>My local variable text: <%= resource %></h1>

And i am getting the following message:

我收到以下消息:

undefined local variable or method `resource' for #<#<Class:0x00000118ebce90>:0x00000118ec3498>

I've tried the following syntaxes in the controller:

我在控制器中尝试了以下语法:

render template: "books/show", locals: { resource: "Some text" }
render template: "books/show", locals: { resource => "Some text" }
render template: "books/show", :locals => { resource: "Some text" }
render template: "books/show", :locals => { resource => "Some text" }

Without any luck...

没有运气...

Any clues ?

任何线索?

Thanks!

谢谢!

回答by Naresh Kumar P

Passing Data from the Controller to the View

将数据从控制器传递到视图

Here's a NovelController class, to be put into app/ controllers/novel_controller.rb.

这是一个 NovelController 类,要放入 app/ controllers/novel_controller.rb.

class NovelController < ApplicationController
      def index
        @title = 'Shattered View: A Novel on Rails'
      end
end

Since this is the Novel controller and the index action, the corresponding view is in app/views/novel/index.html.erb

由于这是Novel控制器和索引动作,对应的视图在 app/views/novel/index.html.erb

<h1><%= @title %></h1>

Output:

输出:

Shattered View: A Novel on Rails

The view is interpreted after NovelController#indexis run. Here's what the view can and can't access:

视图在NovelController#index运行后被解释。以下是视图可以访问和不能访问的内容:

  • It can access the instance variables @title, because they've been defined on the NovelControllerobject by the time NovelController#indexfinishes running.
  • It can call instance methods of the instance variables @title.
  • 它可以访问实例变量@title,因为它们在完成运行NovelController时已在对象上定义NovelController#index
  • 它可以调用实例变量的实例方法@title

回答by Pradnya Kulkarni - Telang

I think it should be like

我认为它应该像

render 'books/show', :locals => {:resource => 'Some text'}

render 'books/show', :locals => {:resource => 'Some text'}

It works for me

这个对我有用

回答by silverdr

First I am wondering why would you need the template:. Are you on Rails 2.x? If not, then the :templateoption is no longer required. You should be able to get along fine with just

首先我想知道你为什么需要template:. 你在 Rails 2.x 上吗?如果不是,则:template不再需要该选项。你应该能够相处得很好

render "books/show"

render "books/show"

Second do you need to specify the template? What is the controller you want to render from? If that's BooksController, then you don't need the template path either, which makes the line being just

其次需要指定模板吗?你想要渲染的控制器是什么?如果是BooksController,那么您也不需要模板路径,这使得该行只是

render

render

That's without the variables yet. Now, I just checked, and a simple:

那还没有变量。现在,我刚刚检查了一个简单的:

render locals: { resource: "Some text" }

render locals: { resource: "Some text" }

as well as:

也:

render 'books/show', locals: { resource: "Some text" }

render 'books/show', locals: { resource: "Some text" }

works for me just fine. Maybe earlier Rails versions treated 'resource' as some kind of a keyword? Don't know, but the above Worksforme? in both forms.

对我来说很好。也许较早的 Rails 版本将“资源”视为某种关键字?不知道,但是上面的Worksforme?两种形式。

回答by Ryan Bigg

You can't pass local variables from a controller to a view. What you could do is define it as an instance variable and then it would automatically be available in the view.

您不能将局部变量从控制器传递到视图。你可以做的是将它定义为一个实例变量,然后它会自动在视图中可用。

@resource = @book

If you want to pass entirely different objects, then just define those instance variables differently.

如果你想传递完全不同的对象,那么只需以不同的方式定义这些实例变量。

回答by pramod

You can get it as follows. All gives the same result. Here is the referencelink.

您可以通过以下方式获取。都给出了相同的结果。这是参考链接。

render action: :show , locals: { resource: "Some text"}
render :show , locals: { resource: "Some text"}
render locals: { resource: "Some text"}