Rails Scaffolding (Scaffold) feature

Source: Internet
Author: User

Scaffold is a fast-developing code framework for rails applications that enables CRUD operations with a single command.

1: Create an app
cd scaffoldapp rails s

Open Http://localhost:3000/in the browser

2: Create a scaffold named blog

Use Rails's scaffold to create templates that add, delete, modify, query (CRUD) operations.

rails g scaffold blog title:string content:textpicture:string

Database migration:

rakedb:migrate

Browser access: Http://localhost:3000/blogs

3: Add image upload function

can refer to my previous blog in rails to upload images using Carrierwave
Update Gemfile

‘carrierwavegenerate uploader Picture

Modify/APP/MODELS/BLOG.RB to mount picture properties

:picturePictureUploader

Open App/views/ideas/_form.html.erb and find this line:

<%= f.text_field :picture%>

Change it to:

<%= f.file_field :picture%>

And put this line:

<%= form_for(@blogdo%>

Change to:

<%= form_for(@blog,:html => {:multiparttruedo |f| %>

Open the App/views/ideas/show.html.erb and

<%=@blog.picture%>

Switch

<%= image_tag(@blog300if@blog.picture%>
4. Modify the Style

Open/APP/CONTROLLERS/BLOGS_CONTROLLER.RB
Create a new method

def list     @blogsBlog.all  end

Create a new List.html.erb under the/app/views/blogs/directory

<div class="Bgheader">    <H1>My Blog</H1></div><% @blogs.  each  do |blog| %>      <h2 class="Bgtitle"><%=link_to blog.title,blog%></H2>      <p><%= blog.content[0,]%></P><% End %>

Modify/CONFIG/ROUTES.RB, add:

‘blogs#list‘

Increase/app/assets/stylesheets/application.css

Body{  padding: 0px;     margin:0px;     width: +px;  margin:0 auto; }. Bgheader{  margin-top: -px;     height: px;  background-color: #E9F2E8; }. Bgheader H1{  color: #238A2A;     padding-top: px;  padding-left: px; }. Bgtitle a: Link,. Bgtitle a: Visited{  color: #0080FF;} #blog_title{  width:px;  height:px; }#blog_content{  width:480px;  height:px; }. Bgcontainer{  width: +px;} . Bgshow{  width: +px;} 

Modify/app/views/blogs/show.html.erb

<div class="Bgshow"><p id="notice"><%= Notice%></P><h2>  <%= @blog. Title%></H2>  <%= @blog. Content%><hr>  <%= Image_tag (@blog. Picture_url,: width = ) if @blog. picture.present?%><hr><%= link_to ' Edit ', Edit_blog_path (@blog)%> |<%= link_to ' back ', Blogs_path%> </div>
Effect

Home

Details page

Rails Scaffolding (Scaffold) feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.