簡體   English   中英

如何刪除在Rails中用collection_select保存的記錄?

[英]How do I delete records saved with collection_select in Rails?

我的目標是,當從collection_select中取消選擇項目時,能夠更新保存的記錄(然后重新提交該記錄。)預先感謝您的幫助!

細節

我有一個Newsavedmaps的表格。 Newsavedmaps可以有許多路標。 用戶可以在collection_select中選擇航路點,並且在保存Newsavedmap時,這些航路點將保存到自己的數據庫表中。

問題是:當用戶打開他們保存的Newsavedmap時,我希望他們能夠取消選擇航路點。 當他們再次保存Newsavedmap時,我希望刪除選定的航路點。

這是我正在維護的Rails 2.3X應用程序,這就是為什么collection_select在下面使用其他格式的原因。

模型

class Newsavedmap < ActiveRecord::Base
  belongs_to :itinerary
  has_many :waypoints, :dependent => :destroy
  accepts_nested_attributes_for :waypoints, :reject_if => lambda { |a| a[:waypointaddress].blank? }, :allow_destroy => true
end

視圖

    <% form_for @newsavedmap, :html => { :id => 'createaMap' } do |f| %>
      <%= f.error_messages %>               
      <%= f.text_field :name, {:id=>"savemap_name", :size=>30 }%></p>

      <%= collection_select :waypoints, :waypointaddress, @newsavedmap.waypoints, :waypointaddress, :waypointaddress, {}, { :multiple => true, :class => "mobile-waypoints-remove", :id =>"waypoints" } %>

    <% end %> 

Newsavedmaps控制器

def create
  @newsavedmap = Newsavedmap.new(params[:newsavedmap])
  waypoint = @newsavedmap.waypoints.build

  respond_to do |format|
    if @newsavedmap.save
      flash[:notice] = 'The new map was successfully created.'
      format.html { redirect_to "MYURL"}
      format.xml  { render :xml => @newsavedmap, :status => :created, :location => @newsavedmap }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @newsavedmap.errors, :status => :unprocessable_entity }
    end
  end
end


def update
  @newsavedmap = Newsavedmap.find(params[:id])

  if @newsavedmap.itinerary.user_id == current_user.id
    respond_to do |format|
      if @newsavedmap.update_attributes(params[:newsavedmap])
        flash[:notice] = 'Newsavedmap was successfully updated.'
        format.html { redirect_to "MYURL" }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @newsavedmap.errors, :status => :unprocessable_entity }
      end
    end
  else
    redirect_to '/'
  end
end

創建新記錄時的參數

參數:{“ newsavedmap” => {“ name” =>“我的地圖名稱”,其他字段未顯示,“ waypoints” => {“ waypointaddress” => [“ 1600 Pennsylvania Ave NW,華盛頓特區,DC 20500” ,“ 350 5th Ave,紐約,NY 10118”]}}

我認為您的問題采用正確構建的形式(以及由此產生的參數)。

我建議

  1. 看寶石nested_form
  2. waypoints_attributes添加到attr_accessible
  3. 在您的表單中實現來自gem(從第1點開始)的助手

Rails魔術應該做其他工作

另一個變體(不使用寶石)( 我認為要困難得多!

您可以完全刪除accept_nested_attributes並直接使用參數。 但是在這種情況下,您應該手動管理所有過程:正確插入記錄,正確銷毀它們。

在您的情況下,它應該像這樣(未經測試!)。 該示例基於您在問題中發布的參數。

def create
  # delete params 'waypoints' it will be manage manually
  waypoints = params[:newsavedmap].delete(:waypoints)
  @newsavedmap = Newsavedmap.new(params[:newsavedmap])
  waypoints.each do |waypoint|
    @newsavedmap.waypoints.build(:waypointaddress => waypoint)
  end
  if @newsavedmap.save
    ...
  end
end

主要的麻煩將在方法update

def update
  # delete params 'waypoints' it will be manage manually
  waypoints = params[:newsavedmap].delete(:waypoints)

  # find and setup attributes
  @newsavedmap = Newsavedmap.find(params[:id])
  @newsavedmap.attributes = params[:newsavedmap]

  # TROUBLES start here
  # destroy not checked (but existed in DB) waypoints
  existed_waypoints = @newsavedmap.waypoints
  existed_waypoint_addresses = existed_waypoints.map(&:waypointaddress)
  new_waypoints = []
  waypoints.each do |waypoint|
    if existed_waypoint_addresses.include?(waypoint)
      # existed waypoint was selected on the form
      # find it and add to new_waypoints
      new_waypoints << @newsavedmap.waypoints.find_by_waypointaddress(waypoint)
    else
      # new waypoint was selected on the form
      # build it and add to new_waypoints
      new_waypoints << @newsavedmap.waypoints.build(:waypointaddress => waypoint)
    end
  end
  # setup new records for waypoints
  @newsavedmap.waypoints = new_waypoints

  if @newsavedmap.save
    # destroy existed but not selected waypoints
    (existed_waypoints - new_waypoints).map(&:destroy)
    ...
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM