<%= 2.times do |f| %>
<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>
<%= end %>
或
<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>
<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>
params中存储的格式相当与 params = {"utf-8"=>"✓", "subject"=>{"title" => "fdsafdsafdsa", "body_data" => [{:origin_url =>HTML对应属性为name="subject[body_data][][origin_url]"的标签输入的值, :medium_url => fdas, :width => dsafds, :height => dasfsdfdas}, {:origin_url =>assadasdsa, :medium_url => fdassss, :width => dssssafds, :height => dasfssssdfdas}]}}
!! 记住Subject的属性一定要存在params中key为subject的散列中
因为params完整性定义时要用
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]
)
end
!!在后端中拿取<input type="hidden" name="subject[body_data][][origin_url]" value="<%= f[:origin_url] %>"></input>前端输入值用params[:subject][:body_data][0或者1或者遍历][:origin_url]
controller中params接受来自前端数据 并进行遍历(*****只有数组才可以对其遍历, 哈系不可以遍历 => 具体意思是[{type: "fdsa", content: "assaas"}, {type: "dsafsd", content: "aaaa"}]可以遍历 {{type: "dsaf", content: "dsafdsa"}, {type: "sdfa", content: "assaa"}}不可以遍历)
params[:subject][:body_data].each do |f|
p f[:origin_url]
p f[:medium_url]
p f[:width]
p f[:height]
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=\'#\' οnclick=\'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=\'#\' οnclick=\'deltr("+(i-1)+")\'>删除</a></td>"
+"</tr>");
}
controller:
class Manage::SubjectsController < ManageController
def index
@q = Subject.ransack(params[:q])
@subjects = @q.result.order(id: :desc).page(params[:page])
end
def create
@subject = Subject.create(subject_params)
if @subject.save
redirect_to manage_subjects_path
end
end
def new
@subject = Subject.new
end
def edit
@subject = Subject.find(params[:id])
@rooms_subjects = @subject.rooms.all
end
def destroy
Subject.find_by(id: params[:id]).delete
redirect_to manage_subjects_path
end
def show
@subject = Subject.find(params[:id])
end
def update
@subject = Subject.find(params[:id])
if @subject.update(subject_params)
redirect_to manage_subjects_path
end
end
def delete_room
@subject = Subject.find(params[:id])
@subject.rooms_subjects.find_by(room_id: params[:room_id]).delete
redirect_to :back
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]
)
end
end
model:
class Subject < ActiveRecord::Base
belongs_to :user
has_many :rooms_subjects
has_many :rooms, through: :rooms_subjects
has_many :photos, as: :content
# body: [{type: "text", content: "Hello world!~~~"},
# {type: "photo", width: 300, height: 200, origin_url: "", medium_url: ""}]
store :data, accessors: [:body]
SubjectType = %w(Romance Classical Pink Fashion Gent)
STATUS = %w(pending depending)
def photos
self.photos.where.not(position: 9999)
end
def photo
Photo.where(content_id: self.id, content_type: self.class.name, position: 9999).order(id: :desc).first
end
def web_thumb_url
"http://cdn.a0.bnbtrip.com/assets/images/web/2016/02/landing2.jpg"
end
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
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 %>