2016-09-25 97 views
1

我堅持管理嵌套屬性。我是一個新人,願意提供任何建議或指出我的錯誤。所以我有這樣一個模型票(它也有枚舉整型字段狀態,引用stuff_id體等)無法管理嵌套屬性

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 

最後查看

[email protected] 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" 

對於未經許可的參數回覆,我有一個錯誤。此外,在我看來,除了這個錯誤之外,我的代碼中還有很多錯誤。我非常需要幫助。請幫助我解決這個問題。

回答

0

好吧,我已經找到了解決辦法很簡單

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

一切是相當同文。