在Rails中以一种形式一次提交或更新多个对象

时间:2015-02-19 02:04:08

标签: ruby-on-rails forms

我在下面有这个表单,我希望一次更新多个对象的相同属性。这并不像在表单外移动提交按钮那么容易,因为我已经尝试过了。

http://i.stack.imgur.com/HXeSn.png

这里是观点(借助引导程序)

- @venues.each do |venue|
.row.placeholders
    = form_for :venue do |f|
      - Venue::ACTIVE_FEATURES.each do |active_feature|
        = check_box_tag "venue[active_features][]", active_feature, @venues.first.active_features.include?(active_feature), disabled: true
        = h active_feature.humanize
        %br/
      = hidden_field_tag "venue[active_features][]", ""
    %span.text-muted
  .col-xs-6.col-sm-3.col-md-2.placeholder
    %h4 Contact
    %span.text-muted
      = form_for :venue, :url => {:action => :update}, :method => "put" do |f|
        OpenTable Rep:
        = f.text_field :opentable_rep
        %br/
        General Manager:
        = f.text_field :general_manager
        %br/
        = f.submit 'Save Edits'
%hr

控制器

class SnapshotsController < ApplicationController

  def show
    @venues = Venue.where(parent_venue_id: current_admin.parent_venue_id)
  end

  def update
    @venues = Venue.where(parent_venue_id: current_admin.parent_venue_id)
    if @venues.update_attributes(opentable_rep: params[:venue][:opentable_rep], general_manager: params[:venue][:general_manager])
      redirect_to snapshot_path
      flash[:notice] = "Your changes were saved"
    else
      render :show
      flash[:notice] = "Changes didn't save correctly"
    end
  end

end

这是我尝试过的事情

视图

      = form_for :venue, :url => {:action => :update}, :method => "put" do |f|
        - @venues.each do |venue|
              - Venue::ACTIVE_FEATURES.each do |active_feature|
                = check_box_tag "venue[active_features][]", active_feature, @venues.first.active_features.include?(active_feature), disabled: true
                = h active_feature.humanize
                %br/
              = hidden_field_tag "venue[active_features][]", ""
              %span.text-muted
            .col-xs-6.col-sm-3.col-md-2.placeholder
              %h4 Contact
              %span.text-muted
                OpenTable Rep:
                = f.text_field :opentable_rep
                %br/
                General Manager:
                = f.text_field :general_manager
                %br/
        = f.submit 'Save Edits'

这是一个截图 http://i.stack.imgur.com/cs6Us.png

控制器保持与上述相同

我的参数变为

{"utf8"=>"✓", "_method"=>"put", "venue"=>{"active_features"=>["", ""], "opentable_rep"=>"mary", "general_manager"=>"barry"}, "commit"=>"Save Edits", "action"=>"update", "controller"=>"snapshots"}

我希望/需要params,如果它们变为EACH对象,并在提交时更新它们。

任何代码建议?

1 个答案:

答案 0 :(得分:0)

<强>视图

= form_for :venue, :url => {:action => :update}, :method => "put" do |f|
- @venues.each do |venue|
      %h4 Contact
      %span.text-muted
        = f.label :opentable_rep, "OpenTable Rep"
        = f.text_field :opentable_rep, name: "venue[#{venue.id}][opentable_rep]", value: venue.opentable_rep, placeholder: "email"
        = f.label :general_manager, "General Manager"
        = f.text_field :general_manager, name: "venue[#{venue.id}][general_manager]", value: venue.general_manager, placeholder: "email"
        %br/
  %hr
= submit_tag "Save Edits"

<强>控制器

class SnapshotsController < ApplicationController

  def update
    @venues = Venue.where(parent_venue_id: current_admin.parent_venue_id)
    if params[:venue] != nil
        params[:venue].keys.each do |venue_id|
          opentable_rep = params[:venue][venue_id][:opentable_rep]
          general_manager = params[:venue][venue_id][:general_manager]
          @venues.update(venue_id, {opentable_rep: opentable_rep, general_manager: general_manager}).touch
        end
      redirect_to snapshot_path
      flash[:notice] = "Your changes were saved"
    else
      render :show
      flash[:notice] = "Changes didn't save correctly"
    end
  end

end

这最终成为我的解决方案。所有疯狂的参数都在表单中的名称属性中得到了解决。

是否还有其他人有重构我的更新操作的解决方案?看起来有点混乱。