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