ruby小白课程实战--宠物论坛

【Ruby on Rails实战】3.9 发布(创建)帖子

2019-01-09  本文已影响1人  808cb3be8e58

1、创建数据库表格

(1)创建posts表来保存贴子信息,表结构如下:
字段名 字段说明 字段类型 备注
head 标题 string 必填
body 内容 text 必填
account_id 用户id integer
as_type 类型 integer 0普通贴子 1精华帖 2置顶帖
status 状态 integer 0正常 1已删除
(2)在项目命令行下执行rails g model命令创建post数据表映射文件,再执行rake db:migrate将映射文件映射到数据库中
#创建映射文件
/vagrant/data_symtem$ rails g model post head:string body:text account_id:integer as_type:integer status:integer
#系统返回信息
    create    db/migrate/20181012111922_create_posts.rb
    create    app/models/post.rb
    invoke    test_unit
    create      test/models/post_test.rb
    create      test/fixtures/posts.yml
#将db/migrate/20181012111922_create_posts.rb映射文件映射到数据库中,创建post数据表
/vagrant/data_symtem$ rake db:migrate
#系统返回信息
  == 20181012111922 CreatePosts: migrating ======================================
  -- create_table(:posts)
     -> 0.0136s
  == 20181012111922 CreatePosts: migrated (0.0151s) =============================
(3)运行rails g controller posts new create 创建posts_controller.rb文件,posts_controller.rb文件文件中new、create实例方法,views目录下每个实例方法对应的html文件、routes.rb文件中每个实例方法对应的路由。
/vagrant/data_system$ rails g controller posts new create
#系统返回信息
     create  app/controllers/posts_controller.rb
     route  get 'posts/create'
     route  get 'posts/new'
     invoke  erb
     exist    app/views/posts
     create    app/views/posts/new.html.erb
     create    app/views/posts/create.html.erb
     invoke  test_unit
     create    test/controllers/posts_controller_test.rb
     invoke  helper
     create    app/helpers/posts_helper.rb
     invoke    test_unit
     invoke  assets
     invoke    coffee
     create      app/assets/javascripts/posts.coffee
     invoke    scss
     create      app/assets/stylesheets/posts.scss
(4)修改routes.rb文件
#原代码
get 'posts/create'
#改为
post 'posts/create'

代码解析:

2、显示创建帖子的页面

(1)在posts_controller.rb文件中添加上before_action,基本上所有controller都要在before_action时加上check_login方法,这样每次执行action方法之前都会check_login方法,用来检查当前的登陆用户。
#参考代码,无需粘贴
#class PostsController < ApplicationController
before_action :check_login
(2)添加样式,在app/assets/stylesheets/posts.scss中添加下列代码
.new-issue-form-container {
  width: 800px;
  background: white;
  margin: 30px auto;
  .new-issue-form {
    width: 600px;
    margin: 10px auto;
  }
}

.submit-issue-button {
  height: 50px;
  width: 100%;
}
(3)打开views/posts/new.html.erb文件,删掉现有内容,粘贴下面内容,显示标题框、内容框:
<div class="new-issue-form-container clearfix">
  <div class="new-issue-form clearfix">
    <%= form_for Post.new,url: "/posts/create" do |f| %>
      <%= f.hidden_field :account_id %>
      <dl class="form">
        <dt><%= f.label "标题" %></dt>
        <dd><%= f.text_field :head %></dd>
      </dl>
      <dl class="form">
        <dt><%= f.label "内容" %></dt>
        <dd><%= f.text_area :body %></dd>
      </dl>
      <input type="submit" value="发布" class="submit-issue-button btn btn-primary">
    <% end %>
  </div>
</div>

代码解析:

(4)编辑views/home/index.html.erb文件,加上/posts/new链接。这样我们在网站首页点击「发布新帖」按钮,就能进入到创建帖子页面
<!--原代码-->
<%= link_to "发布新帖", "#", class: "banner-btn btn" %>
<!--改为-->
<%= link_to "发布新帖", "/posts/new", class: "banner-btn btn" %>
(5)rails s启动项目,登录账号,点击首页「发布新帖」按钮,我们看一下效果:

3、编辑posts_controller.rb文件中的create方法。点击发布按钮,我们会将new页面中的填写的信息,以params哈希的形式提交到 "/posts/create”对应的posts_controller.rb中的create方法。

(1)创建帖子功能描述

(1)标题不能为空,内容不能少于8个字
(2)发布成功后重定向到根目录,未成功render到new页面

(2)编辑posts_controller.rb文件中的create方法,处理从new页面传过来的params数据,并存到数据库中
def create
  head = params[:post][:head]
  body = params[:post][:body].strip
  if head.blank?
    flash.notice = "标题不能为空"
  elsif body.length < 8
    flash.notice = "内容长度不能少于8个字"
  else
    post = Post.new(account_id: @current_user.id,as_type:0,status:0)
    post.head = head
    post.body = body
    boolean = post.save
    if boolean
      flash.notice = "发布成功"
      redirect_to :root
    else
      flash.notice = "发布失败,请重新发布"
      render "/posts/new"
    end
  end    
end
(3)最后,我们来测试一下,填写标题以及不少于8个字的内容,点击发布按钮发布帖子,发布成功后,在ctrl_c停止项目,再输入rails c进入控制台。输入Post.all查看Post对象是否正确保存了。
#进入控制台
/vagrant/data_system$ rails c
#查看创建的所有帖子
irb(main):001:0> Post.all
=> #<ActiveRecord::Relation [#<Post id: 1, head: "第一个帖子", body: "欢迎来到宠物之家论坛", account_id: 1, as_type: 0, status: 0, created_at: "2018-10-15 07:03:57", updated_at: "2018-10-15 07:03:57">]>

有结果,说明保存成功了。下一长我们会在网站主页面将创建的帖子显示出来。

上一篇 下一篇

猜你喜欢

热点阅读