I am stuck in a problem that cannot be so complicated, but I just don't get it.
Assuming I have 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
with such nested routes:
resources :devices do resources :notifications end
and a notification controller, for example:
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 creating notifications: The form works as an aspect. BUT: I want to achieve the second goal. Notifications must be resendable, so I have this in the notification index view:
<%= link_to 'Resend', device_notifications_path(number: notification.number, message: notification.message), :method => :post %>
But the check failed and im redirected to a new page without any filled in fields that tell me that this number is necessary, so there should not be a flaw that I don't get.
Parameters from the request:
[["user_id", xyz]] ["_method", "post"] ["authenticity_token", "myauthenticitytokenstring"] ["number", "+1555123456789"] ["action", "create"] ["controller", "notifications"] ["device_id", "9"] ["notification", {}]
(no message required)
I think the error lies in my notification_params method in the controller.
Can someone help me please?