增加数据库表
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
en
带验证的输入框
<%= 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</label>
<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] ])%>
读取文件 //添加图片
<%= f.file_field :picture %>
增加删除
<div >
<%= f.submit t(:button_save) ,:class=>"btn btn-info btn-sm"%>
<%= link_to t(:button_back), hukous_path(@hukou=params[:hukou]) ,{ :class=>"btn btn-default btn-sm"} %>
</div>
创建分页方法
class Hukou < ActiveRecord::Base
# 分页
def self.search(page)
order('id desc ').paginate(page: page, per_page: 2) // id 为要查询的序列,在数据库中,一般是第一项
end
end
在Controller里面
index设置成这样
def index
@hukous = Hukou.search(params[:page]||1)
end
update设置成这样
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
destory设置成这样
# DELETE /hukous/1
# DELETE /hukous/1.json
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>
<td>
<%= job.tpool_role.role_name %>
<div class="col-lg-4">
<%= f.select :role_id,
options_for_select(@role_map,
selected: @job[:role_id]) ,{},{class:"form-control"} %>
#
def map_select
#角色
@role_map=TpoolRole.select(:id, :role_name).map { |c| [c.role_name,c.id] }
@role_map.insert(0,[t(:actionview_instancetag_blank_option),""])
查找功能的实现
在models的rb文件中加这段代码
def self.search_by_conditions(conditions, page)
where(conditions).order('id desc ').paginate(page: page, per_page: 5)
end
控制器中加
def index
conditions = []
[:name, :user_type, :old, :sex].each{
|attr| conditions << Hukou.send(:sanitize_sql,
["#{attr} LIKE ? ", "%#{params[attr]}%"]) unless params[attr].blank? }
conditions = conditions.any? ? conditions.collect { |c| "(#{c})" }.join(' AND ') : nil
@hukous = Hukou.search_by_conditions(conditions,params[:page])
end
在网页中加以下代码
<%= form_tag(manager_tpool_agencies_path, :method => :get, class:"city-group form-inline") do %>
<div class="col-lg-12" style="margin:0 0 8px 0;padding:0">
<div class="col-lg-4" style="padding:0">
<div class="form-group">
<label for="agency_name">
<%= t(:lable_agency_name)%>:
</label>
<%= text_field_tag 'agency_name', params[:agency_name] ,:class=>"form-control"%>
</div>
</div>
<div class="col-lg-8" style="padding:0">
<div class="form-group">
<label for="exampleInputName2">
<%=t(:lable_agency_region)%>:
</label>
<%= select_tag "agency_province", options_for_select(ChinaCity.list,:selected => params[:agency_province]), class: 'city-select form-control', prompt: t(:label_province)%>
<%= select_tag "agency_city", options_for_select(ChinaCity.list(params[:agency_province]),:selected => params[:agency_city]) , class: 'city-select form-control', prompt: t(:label_city)%>
<%= select_tag "agency_region", options_for_select(ChinaCity.list(params[:agency_city]),:selected => params[:agency_region]) , class: 'city-select form-control', prompt: t(:label_region)%>
</div>
</div>
</div>
<div class="col-lg-12" style="padding:0">
<div class="col-lg-4" style="padding:0">
<div class="form-group">
<label for="agency_type">
<%= t(:lable_agency_type)%>:
</label>
<%= select_tag :agency_type, options_for_select([[t(:label_financial_major),t(:label_financial_major)],[t(:label_big_enterprise),t(:label_big_enterprise)],[t(:label_research_institutions),t(:label_research_institutions)]], :selected => params[:agency_type] ),{:prompt => t(:actionview_instancetag_blank_option),class: "form-control"}%>
</div>
</div>
<div class="col-lg-3" style="padding:0">
<%= submit_tag t(:label_search), :class => "btn btn-info btn-sm", :name => nil %><!--sousuo-->
<input type="button" id="clear" class="btn btn-info btn-sm" value= "<%= t(:button_reset) %>" />
</div>
</div>
<% end %>
上传图片
循环处
<%= form_for [@t_agency] ,:html => { :multipart=>'true', :class=>"form-horizontal" } do |f|%>
循环下面写
<%=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中添加
# POST /hukous
# POST /hukous.json
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})