小编Jul*_*fke的帖子

rails 4带有link_to和带有强参数的方法帖子

我陷入了一个不会那么复杂的问题,但我只是没有把事情做对.

假设我有两个型号:

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)

http-post link-to strong-parameters ruby-on-rails-4

5
推荐指数
1
解决办法
1744
查看次数