重要!!!model原带update和cerate进行逻辑更新和存值(代码复用)
2016-04-15 17:37
344 查看
model中自带的update和create(update和create都是在controller中对数据表类的调用)是相当于对每一个params的值和其对应数据库同名的字段进行等号操作
!!!重要::所以页面接受值存入数据库 html中标签接受数据库所要的数据时属性name要与该数据库的字段一一对应
!!!重要::所以在model中定义字段的等号操作,相当与在controller中对数据表类进行update和create操作时(params相应字段存入数据库等号操作)可以自定义一个逻辑筛选,但在该函数中必须对数据进行保存或更新,因为自定义等号操作rails就不帮你进行保存了。
!!!重要::等号操作是 数据库字段 = params[对应数据库字段一样名字的] 所以在model中定义等号操作时注意
####传入参数image实际是params完整性定义(params.require(:subject).permit(:user_id, :title, :description, :subject_type, :position, :status, :photo,body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]))的对应的params[:photo]
####params完整性定义的符号名是数据库字段的子集, 名字最好相同, 否则得在等号操作update, create中进行更改
def photo=(image)
photo = self.photo
if photo.present?
photo.update(image: image)
else
Photo.create(content_id: self.id, content_type: self.class.name, image: image, position: 9999)
end
end
#####传入参数params实际是params完整性定义(params.require(:subject).permit(:user_id, :title, :description, :subject_type, :position, :status, :photo,body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]))的对应的params[:body]
def body_data=(params)
new_body = []
if params.present?
params.each do |f|
if f["content"].present?
new_body << {type: "text", content: f[:content]}
else
if f["photo"].present?
photo = Photo.create(content_id: self.id, content_type: self.class.name, image: f["photo"])
new_body << {type: "photo", width: photo.width, height: photo.height, origin_url: photo.image_url, medium_url: photo.small_url}
elsif f["origin_url"].present?
new_body << {type: "photo", width: f["width"], height: f["height"], origin_url: f["origin_url"], medium_url: f["medium_url"]}
end
end
end
end
self.update(body: new_body)
end
#######在controller中params完整性定义params.require(:subject).permit(:user_id, :title, :description, :subject_type, :position, :status, :photo,body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]),首先是防止不该接受的参数, 其次在对应model提供的update,create时不用params[:subject][:photo]才能引用到只需要params[:photo]就可以引用到,
因为params.require(:subject)相当于把:subject忽略了, 但除了用到完整性定义的函数进行update, create(相当与用params.require(:subject)赋值)之外,在controller中引用页面html传入的数值必须用完整的params(例:params[:subject][:photo], params[:subject][:body][0][:origin_url]才能引用到)
实例:
controller:
def create
@subject = Subject.create(subject_params)
if @subject.save
redirect_to manage_subjects_path
end
end
def update
@subject = Subject.find(params[:id])
if @subject.update(subject_params)
redirect_to manage_subjects_path
end
end
private
def subject_params
params.require(:subject).permit(
:user_id, :title, :description, :subject_type, :position, :status, :photo,
body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]
)######重要 注意数组date是写params完整性参数的
end
model:
def photo=(image)
photo = self.photo
if photo.present?
photo.update(image: image)
else
Photo.create(content_id: self.id, content_type: self.class.name, image: image, position: 9999)
end
end
def body_data=(params)
new_body = []
if params.present?
params.each do |f|
if f["content"].present?
new_body << {type: "text", content: f[:content]}
else
if f["photo"].present?
photo = Photo.create(content_id: self.id, content_type: self.class.name, image: f["photo"])
new_body << {type: "photo", width: photo.width, height: photo.height, origin_url: photo.image_url, medium_url: photo.small_url}
elsif f["origin_url"].present?
new_body << {type: "photo", width: f["width"], height: f["height"], origin_url: f["origin_url"], medium_url: f["medium_url"]}
end
end
end
end
self.update(body: new_body)
end
view:
<% if @subject.body.present? %>
<% i = 0 %>
<% @subject.body.each do |f|%>
<tr>
<td>
<%= i += 1 %>
</td>
<td>
<input type="text" name="subject[body_data][][content]" value="<%= f[:content] %>"></input>
</td>
<td>
<input type="hidden" name="subject[body_data][][origin_url]" value="<%= f[:origin_url] %>"></input>
<input type="hidden" name="subject[body_data][][medium_url]" value="<%= f[:medium_url] %>"></input>
<input type="hidden" name="subject[body_data][][width]" value="<%= f[:width] %>"></input>
<input type="hidden" name="subject[body_data][][height]" value="<%= f[:height] %>"></input>
<%= image_tag(f[:medium_url]) if f[:medium_url] %>
<input type="file" name="subject[body_data][][photo]"></input>
</td>
<td>
<a href="#" class="delete_photo_text_button">删除</a>
</td>
</tr>
<% end %>
<% end %>
js:
$(document).ready(function(){
$(".ui celled striped tr").attr("align","center");
$("#add_photo_text_button").click(function(){
var _len = $("#add_photo_text tr").length;
$("#add_photo_text").append("<tr id="+_len+" align='center'>"
+"<td>"+_len+"</td>"
+"<td><input type='text' name='subject[body_data][][content]' id='desc"+_len+"' /></td>"
+"<td><input type='file' name='subject[body_data][][photo]' id='desc"+_len+"' /></td>"
+"<td><a href=\'#\' onclick=\'deltr("+_len+")\'>删除</a></td>"
+"</tr>");
})
})
var deltr =function(index)
{
var _len = $("#add_photo_text tr").length;
$("tr[id='"+index+"']").remove();
for(var i=index+1,j=_len;i<j;i++)
{
var nextTxtVal = $("#desc"+i).val();
$("tr[id=\'"+i+"\']")
.replaceWith("<tr id="+(i-1)+" align='center'>"
+"<td>"+(i-1)+"</td>"
+"<td><input type='text' name='subject[body_data][][content]' value='"+nextTxtVal+"' id='desc"+(i-1)+"'/></td>"
+"<td><input type='file' name='subject[body_data][][photo]' value='"+nextTxtVal+"' id='desc"+(i-1)+"'/></td>"
+"<td><a href=\'#\' onclick=\'deltr("+(i-1)+")\'>删除</a></td>"
+"</tr>");
}
}
!!!重要::所以页面接受值存入数据库 html中标签接受数据库所要的数据时属性name要与该数据库的字段一一对应
!!!重要::所以在model中定义字段的等号操作,相当与在controller中对数据表类进行update和create操作时(params相应字段存入数据库等号操作)可以自定义一个逻辑筛选,但在该函数中必须对数据进行保存或更新,因为自定义等号操作rails就不帮你进行保存了。
!!!重要::等号操作是 数据库字段 = params[对应数据库字段一样名字的] 所以在model中定义等号操作时注意
####传入参数image实际是params完整性定义(params.require(:subject).permit(:user_id, :title, :description, :subject_type, :position, :status, :photo,body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]))的对应的params[:photo]
####params完整性定义的符号名是数据库字段的子集, 名字最好相同, 否则得在等号操作update, create中进行更改
def photo=(image)
photo = self.photo
if photo.present?
photo.update(image: image)
else
Photo.create(content_id: self.id, content_type: self.class.name, image: image, position: 9999)
end
end
#####传入参数params实际是params完整性定义(params.require(:subject).permit(:user_id, :title, :description, :subject_type, :position, :status, :photo,body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]))的对应的params[:body]
def body_data=(params)
new_body = []
if params.present?
params.each do |f|
if f["content"].present?
new_body << {type: "text", content: f[:content]}
else
if f["photo"].present?
photo = Photo.create(content_id: self.id, content_type: self.class.name, image: f["photo"])
new_body << {type: "photo", width: photo.width, height: photo.height, origin_url: photo.image_url, medium_url: photo.small_url}
elsif f["origin_url"].present?
new_body << {type: "photo", width: f["width"], height: f["height"], origin_url: f["origin_url"], medium_url: f["medium_url"]}
end
end
end
end
self.update(body: new_body)
end
#######在controller中params完整性定义params.require(:subject).permit(:user_id, :title, :description, :subject_type, :position, :status, :photo,body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]),首先是防止不该接受的参数, 其次在对应model提供的update,create时不用params[:subject][:photo]才能引用到只需要params[:photo]就可以引用到,
因为params.require(:subject)相当于把:subject忽略了, 但除了用到完整性定义的函数进行update, create(相当与用params.require(:subject)赋值)之外,在controller中引用页面html传入的数值必须用完整的params(例:params[:subject][:photo], params[:subject][:body][0][:origin_url]才能引用到)
实例:
controller:
def create
@subject = Subject.create(subject_params)
if @subject.save
redirect_to manage_subjects_path
end
end
def update
@subject = Subject.find(params[:id])
if @subject.update(subject_params)
redirect_to manage_subjects_path
end
end
private
def subject_params
params.require(:subject).permit(
:user_id, :title, :description, :subject_type, :position, :status, :photo,
body_data: [:content, :photo, :origin_url, :medium_url, :width, :height]
)######重要 注意数组date是写params完整性参数的
end
model:
def photo=(image)
photo = self.photo
if photo.present?
photo.update(image: image)
else
Photo.create(content_id: self.id, content_type: self.class.name, image: image, position: 9999)
end
end
def body_data=(params)
new_body = []
if params.present?
params.each do |f|
if f["content"].present?
new_body << {type: "text", content: f[:content]}
else
if f["photo"].present?
photo = Photo.create(content_id: self.id, content_type: self.class.name, image: f["photo"])
new_body << {type: "photo", width: photo.width, height: photo.height, origin_url: photo.image_url, medium_url: photo.small_url}
elsif f["origin_url"].present?
new_body << {type: "photo", width: f["width"], height: f["height"], origin_url: f["origin_url"], medium_url: f["medium_url"]}
end
end
end
end
self.update(body: new_body)
end
view:
<% if @subject.body.present? %>
<% i = 0 %>
<% @subject.body.each do |f|%>
<tr>
<td>
<%= i += 1 %>
</td>
<td>
<input type="text" name="subject[body_data][][content]" value="<%= f[:content] %>"></input>
</td>
<td>
<input type="hidden" name="subject[body_data][][origin_url]" value="<%= f[:origin_url] %>"></input>
<input type="hidden" name="subject[body_data][][medium_url]" value="<%= f[:medium_url] %>"></input>
<input type="hidden" name="subject[body_data][][width]" value="<%= f[:width] %>"></input>
<input type="hidden" name="subject[body_data][][height]" value="<%= f[:height] %>"></input>
<%= image_tag(f[:medium_url]) if f[:medium_url] %>
<input type="file" name="subject[body_data][][photo]"></input>
</td>
<td>
<a href="#" class="delete_photo_text_button">删除</a>
</td>
</tr>
<% end %>
<% end %>
js:
$(document).ready(function(){
$(".ui celled striped tr").attr("align","center");
$("#add_photo_text_button").click(function(){
var _len = $("#add_photo_text tr").length;
$("#add_photo_text").append("<tr id="+_len+" align='center'>"
+"<td>"+_len+"</td>"
+"<td><input type='text' name='subject[body_data][][content]' id='desc"+_len+"' /></td>"
+"<td><input type='file' name='subject[body_data][][photo]' id='desc"+_len+"' /></td>"
+"<td><a href=\'#\' onclick=\'deltr("+_len+")\'>删除</a></td>"
+"</tr>");
})
})
var deltr =function(index)
{
var _len = $("#add_photo_text tr").length;
$("tr[id='"+index+"']").remove();
for(var i=index+1,j=_len;i<j;i++)
{
var nextTxtVal = $("#desc"+i).val();
$("tr[id=\'"+i+"\']")
.replaceWith("<tr id="+(i-1)+" align='center'>"
+"<td>"+(i-1)+"</td>"
+"<td><input type='text' name='subject[body_data][][content]' value='"+nextTxtVal+"' id='desc"+(i-1)+"'/></td>"
+"<td><input type='file' name='subject[body_data][][photo]' value='"+nextTxtVal+"' id='desc"+(i-1)+"'/></td>"
+"<td><a href=\'#\' onclick=\'deltr("+(i-1)+")\'>删除</a></td>"
+"</tr>");
}
}
相关文章推荐
- Python 练习实例15
- C++作业2-2
- Project facet Java version 1.8 is not supported.错误提示解决方法
- DecimalFormat类——Java中字符串类型的数字加减报错和精度缺失的解决办法
- 多进程并发编程----进程间传递文件描述符案例
- java反射代理学习
- Java中的多线程(五)之死锁演示
- Google Play服务中的条码扫描识别
- 巩固C语言(番外篇)----二级指针
- C#中索引器的作用和实现。
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDAOFacto
- Spring MVC事务配置
- javaSocket多次传输没有重新初始化的一个对象接收端只会接收到第一个对象的值
- java 1.7之后的Arraylist容量增长机制
- 在ubuntu下使用VIM作为python开发环境
- PHP定时执行任务的实现
- 【QT】:QT中信号与槽详解
- Java学习路线
- 2.Java之if语句及三目运算符
- Python 练习实例14