Rails Basics
Basics Topics
– DRY Don’t repeat yourself Rails Before it, you should create workspace and move there. To fix dependency my myself Not create Active Record file –skip-test-unit Move to application root folder, and start WEBrick server. You can static file(.html etc..) under applicationroot/public/xxx Please access http://localhost:3000/hello-world.html Let’s create a controller The following files are created Start application and access http://localhost:3000/example/test Rails knows that for the URL path. The current routings can be listed wit the command Use <%= %> This is output. If missing =, this is only Ruby code app/views/layouts/application.html.erb is common layout This part is a part to insert each html.erb Use instance variable Insert page in .erb. The file name must start with an underscore _ _footer.html.erb is file name The location is same directory in this case. Use symbol This is also fine. This code checks existance Redirect the page you have just been Common task in a view.
– Refactoring
Create new Application
rails new applicationame
Create new Application with MySQL
rails new applicationname -d mysql
Create new application without bundle install
ruby new applicationname --skip-bundle
Create new Application without Database
rails new
Create Application without UnitTest
rails new project -d mysql --skip-test-unit
Start Application
rails s
Stop Application
Ctrl + C
Static file
This is testing.
hello-world.html
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<h1>HelloWorld</h1>
</body>
</html>
You can see web page contents of hello-world.htmlCreate HTML dynamically with erb(controller)
rails generate controller Example test
Views are under app/views
example/test.html.erb was created.
You can see this file.
Controllers are under app/controllers/
The name is example_controller.rbRoute Check
rake routes
Insert Ruby code in erb
<h1>Example#test</h1>
<%= 1 + 1 %>
<p>Find me in app/views/example/test.html.erb</p>
Layout
In this file, there is following code
<%= yield %>
Passing Instance Variables from a Controller to View
controller example
class ExampleController < ApplicationController
def test
@current_time = Time.now
end
end
[/ruby]
View example
[ruby]
Current time: <%=@current_time %>
Partials
How to insert?
<%= render "footer" %>
To write path correctly, you can use partials in any places, for example, under app/views/layouts
etc…How to pass parameter to Partials
<%= render "footer", :start_year => '2000' %>
<%= "#{start_year}-" if defined? start_year %>
More
<%= render :partial=>"footer",:locals => {:start_year => '2000'} %>
Redirect
class RedirectController < ApplicationController
def a
redirect_to redirect_b_path
end
def b
end
end
[/ruby]
Please check rake routes
<pre>
redirect_a GET /redirect/a(.:format)
redirect_b GET /redirect/b(.:format)
</pre>
Following is equivalent for it
[ruby]
redirect_to :action => 'b'
Back
redirect_to :back
Helper
It must be under app/helpers
I’ve read a few good stuff here. Certainly worth bookmarking for revisiting. I surprise how much attempt you set to make such a excellent informative site.