我陷入了一个不会那么复杂的问题,但我只是没有把事情做对.
假设我有两个型号:
class Notification < ActiveRecord::Base
belongs_to :device
validates :number, presence: true
end
Run Code Online (Sandbox Code Playgroud)
和
class Device < ActiveRecord::Base
belongs_to :user
has_many :notifications, :dependent => :destroy
//rest omitted for brevity
end
Run Code Online (Sandbox Code Playgroud)
使用嵌套路线如下:
resources :devices do
resources :notifications
end
Run Code Online (Sandbox Code Playgroud)
和这样的通知控制器:
class NotificationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_device, :only => [:index, :new, :create]
before_filter :load_notification, only: :create
load_and_authorize_resource :device
load_and_authorize_resource :notification, :through => :device
def index
end
def new
@notification = @device.notifications.build
end
def create
params.each do |param|
logger.debug param
end
@notification …Run Code Online (Sandbox Code Playgroud)