Rails 4 with link_to and method mail with strong parameters

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 //rest omitted for brevity end 

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?

+4
source share
1 answer

I had a similar problem just now, and this is what worked for me:

 <%= link_to 'Resend', device_notifications_path(@notification.device_id, notification: { number: notification.number, message: notification.message }), :method => :post %> 

Basically, you need to wrap the data of your controller / model in a hash for the parameters of the controller. This is how the controller itself reads it. Also, don't you miss device_id in your device_notifications_path ?

 [["user_id", xyz]] ["_method", "post"] ["authenticity_token", "myauthenticitytokenstring"] ["action", "create"] ["controller", "notifications"] ["device_id", "9"] ["notification", {["number", "+1555123456789"]}] 

Now, by saying this, I just assume that device_id is in your URL route: http:\\localhost:3000\notifications\9

This is why device_id need not be in the hash itself. This is based only on what I am doing here without continuing view and routes . In general, this is due to the hash. Play around a bit and use p to print the data in your development.log file:

 def create p params p notification_params ... end 

Additionally, optionally, but not required, you can DRY change params def parameters using .require instead of .fetch as follows:

 private def notification_params params.require(:notification).permit(:number, :device_id, :message) end 
+4
source

Source: https://habr.com/ru/post/1497183/


All Articles