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.rbclass 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 < ApplicationControllerbefore_action :get_by_id,:only =>[:show,:destroy,:edit,:update]def index@blogs = Blogputs "--------------------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"enddef new@blog = Blog.new@blog.title = params[:blog_title]@blog.time = params[:blog_time]@blog.content = params[:blog_content]@blog.save!enddef createputs("==params is: ")blog.create( { :time => params[:time], :title=> params[:title], :content => params[:content] } )redirect_to blogs_pathenddef editenddef 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_pathenddef showputs "==== im show"enddef destroy@blog.deleteredirect_to blogs_pathendprivatedef 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 docollection doget :listendendnamespace :interface doresources :blogs docollection doget :allendendendend
增加文件
app/controllers/interface/blogs_controller.rb
module Interfaceclass BlogsController < ApplicationControllerdef 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}endrender json: resultendend
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.
blogs rails crud相关推荐
- 小狗钱钱_✅每次构建待办事项列表应用程序时,都会有一只小狗? 死了?
小狗钱钱 by Hrishi Mittal 由Hrishi Mittal ✅每次构建待办事项列表应用程序时,都会有一只小狗 ? 死了? (✅ Every time you build a to-do ...
- 软件欺诈的骗局揭露:“替罪羊”究竟是如何构建的?
创建一个能工作的软件产品是很困难的,卖欺诈性的软件产品要容易得多. 声明:本文已获作者Matt Stancliff翻译授权. 作者 | Matt Stancliff 译者 | 苏本如,责编 | 郭芮 ...
- Ruby on Rails 和 J2EE:两者能否共存?
Ruby on Rails 和 J2EE:两者能否共存? 两个 Web 应用程序框架的比较 文档选项 <tr valign="top"><td width=&qu ...
- Rails + rabl
当我们使用rails generate scaffold的方式生成MVC的时候,rails会自动给我们生成一系列的文件,包括了如何用json显示model的view.这样其实默认了你的系统是一个CRU ...
- 使用jquery-easyui写的CRUD插件(2)
首先定义变量 代码var options = jQuery.extend({},jQuery.fn.crudUIGrid.defaults, options);var addTypeName = op ...
- [ruby on rails] 跟我学之(6)显示指定数据
根据<[ruby on rails] 跟我学之路由映射>,我们知道,可以访问 GET /posts/:id(.:format) 来显示具体的对象. 1. 修改action 修改 ap ...
- 监视Rails进程内存泄漏的技巧
Rails应用比较容易遇到的两类性能问题:一类是Rails执行很慢,CPU消耗过高:另一类是Rails进程内存泄漏.解决这两类问题都需要你首先能够精确定位出现问题的代码,然后才知道如何对症下药. 一. ...
- crud-table_我个人的CRUD故事-或我如何来到CUBA平台
crud-table 在此博客文章中,我想介绍一下我如何使用CUBA平台以及该工具的好处. 在我年轻的"业务应用程序开发"历史上,我将深入探讨各个阶段,只是给您一些背景知识. 因此 ...
- mybatis crud_MyBatis教程– CRUD操作和映射关系–第1部分
mybatis crud CRUD操作 MyBatis是一个SQL Mapper工具,与直接使用JDBC相比,它极大地简化了数据库编程. 步骤1:创建一个Maven项目并配置MyBatis依赖项. & ...
最新文章
- android 模拟器配置上网_10 款主机模拟器,让你畅玩全球大作,嗨到飞起
- sql parser
- MYSQL 取中位数
- Node.js 框架
- ssh能够连接而sftp不能连接的解决方法
- 如何系统学习python
- html5游戏制作总结
- 卢伟冰:Redmi K30会支持全网通5G 雷军:必须的!
- Yeelink初步体验
- on where having总结
- [Python]ImportError: This platform lacks a functioning sem_open implementation, therefore, the requi
- VS2019的C++项目如何查看源文件(.h,.cc.cpp等)所在的工程
- 当年南大小百合上拍古典诗词版版主的
- 大数据(3i)Sqoop安装和操作
- PandoraBox Openwrt 上面设置DNS
- 数据挖掘项目之Airbnb 新用户的民宿预定结果预测
- 单片机芯片解密OTP存储器加密
- 逻辑卷(lvm)的配置与管理
- 如何建立一个属于自己的网站(小白教程)
- WebGIS理论知识(七)—走进互联网地图