繁体   English   中英

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

[英]Submitting or updating multiple objects at once in one form in Rails

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

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对象,并在提交时更新它们。

任何代码建议?

查看

= 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

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

有没有其他人有重构我的更新操作的解决方案? 看起来有点凌乱。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM