params详解

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


以上二者相同,因为堆跌之后遇见最后一个参数(origiin_url或medium_url等等)相同时, 第二个参数自曾([])


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值