带有link_to的rails 4和带有强参数的方法post [英] rails 4 with link_to and method post with strong parameters

查看:187
本文介绍了带有link_to的rails 4和带有强参数的方法post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了一个不会那么复杂的问题,但我只是没有把事情搞定。

I'm stuck in a problem which can't be that complicated, but I'm just not getting things right.

假设我有两个型号:

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

现在,在创建通知时:
表单按预期工作。但是:我想实现第二个目标。
通知应该是可重新发送的,所以我在通知索引视图中有这个:

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.

请求中的参数:

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

(不需要留言)

我认为错误在于控制器中的notification_params方法。

I think the error lies in my notification_params method in the controller.

有人可以帮助我吗?

推荐答案

我刚才有类似的问题,这对我有用:

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 %>

基本上,您需要将控制器/模型数据包装到控制器参数的散列中。这就是控制器本身如何读取它。另外,您是否错过了 device_notifications_path 中的 device_id

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"]}]

现在,在说,我只是假设 device_id 位于您的URL路由中:
http:\\localhost:3000 \\\
otifications \ 9

Now, in saying that, I'm just assuming that device_id is located in your URL route: http:\\localhost:3000\notifications\9

这就是为什么 device_id 不必在哈希本身。这只是基于我在这里假设的,没有更多的视图路由继续。总而言之,它确实与哈希有关。玩一下,并使用 p 打印出你的development.log中的数据进行测试:

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

此外,可选但不是必需的,您可以使用 .require 而不是<$来干扰控制器的参数def c $ c> .fetch 像这样:

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

这篇关于带有link_to的rails 4和带有强参数的方法post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆