无法管理嵌套属性

时间:2016-09-25 16:15:38

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

我一直在管理嵌套属性。我是铁杆的新人,非常感谢任何建议或指出我的错误。所以我有一个这样的模型Ticket(它还有enum整数字段状态,引用stuff_id body等)

class Ticket < ActiveRecord::Base
  include Generator
  has_paper_trail only:[:stuff_id, :status], on:[:update] 
  has_many :replies
  accepts_nested_attributes_for :replies
end

我也有回复模式

class Reply < ActiveRecord::Base
  belongs_to :ticket
  validates :ticket_id, :body, presence: true
end

我的目标是让机会为故障创建回复,并可选择更改故障单的状态。目前我尝试以这种方式管理它: 的routes.rb

resources :tickets do
  patch 'stuff_update', on: :member
  resources :replies
end

dashboards_controller.rb

class DashboardsController < ApplicationController
  #before_action :authorize  
  before_action :method 

  def opened       
  end 

  protected 
  def method
   @tickets = Ticket.send(action_name.to_sym).includes(:replies)
  end
  def ticket_params
    params.require(:ticket).permit(:status, replies_attributes: [:body])
  end
end

tickets_controller.rb

class TicketsController < ApplicationController
  #before_action :authorize, only:[:stuff_update]
  before_action :load_ticket, only:[:show, :update, :stuff_update]  

  respond_to :js

  def stuff_update
    @ticket.update(ticket_params)          
  end
  protected
  def ticket_params
    params.require(:ticket).permit(:name, :subject, :email, :status, :body, :department, :stuff_id, replies_attributes: [:id, :body])
  end

  def load_ticket
    @ticket = Ticket.find(params[:id])
  end
end

最后查看

-@tickets.each do |t|
  .panel.panel-default
    p=t.subject
    =form_for t,{ url: "/tickets/#{t.id}/stuff_update"}, {method: :patch} do |f|
      = f.label :status, class: 'label_hidden'
      = f.select :status, Ticket.statuses.keys, {}, {class:'form-control'}
      = f.fields_for t.replies.build do |ff|  
        = ff.label :body
        = ff.text_field :body 
      = f.submit "Submit"

我对未经许可的参数回复有错误。在我看来,除了这个错误,我的代码中还有很多错误。我迫切需要帮助。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

好的,我发现的解决方案非常简单

FirebaseAuth

其他一切都非常合情合理。