Ruby Rail First Step: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 78: | Line 78: | ||
<%= link_to 'Back', posts_path %> | <%= link_to 'Back', posts_path %> | ||
<pre> | <pre> | ||
* render 'form' is a partials. A code which can be reused | |||
===Form === | |||
* app/views/posts/_form.html.erb | |||
<pre> | |||
<%= form_for(@post) do |f| %> | |||
<% if @post.errors.any? %> | |||
<div id="error_explanation"> | |||
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> | |||
<ul> | |||
<% @post.errors.full_messages.each do |message| %> | |||
<li><%= message %></li> | |||
<% end %> | |||
</ul> | |||
</div> | |||
<% end %> | |||
<div class="field"> | |||
<%= f.label :name %><br> | |||
<%= f.text_field :name %> | |||
</div> | |||
<div class="field"> | |||
<%= f.label :title %><br> | |||
<%= f.text_field :title %> | |||
</div> | |||
<div class="field"> | |||
<%= f.label :content %><br> | |||
<%= f.text_area :content %> | |||
</div> | |||
<div class="actions"> | |||
<%= f.submit %> | |||
</div> | |||
<% end %> | |||
</pre> | |||
Revision as of 00:02, 15 November 2014
- Your first application with MySQL
Create the Application
rails new firstapp -d mysql
- Move into the application directory
cd firstapp
- If you setup MySQL or Postgres with a username/password, modify the
- config/database.yml file to contain the username/password that you specified
- Create the database
rake db:create
- Start the server
rails server
- Visite the web site
- change the default page
Add the following into config/routes.rb
root :to => "home#index"
Start to Work with Resources
Generate a resource (Post)
rails generate scaffold Post name:string title:string content:text
Creating and Customizing Rails Generators & Templates
Create Tables (posts)
rake db:migrate
- To create in production
rake db:migrate RAILS_ENV=production.
Add a link into the home page
- Add Post Page in the index file app/view/home/index.html.erb
<%= link_to "My Blog", posts_path %>
Add validation
- Add validation into app/modles/post.rb
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
end
Create an Xml List
- Modify the sectioni def index of the file app/controllers/posts_controller.rb
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
- Now try http://localhost:3000/posts.xml
- More information at Active Record Query Interface
Creation Process
New Post Request
- the form is empty but has the same feild a the one used for modification
- Inside the file app/controllers/posts_controller.rb
def new
@post = Post.new
end
Empty Form
- app/view/new.html.erb
<h1>New post</h1>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
<pre>
* render 'form' is a partials. A code which can be reused
===Form ===
* app/views/posts/_form.html.erb
<pre>
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>