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
<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
local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not. Here you render a partial with some values, the headline and person will become accessible with predefined value.
Within the context of a layout, <%= yield %> identifies a section where content from the view template should be inserted. The simplest way to use this is to have a single yield, into which the entire contents of the view currently being rendered is inserted.
In the Ruby programming language, an instance variable is a type of variable which starts with an @ symbol. An instance variable is used as part of Object-Oriented Programming (OOP) to give objects their own private space to store data. We say that objects can: Do things.
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 Tutorial demonstrates my favorite method for doing this, which is to create an instance variable that gets referenced in the layout exactly the way you want.
class ApplicationController < ActionController::Base
protect_from_forgery
attr_accessor :extra_title
...
That makes @extra_title
accessible in all controllers. Now inside one particular controller:
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:
<!DOCTYPE html>
<html>
<head>
<title>Best. App. Ever. <%= @extra_title %></title>
...
And now you're riding the Rails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With