Part 4
Create a piece of our own article! Before I did not design this simple project structure, I created a controller called the blog, now found that I do not need this, I currently only need a article model, and a welcome interface, the Welcome interface will show all the articles and some of the content, However, if the article is too long is not displayed, and the article list is only the title of all articles, will not show anything, whether in the Welcome screen or the article List page, click on the title of the article will show the full article content. Ok, start!
-First delete the blog I created earlier this controller
Rails Destroy Controller Blogs
And then create the welcome we need this controller, to tell the truth, I do not know what the first page should use the name, temporarily with welcome bar.
Rails Generate Controller Welcome
Still have to create an empty index method and view inside of the Index.html.erb file. But how did the article come about? First you have to create your own Ah!
-Create article this model, which contains the simple title and content of the two properties to meet the current needs of
Rails Generate model article title:string Content:text
Then you need to generate a database table and do a rake db:migrate operation.
-Generate data. We can manually create some content into the database. Use Rails c to enter the console. And then insert the data
A = Article.newa.title = "First article" a.content = "This is the content of the first article"
In this way, the first article is created.
-Retrieve all the articles in the Welcome Index method
def index@articles = Article.allend
Add in Index.html.erb in the Welcome directory in view
<p> First article: <%= @articles. First.title%><br> content: <%= @articles. first.content%></p>
OK, refresh the page again to see the first data we just created.
Part 5
The next step is to beautify this page, to create a slightly better spectacle.
-Add CSS for each article's overall shape in the application.css in the stylesheets directory
. article_content { border:1px solid #dddddd; border-radius:5px; Background-color: #ffffff; margin-top:10px; padding:5px 20px 5px 20px;}
And then you can use the ". Article_content" as the Div class in the index.html.erb.
<div class= "Article_content" >
Below we want to click on the title of the article to display the full text. Since there is a jump between pages, then the route is necessary, in the CONFIG/ROUTES.RB add article's restful routeResources:articles
If you do not know which routes were generated at this point, you can use rake routes in the terminal to display all the routing information
Then change the contents of our welcome Index.html.erb file to
<% @articles. Each do |article| %><div class= "article_content" >
According to the rules of routing, we need to have article this controller, and it must implement the Show method and add Show.html.erb file
Rails Generate Controller Articles
Article this controller's Show method
def show@article = Article.find (Params[:id]) end
Article contents of the Show.html.erb file<div class= "Article_content" >
-Add the "Blog" above the navigation bar"," List of articles "," About bloggers "column link <a class= "Navbar-brand" href= "/" >blog</a><li class= "active" ><a href= "articles" > article List </a ></li><li><a href= "About" > About Bo Master </a></li>
Here the About page will not find the route, I temporarily put it in the welcome inside, so in welcome add about method
def aboutend
Then add a About.html.erb file below the Welcome subdirectory of the view. Finally add a route to the routes
Get '/about ' = ' welcome#about '
To this, a basic operation of the blog appears, the next is to create a article add curd inside the missing "CUD" method and the page further landscaping.
Rails + Bootstrap personal blog Building complete process (2)