ruby on rails界面常用控件写法以及rails常用方法
2016-08-08 17:01
483 查看
在数据表中增加字段
带验证的输入框,在model中要加入验证
单选框 //性别选择
下拉框
创建分页方法
在model中添加如下代码
在Controller里这样写
最后在要显示的index.erb页面的最后加上下面代码
上传图片的方法,主要代码:
上传图片 script里面写下面的
控制器的create中添加
进入调试
修改文件
增加字段
查看路由
mongodb查询方式
class CreateTpoolUserTypes < ActiveRecord::Migration def change create_table :tpool_user_types , id: false do |t| t.string :id,limit: 30 t.string :utype_name, limit: 50 t.string :utype_desc, limit: 500 t.timestamps null: false end end end
带验证的输入框,在model中要加入验证
<%= form_for [@hukou] ,:html => { :class=>"form-horizontal" } do |f|%> <!--@hukou 是用户数据表名--> <div class="form-group"> <label for="hukou_name" class="col-sm-2 control-label">name</labe <div class="col-sm-6"> <%= f.text_area :name , cols: 0, rows: 1 %> </div> <div class="col-sm-3"> <%= error_div(@hukou, :name, "name") %> </div> </div> <%end%>
单选框 //性别选择
<%= radio_button_tag(:sex, "1") %> <%= label_tag(:age_child, "man") %> <%= radio_button_tag(:sex, "0") %> <%= label_tag(:age_adult, "women") %>
下拉框
<%= f.select :user_type, options_for_select([ ['管理员1',0], ['管理员2', 1], ['管理员3', 2], ['管理员4', 3] ])%>
创建分页方法
在model中添加如下代码
class Hukou < ActiveRecord::Base # 分页 def self.search(page) order('id desc ').paginate(page: page, per_page: 2) #id 为要查询的序列,在数据库中,一般是第一项,per_page表示每页现实的个数 end end
在Controller里这样写
def index @hukous = Hukou.search(params[:page]||1) end def update @page=params[:page] respond_to do |format| if @hukou.update(hukou_params) format.html { redirect_to @hukou, notice: 'Hukou was successfully updated.' } format.json { render :show, status: :ok, location: @hukou } redirect_to hukous_path(page:@page), notice: t(:notice_successful_update) else format.html { render :edit } format.json { render json: @hukou.errors, status: :unprocessable_entity } end end end def destroy @hukou.destroy @page=params[:page] respond_to do |format| format.html { redirect_to hukous_url, notice: 'Hukou was successfully destroyed.' } format.json { head :no_content } redirect_to hukous_path(page:@page),notice: t(:notice_successful_delete) end end< 4000 /span>
最后在要显示的index.erb页面的最后加上下面代码
<div class="col-lg-12 digg_pagination"> <%= will_paginate @hukous, :previous_label => t(:label_previous), :next_label => t(:label_next) %> </div>
上传图片的方法,主要代码:
<%=file_field_tag "image_file_path", accept: "image/bmp,image/png,image/gif,image/jpeg", style:"display:none;" %> <div class="col-lg-4"> <p> <img id="picture_img" src="" style="width:109px;height:154px;"/> </p> <p><%=hidden_field_tag "picture" %></p> </div>
上传图片 script里面写下面的
var target_img_object; var target_input_object; $('#papers_popup_img').click(function(){ $("#image_file_path").val(""); $("#image_file_path").click(); target_img_object = $("#papers_popup_img"); target_input_object = $("#papers_popup_url"); }); //################################# //# 使用插件ajaxfileupload图片上传 //################################## $('#image_file_path').ajaxfileupload({ 'action': '/user_center/candidates/upload_image', 'params': { 'authenticity_token': $("input[name='authenticity_token']").val() }, 'onComplete': function(response) { if( target_img_object != undefined && (target_img_object.length > 0) ) { // 把上传成功的图片显示出来 target_img_object.attr("src", response); } if( target_img_object != undefined && (target_img_object.length > 0) ) { // // 把上传成功的图片路径保存起来 target_input_object.val(response); } }, 'onStart': function() { }, 'onCancel': function() { } });
控制器的create中添加
def create @hukou = Hukou.new(hukou_params) image_url=params[:picture] #加上此句 @hukou.picture = image_url respond_to do |format| if @hukou.save format.html { redirect_to @hukou, notice: 'Hukou was successfully created.' } format.json { render :show, status: :created, location: @hukou } else format.html { render :new } format.json { render json: @hukou.errors, status: :unprocessable_entity } end end end
进入调试
binding.pry
修改文件
rails g migration ChangeTypeFieldForT_Agencies
增加字段
ruby script/rails generate migration add_password_to_myblogs password:string rake db:migrate #更新表
查看路由
rake routes | grep 路由名字
mongodb查询方式
item = MongoSalesWork.where({sales_code: params.sales_code, :can_time.gt => params.app_start_time,:can_time.lte => params.app_end_time})
相关文章推荐
- Ruby on Rails常用内置方法
- Ruby on Rails的下载及安装以及开发环境的搭建
- Ruby on Rails 的检验方法(Validation Helpers)大全
- Android常用控件-DatePicker以及对话框的两种使用方法
- Ruby on Rails,创建模型,附赠模型与表名不一致时的解决方法
- Ruby On Rails 框架下的常用插件介绍 --- haml
- Ruby on rails开发从头来(四十七)- ActiveRecord基础(强大的find方法)
- Ruby on Rails中执行存储过程和指定SQL语句的方法
- Ruby on rails开发从头来(四十七)- ActiveRecord基础(强大的find方法)
- Ruby on Rails 的检验方法(Validation Helpers)大全
- Ruby on Rails 的检验方法(Validation Helpers)大全 (转)
- Ruby on Rails本地安装方法
- Ruby on Rails中select使用方法
- Ruby on rails常用插件下载
- Ruby on Rails实现文件的下载以及中文文件名乱码问题
- Ruby on Rails本地安装方法
- Ruby on Rails本地安装方法
- Ruby on rails开发从头来(四十七)- ActiveRecord基础(强大的find方法)
- ruby on rails 常用命令
- 几种常见Ruby on Rails内置方法介绍