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|%>       

  <div class="form-group">
    <label for="hukou_name" class="col-sm-2 control-label">namelabe
    <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})

你可能感兴趣的:(rails,ruby)