Ruby-on-rails 如何在 Rails 中的 VIews 模板中创建和使用变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15171850/
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
How to create and use variable in VIews Template in Rails ?
提问by iCyborg
I am still new to ruby and rails and is looking to create a variable so I can use it over and over again in the views template. For example,my code right now is
我还是 ruby 和 rails 的新手,正在寻找创建一个变量,以便我可以在视图模板中一遍又一遍地使用它。例如,我现在的代码是
<title>Home Page</title>
<h3>Welcome to my Home Page</h3>
Now I want to make this "Home Page" as variable or symbol so I can just use that variable/symbol rather than typing the string over and over, how to do it ?
现在我想把这个“主页”作为变量或符号,这样我就可以使用那个变量/符号而不是一遍又一遍地输入字符串,怎么做?
Thanks
谢谢
回答by Austin Mullins
When I first read your question, I thought you were asking for this, but I realize this is different.
当我第一次阅读你的问题时,我以为你在问这个,但我意识到这是不同的。
Michael Hartl's amazing Ruby-on-Rails Tutorialdemonstrates my favorite method for doing this, which is to create an instance variable that gets referenced in the layout exactly the way you want.
Michael Hartl 令人惊叹的Ruby-on-Rails 教程演示了我最喜欢的方法,即创建一个实例变量,在布局中完全按照您想要的方式引用。
rails_root/app/controllers/application_controller.rb
rails_root/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
attr_accessor :extra_title
...
That makes @extra_titleaccessible in all controllers. Now inside one particular controller:
这使得@extra_title所有控制器都可以访问。现在在一个特定的控制器中:
rails_root/app/controllers/things_controller.rb
rails_root/app/controllers/things_controller.rb
class ThingsController < ApplicationController
def index
@extra_title = "| Things"
...
Ok, so what is this all for? Oh right, we wanted to use this in a layout:
好吧,这一切是为了什么?哦对了,我们想在布局中使用它:
rails_root/app/views/layouts/application.html.erb
rails_root/app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Best. App. Ever. <%= @extra_title %></title>
...
And now you're riding the Rails.
现在您正在使用 Rails。
回答by My God
You can use an instance variable prefixed by @so that it can be used throughout your view.
您可以使用实例变量,prefixed by @以便它可以在您的整个视图中使用。
For example:
例如:
Controller:
控制器:
@my_home = "Home Page"
View:
看法:
<title><%= @my_home %></title>
回答by rorra
In order to do that, you use either a layout or a partial. There is a nice guide here: http://guides.rubyonrails.org/layouts_and_rendering.html
为此,您可以使用布局或部分。这里有一个很好的指南:http: //guides.rubyonrails.org/layouts_and_rendering.html
You can put that content in the file app/views/layout/application.html.erb, and it will be used as your default layout.
您可以将该内容放在文件 app/views/layout/application.html.erb 中,它将用作您的默认布局。
The other thing you can do, is to create a partial, so you can create a file like /app/views/shared/_header.html.erb with that content, and then you can render it from any view by writting
您可以做的另一件事是创建一个部分,这样您就可以使用该内容创建一个像 /app/views/shared/_header.html.erb 这样的文件,然后您可以通过编写从任何视图呈现它
render partial: '/shared/header.html.erb'

