嵌套属性不在父编辑表单中验证。如何在编辑操作中验证父表单中的子表单?

时间:2015-04-21 10:07:45

标签: ruby-on-rails ruby-on-rails-4 nested-attributes

我的任务有很多约会。创建新任务时,我只有一个约会(因为约会只能一次创建一个)。我对new和create动作没有任何问题,约会将被验证保存到db。问题是当我编辑任务时,子表单(约会)没有验证也没有更新。

任务控制器中的强大参数:

def task_params
    params.require(:task).permit(:category_id, :subcategory_id, :title, :description, :pay_offer, :is_pay_per_hour, :county_id, :area_id, 
        appointments_attributes: [:id, :start_date, :start_time, :duration])
end

任务:

has_many :appointments, :dependent => :destroy
accepts_nested_attributes_for :appointments

预约:

belongs_to :task

任务控制器中的操作:

def new
    @task = current_user.task_posts.build
    @task.appointments.build
end

def create
    @task = current_user.task_posts.build(task_params)
    if @task.save
        flash[:success] = "Micropost created!"
        redirect_to @current_user
    else
        render 'new'
    end
end

def edit

    @task = Task.find(params[:id])
    @task.county_id = @task.county.id
    @task.category_id = @task.category.id
    @task.appointments.first
end

def update
    @task = Task.find(params[:id])

    if @task.update_attributes(task_params)
        flash[:success] = "Task updated"
        redirect_to @task
    else
        render 'edit'
    end
end

编辑和新模板:

<%= simple_form_for @task, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
.....some fields of @task
<% f.simple_fields_for :appointments do |b| %>

<%= b.input :start_date, label: "Start date", wrapper: :horizontal_input_group do %>
<%= b.input_field :start_date, placeholder: 'Set ', class: "form-control date start" %><span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
        <% end %>

<%= b.input :start_time, label: "Start time", wrapper: :horizontal_input_group do %>
<%= b.input_field :start_time, placeholder: 'Starting time at', as: :string, class: "form-control time start" %><span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
        <% end %>

<%= b.input :duration, label: "Duration", wrapper: :horizontal_input_group  do %>
<%= b.input_field :duration, placeholder: 'Duration', as: :integer, class: "form-control", min: "1", max: "8"%>
<span class="input-group-addon" id="hour-addon">Hour(s)</span>
<% end %>
<% end %>

一些情景:

假设日期时间是2015年4月28日上午7:00。 如果我去编辑表格并将开始日期和时间更改为2015年5月2日凌晨2:00,这就是我将在控制台中获得的内容:

Started PATCH "/tasks/121" for ::1 at 2015-04-21 11:19:20 +0100
Processing by TasksController#update as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"+Qfn0bsSEHWXxnHFAQCpr/rAL8OkY
ichj2FDfAXF8siWu3/NsZlj2O3N5PptcCqy8PyhH3/mIT4/ZOvozoh9hw==", "task"=>{"category
_id"=>"1", "subcategory_id"=>"2", "title"=>"ssdsdsssdasdsdddddddddd", "descripti
on"=>"sdsdsssssssssssssssssssssssss", "county_id"=>"15", "area_id"=>"659", "is_p
ay_per_hour"=>"0", "pay_offer"=>"29", "appointments_attributes"=>{"0"=>{"start_d
ate"=>"2/5/2015", "start_time"=>"02:00 AM", "duration"=>"0", "id"=>"134"}}}, "co
mmit"=>"Update Task", "id"=>"121"}
  Task Load (1.0ms)  SELECT  `tasks`.* FROM `tasks` WHERE `tasks`.`id` = 121  OR
DER BY created_at DESC LIMIT 1
   (0.0ms)  BEGIN
  Appointment Load (1.0ms)  SELECT `appointments`.* FROM `appointments` WHERE `a
ppointments`.`task_id` = 121 AND `appointments`.`id` = 134
   (0.0ms)  COMMIT

它仍然是2015年4月28日,上午7:00。

COMPLETE CONSOLE LOG:

Started PATCH "/tasks/131" for ::1 at 2015-04-21 11:41:49 +0100
Processing by TasksController#update as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"iP++ZG5VpAevK2awi6vaoixVDA8zc
BI7b0IducbwmvvnQyZ4ZN7XqtUg84/n21m/JmmC0+j0FCTfR7UtDb0VtA==", "task"=>{"category
_id"=>"1", "subcategory_id"=>"3", "title"=>"dddddddddddddddddddddddddddddd", "de
scription"=>"ddddddddddddddddddddddddddddddddddddddddddd", "county_id"=>"17", "a
rea_id"=>"723", "is_pay_per_hour"=>"0", "pay_offer"=>"22", "appointments_attribu
tes"=>{"0"=>{"start_date"=>"2/5/2015", "start_time"=>"02:00 AM", "duration"=>"0"
, "id"=>"145"}}}, "commit"=>"Update Task", "id"=>"131"}
  Task Load (0.0ms)  SELECT  `tasks`.* FROM `tasks` WHERE `tasks`.`id` = 131  OR
DER BY created_at DESC LIMIT 1
   (0.0ms)  BEGIN
  Appointment Load (0.0ms)  SELECT `appointments`.* FROM `appointments` WHERE `a
ppointments`.`task_id` = 131 AND `appointments`.`id` = 145
   (0.0ms)  COMMIT
Redirected to http://localhost:3000/tasks/131
Completed 302 Found in 11ms (ActiveRecord: 0.0ms)


Started GET "/tasks/131" for ::1 at 2015-04-21 11:41:49 +0100
Processing by TasksController#show as HTML
  Parameters: {"id"=>"131"}
  Task Load (0.0ms)  SELECT  `tasks`.* FROM `tasks` WHERE `tasks`.`id` = 131  OR
DER BY created_at DESC LIMIT 1
  Response Load (0.0ms)  SELECT `responses`.* FROM `responses` WHERE `responses`
.`task_id` = 131  ORDER BY created_at DESC
  User Load (0.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT
 1
  Area Load (0.0ms)  SELECT  `areas`.* FROM `areas` WHERE `areas`.`id` = 723 LIM
IT 1
  County Load (0.0ms)  SELECT  `counties`.* FROM `counties` WHERE `counties`.`id
` = 17 LIMIT 1
  Subcategory Load (0.0ms)  SELECT  `subcategories`.* FROM `subcategories` WHERE
 `subcategories`.`id` = 3 LIMIT 1
  Category Load (0.0ms)  SELECT  `categories`.* FROM `categories` WHERE `categor
ies`.`id` = 1 LIMIT 1
  Rendered tasks/_task_info.html.erb (11.0ms)
  CACHE (0.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
[["id", 1]]
  Rendered tasks/_question_negotiation_form.html.erb (67.0ms)
  Rendered collection (0.0ms)
  Appointment Load (1.0ms)  SELECT  `appointments`.* FROM `appointments` WHERE `
appointments`.`task_id` = 131  ORDER BY `appointments`.`id` ASC LIMIT 1
  CACHE (0.0ms)  SELECT  `appointments`.* FROM `appointments` WHERE `appointment
s`.`task_id` = 131  ORDER BY `appointments`.`id` ASC LIMIT 1  [["task_id", 131]]

  CACHE (0.0ms)  SELECT  `appointments`.* FROM `appointments` WHERE `appointment
s`.`task_id` = 131  ORDER BY `appointments`.`id` ASC LIMIT 1  [["task_id", 131]]

  CACHE (0.0ms)  SELECT  `appointments`.* FROM `appointments` WHERE `appointment
s`.`task_id` = 131  ORDER BY `appointments`.`id` ASC LIMIT 1  [["task_id", 131]]

  Rendered tasks/_first_appointment_info.html.erb (6.0ms)
  Rendered tasks/_appointed_worker.html.erb (9.0ms)
  Rendered tasks/show.html.erb within layouts/application (96.1ms)
  Rendered layouts/_header.html.erb (4.0ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 149ms (Views: 143.1ms | ActiveRecord: 1.0ms)


Started GET "/assets/application-8636d652720c67486ff35a011cfcda8b.css" for ::1 a
t 2015-04-21 11:41:49 +0100


Started GET "/assets/application-fb8ea68f36532aeef58f0517a0a44ab7.js" for ::1 at
 2015-04-21 11:41:49 +0100

有人可以帮我吗?

0 个答案:

没有答案