简体   繁体   中英

Rails - 5: How do I pass an array (has_many association) to the controller action

I have a model event and another model event_rule

class Event < ApplicationRecord
    has_many :event_rules
end

class EventRule < ApplicationRecord
    belongs_to :event
end

I have written an api event#create for saving an event. Here's the body of the POST request:

{
    "name": "asd",
    "code": "ad",
    "isActive": true,
    "description": "asd",
    "notes": "",
    "goalAmount": 0,
    "exportId": "",
    "defaultCurrency": 1,
    "eventStartDate": "2017-04-25T18:30:00.000Z",
    "eventEndDate": "2017-04-27T18:30:00.000Z",
    "eventRules": [
        {
            "extraInformation": "{}",
            "lookupKeyValuePairId": 40
        }
     ]
 }

Here's params hash:

Parameters: {"name"=>"asd", "code"=>"ad", "is_active"=>true, "description"=>"asd", "notes"=>"", "goal_amount"=>0, "export_id"=>"", "default_currency"=>1, "event_start_date"=>"2017-04-25T18:30:00.000Z", "event_end_date"=>"2017-04-27T18:30:00.000Z", "event_rules"=>[{"extra_information"=>"{}", "lookup_key_value_pair_id"=>40}], "client_id"=>"0", "event"=>{"name"=>"asd", "code"=>"ad", "description"=>"asd", "is_active"=>true, "goal_amount"=>0, "export_id"=>"", "event_start_date"=>"2017-04-25T18:30:00.000Z", "event_end_date"=>"2017-04-27T18:30:00.000Z", "default_currency"=>1, "notes"=>""}}

I want the 'event_rules' to be included INSIDE the event . How can do this?

def create
  # initialize Event object with `event_params`
  event = Event.new(event_params)

  # initialize EventRule object per each `event_rule_params`, and associate the EventRule as part of `event.event_rules`
  event_rules_params.each do |event_rule_params|
    event.event_rules << EventRule.new(event_rule_params)
  end

  if event.save
    # SUCCESS
  else
    # FAILURE
  end
end

private

def event_params
  params.require(:event).permit(:name, :code, :is_active, :description, :notes, :goal_amount, :export_id, :default_currency, :event_start_date, :event_end_date, :notes)
end

def event_rules_params
  params.require(:event).fetch(:event_rules, []).permit(:extra_information, :lookup_key_value_pair_id)
end

Alternative Rails-way Solution:

if you have control over the parameters that get sent, reformat your request into something like the following (take note of changing event_rules into event_rules_attributes -- Rails Standard) (More Info Here)

Parameters: {
  "event"=>{
    "name"=>"asd",
    "code"=>"ad",
    "description"=>"asd",
    "is_active"=>true,
    "goal_amount"=>0,
    "export_id"=>"",
    "event_start_date"=>"2017-04-25T18:30:00.000Z", 
    "event_end_date"=>"2017-04-27T18:30:00.000Z",
    "default_currency"=>1,
    "notes"=>"",
    "event_rules_attributes"=>[
      {
        "extra_information"=>"{}",
        "lookup_key_value_pair_id"=>40
      }
    ]
  }
}


# controllers/events_controller.rb
def create
  event = Event.new(event_params)

  if event.save
    # SUCCESS
  else
    # FAILURE
  end
end

private

def event_params
  params.require(:event).permit(:name, :code, :is_active, :description, :notes, :goal_amount, :export_id, :default_currency, :event_start_date, :event_end_date, :notes, event_rules_attributes: [:extra_information, :lookup_key_value_pair_id])
end

# models/event.rb
accepts_nested_attributes_for :event_rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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