我陷入了一个不可能那么复杂的问题,但我只是没有把事情做好。
假设我有两个模型:
class Notification < ActiveRecord::Base
belongs_to :device
validates :number, presence: true
end
和
class Device < ActiveRecord::Base
belongs_to :user
has_many :notifications, :dependent => :destroy
//rest omitted for brevity
end
嵌套路由如下:
resources :devices do
resources :notifications
end
和这样的通知控制器:
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 = @device.notifications.build(notification_params)
if @notification.save
redirect_to [@notification.device, @notifications], notice: 'Notification was successfully created.'
else
render action: 'new'
end
end
private
def load_notification
@notification = Notification.new(notification_params)
end
def set_device
@device = Device.find(params[:device_id])
end
def notification_params
params.fetch(:notification, {}).permit(:number, :device_id, :message)
end
end
现在,当涉及到创建通知时:表单按方面工作。但是:我想实现第二个目标。通知应该是可重新发送的,所以我在通知索引视图中有这个:
<%= link_to 'Resend', device_notifications_path(number: notification.number, message: notification.message), :method => :post %>
但是验证失败并且我重定向到新页面而没有任何预填充字段告诉我该数字是必需的,因此必须缺少一些我没有得到的明显内容。
请求中的参数:
[["user_id", xyz]]
["_method", "post"]
["authenticity_token", "myauthenticitytokenstring"]
["number", "+1555123456789"]
["action", "create"]
["controller", "notifications"]
["device_id", "9"]
["notification", {}]
(不需要留言)
我认为错误在于控制器中的 notification_params 方法。
任何人都可以帮助我吗?