rails 4 with link_to and method post with strong parameters

一曲冷凌霜 提交于 2019-12-05 09:46:27

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 your controller/model data into a hash for the controller's params. That's just how the controller itself reads it. Also, are you not missing the 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, in saying that, I'm just assuming that device_id is located in your URL route: http:\\localhost:3000\notifications\9

This is why device_id would not have to be in the hash itself. This is just based on what I'm assuming here without more of the view and routes to go on. All in all, it does have to do with the hash. Play around a bit, and use p to print out data in your development.log to test:

def create
  p params
  p notification_params

  ...
end

In addition, optional but not required, you could DRY up your controller's params def using .require instead of .fetch like this:

private

def notification_params
  params.require(:notification).permit(:number, :device_id, :message)
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!