I'm stuck in a problem which can't be that complicated, but I'm just not getting things right.
Assuming I've got two Models:
class Notification < ActiveRecord::Base
belongs_to :device
validates :number, presence: true
end
and
class Device < ActiveRecord::Base
belongs_to :user
has_many :notifications, :dependent => :destroy
//rest omitted for brevity
end
with nested routes like so:
resources :devices do
resources :notifications
end
and a notifications controller like so:
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
Now when it comes to create notifications: The Form works as aspected. BUT: I want to achieve a second goal. Notifications should be resendable, so I have this in notifications index view:
<%= link_to 'Resend', device_notifications_path(number: notification.number, message: notification.message), :method => :post %>
But the validation fails and im redirected to new page without any prefilled fields telling me that number is required, so there must be missing something obvious I don't get.
The params from the request:
[["user_id", xyz]]
["_method", "post"]
["authenticity_token", "myauthenticitytokenstring"]
["number", "+1555123456789"]
["action", "create"]
["controller", "notifications"]
["device_id", "9"]
["notification", {}]
(Message is not required)
I think the error lies in my notification_params method in the controller.
Can anyone help me, please?