blogs rails crud

使用rails创建一个新的项目,blogs 在远程也创建项目,进行git init .创建连接

首先,使用scaffold 直接创建表格和列

bundle exec rails genreate scaffold blogs title:string content:string

创建数据表

bundle exec rake db:migrate

修改环境文件

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  #注释下边这一行
  #protect_from_forgery with: :exception
end

修改路由

class BlogsController < ApplicationController

  before_action :get_by_id,:only =>[:show,:destroy,:edit,:update]

  def index
    @blogs = Blog
    puts "--------------------hihih"
    @blogs = @blogs.where('name like ? ', "%#{params[:blog_title]}%") if params[:blog_title].present?
    @blogs = @blogs.where('address like ? ', "%#{params[:blog_content]}%") if params[:blog_content].present?
    @blogs = @blogs.page(params[:page]).per(30)

    puts "-------end--index-----------hihih"
  end

  def new
    @blog = Blog.new
    @blog.title = params[:blog_title]
    @blog.time = params[:blog_time]
    @blog.content = params[:blog_content]
    @blog.save!

  end

  def create
    puts("==params is: ")
    blog.create(  { :time => params[:time], :title=> params[:title], :content => params[:content] }  )
    redirect_to blogs_path
  end

  def edit
  end

  def update
    #@blog = Blog.find(params[:id])
    puts "=== in update, before : #{@blog.inspect}"
    @blog.title = params[:blog_title]
    @blog.time = params[:blog_time]
    @blog.content = params[:blog_content]
    @blog.save!
    puts "=== in update, after: #{@blog.inspect}"
    redirect_to blogs_path

  end

  def show
    puts "==== im show"
  end

  def destroy
    @blog.delete
    redirect_to blogs_path
  end

  private
  def get_by_id
    @blog = Blog.find(params[:id])
  end
end

渲染页面app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Blogs</title>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  </head>
  <body>
    <div> application.html.erb</div>
    <%= yield %>
  </body>
</html>

编辑页面 app/views/blogs/edit.html.erb

<h3>编辑</h3>
<%= render 'form', blog: @blog %>

<br/>
<%= link_to '详情', @blog %> |
<%= link_to '返回', @blog_path %>

form 页面app/views/blogs/_form.html.erb

<%= form_for @blog do |form| %>
  <teble class='table table-striped table-hover table-border'>
    <tr>
        <th style="width: 150px;">
            标题
        </th>
        <td> 
            <%= form.text_field :title,class: "form-control" %>
        </td>
    </tr>
    <tr>
        <th>
            content
        </th>
        <td> 
            <%= form.text_field : contenet,class: "form-control" %>
        </td>
    </tr>
<%= end %>

列表页面app/views/blogs/index.html.erb
 

<h3>列表</h3>

博客总数<%= @total_ count  %>
<br/>
<%= paginate @blogs %>

<table class="table table-striped">
  <% @blogs.each do |blog| %>
    <tr>
      <td >
        <a href="/blogs/<%= blog.id %>" > <%= blog.id %> <%= blog.title %> </a>
      </td>
      <td>
        <%=blog.title %>
      </td>
      <td>
        <%=blog.content %>
      </td>
      <td>
        <%= link_to "编辑", edit_blog_path(:id => blog.id) %>
        <%= link_to "删除",blog,:method => :delete, class: 'btn btn-primary' %>
      </td>
    </tr>
  <% end %>
</table>

<br/>

<%= link_to "新建", new_blog_path %><br/>

新建页面app/views/blogs/new.html.erb

<h3>新建页面</h3>
<%= render 'form', blog: @blog %>
<br/>
<%= link_to "返回",blogs_path %>

详情页面app/views/blogs/show.html.erb

<table class='table table-striped table-hover table-border'>
  <tr>
    <th style="width:150px;">
      标题
    </th>
    <td>
      <%= @blog.title %>
    </td>
    <td>
    </td>
  </tr>
  <tr>
    <th>
      内容
    </th>
    <td>
      <%= @blog.content %>
    </td>
    <td>
    </td>
  </tr>
</table>

<br/>
<%= link_to'返回',blogs_path, class: 'btn btn-primary' %>

使用hash  routes.rb

Rails.application.routes.draw do resources :blogs do
    collection do
      get :list
    end
  end

  namespace :interface do
    resources :blogs do
     collection do
      get :all
      end
    end
  end

end

增加文件

app/controllers/interface/blogs_controller.rb
module Interface
  class BlogsController < ApplicationController
    def all

      #blog = Blog.first
      #result = {
      #  id: blog.id,
      #  title: blog.title,
      #  content: blog.content
      #}

      result = Blog.all.map do |blog|

        # 这个是返回值,在map 中,不能写return, 默认返回最后一行代码的值
        {
          id: blog.id,
          title: blog.title,
          content: blog.content
        }
      end

      render json: result
    end
  end
end

使用分页Gemfile


source 'https://rubygems.org'


gem 'kaminari'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.8'
# Use sqlite3 as the database for Active Record
#gem 'sqlite3'
#gem 'mysql2'
#gem 'mysql2', '~> 0.3.18'
gem 'pg', '0.18.4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值