Method: Cms9::PostFieldsController#create

Defined in:
app/controllers/cms9/post_fields_controller.rb

#createObject

rubocop:disable all



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/cms9/post_fields_controller.rb', line 10

def create
  @field = PostField.new(post_field_params)
  @field.user_id = current_user.id

  if PostField.where(name: @field[:name],
                     post_definition_id: @field[:post_definition_id]).blank?
    if %w(select_single select_multiple).include?(@field[:field_type])
      @field. = ''
      values = params[:multi_values]

      values.each_with_index do |value, index|
        if value != '' && index.zero?
          @field. = value
        elsif !index.zero?
          @field. = @field. + ',' + value
        end
      end
    elsif !%w(select_single select_multiple image)
          .include?(@field[:field_type])
      @field. = nil
    end

    if @field.save
      create_post_field_event
      redirect_to edit_post_definition_path(id: @field.post_definition_id)
    else
      render :new
    end
  else
    @field.errors.add(:name, 'of field already exist')
    render :new
  end
end