ruby on rails界面常用控件写法以及rails常用方法

在数据表中增加字段

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

最后在要显示的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})
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值