我的目标是能够在从 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 %>
新闻保存地图控制器
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"=>"我的地图名称", OTHER FIELDS NOT SHOWN ABOVE, "waypoints"=>{"waypointaddress"=>["1600 Pennsylvania Ave NW, Washington, DC 20500" , "350 5th Ave, New York, NY 10118"]}}