如果子值为空,则不会保存嵌套表单

时间:2014-06-09 04:44:35

标签: ruby-on-rails-4

我有一张表格:

<%= simple_form_for([@ticket,@note])  do |f| %>
  <%= f.input :name, label: "Note", input_html: {:"x-webkit-speech" => ""} %>

  <%= f.simple_fields_for :timesheets do |builder| %>
    <%= builder.input :duration, :input_html => { :size => 3, :maxlength => 5 } %> 
    <%= builder.association :work_type, collection: Choice.work_types %>
  <% end %>

  <%= f.button :submit %>
<% end %>

如果我在表单中输入名称,持续时间和工作类型,则会保存注释和时间表。但是,如果我只输入&#34;注意&#34;并将Duration和WorkType清空错误。

理想情况下,我希望它只是保存一个注释而没有时间表。

以下是发布的参数

Parameters: {"utf8"=>"✓", "authenticity_token"=>"dd2TRC8cU=", "note"=>{"name"=>"My Note", "timesheets_attributes"=>{"0"=>{"duration"=>"", "work_type_id"=>""}}}, "commit"=>"Create Note", "type"=>"note", "ticket_id"=>"21"}

没有特定错误:

(0.1ms)  begin transaction
(0.1ms)  rollback transaction

编辑:模特:

class Timesheet < ActiveRecord::Base

  belongs_to :activity

  validates_numericality_of :duration
  validates_numericality_of :work_type_id

end

class Activity < ActiveRecord::Base

  has_many :timesheets
end  

控制器

def create
  @ticket = Ticket.find(params[:ticket_id])

  if params[:type]      
    case type
    when "note"
      @activity = @ticket.notes.new(activity_params)
    when "email"
      @activity = @ticket.emails.new(activity_params)
    end
  end

  if @activity.save
    redirect_to @ticket, notice: 'Activity was successfully created.'
  else
    # populate variables
    @email = @activity
    @note = @activity
    render action: 'new', error: 'Something went wrong.'
  end
end


def activity_params(action)
  case type
  when 'email'
    activity_params = params.require(:email).permit(:ticket_id, :name, :description, 
                                                    :entity_id, :user_id, :agreement_id, 
                                                    :followup, :type, :from, {:to => []}, 
                                                    {:cc => []}, :draft, {:attachments_attributes => [:file]}, 
                                                    {timesheets_attributes: [:duration, :work_type_id]} )

    # lets convert the array of email addresses to a string
    case action
      when "create"
        activity_params[:to] = activity_params[:to].drop(1).join(", ")
        activity_params[:cc] = activity_params[:cc].drop(1).join(", ")
      when "update"
        activity_params[:to] = activity_params[:to].drop(1).join(", ")
        activity_params[:cc] = activity_params[:cc].drop(1).join(", ")                  
    end

    return activity_params

  when 'note'
    activity_params = params.require(:note).permit(:ticket_id, :name, :description, :entity_id, :user_id, :agreement_id, :followup, :type, {:attachments_attributes => [:file]}, {timesheets_attributes: [:duration, :work_type_id]} )

    return activity_params
  end
end

2 个答案:

答案 0 :(得分:1)

最终解决方案:

  if activity_params[:timesheets_attributes]["0"][:duration].blank? && activity_params[:timesheets_attributes]["0"][:work_type_id].blank? then 
    activity_params.delete :timesheets_attributes
  end

答案 1 :(得分:0)

  

理想情况下,我希望它只保存一个笔记而没有时间表

您需要在特定字段中使用 allow_blank => true ,以便即使使用空白值也会保存记录。

在您的 Timesheet model 中,像这样给出

Class Timesheet < ActiveRecord::Base

....

validates_presence_of :duration, :allow_blank => true

validates_presence_of :work_type_id, :allow_blank => true

end

有关详细信息,请查看 allow_balnk