强参数和在Rails中查找4

时间:2015-11-19 19:34:07

标签: ruby-on-rails ruby-on-rails-4 activerecord strong-parameters

我在自定义控制器中遇到强参数问题。我了解如何在新操作或更新操作中使用强参数。但是,在我的自定义操作中,我似乎无法弄清楚这是否是对params []哈希的不安全使用。

我的视图重定向到具有订单ID和操作号的控制器:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCellWithIdentifier("meetingListCell", forIndexPath: indexPath) as! MeetingListCell
     let meeting = meetingList![indexPath.row]

     cell.meeting.text = meeting.text

     if(meeting.isExpired()) {
         cell.meetingIconImageView.image = UIImage(named: "meeting-expired")
     } else {
         cell.meetingIconImageView.image = UIImage(named: "meeting-new")
     }
     cell.setNeedsLayout()
     cell.setNeedsDisplay()
     return cell
 }

我的确认支付控制器如下:

link_to 'Confirm', confirmpayment_confirm_path(order: order, operacion: order.operacion), :data => { confirm: 'Are you sure?' }

问题是:

我在确认操作中的任何地方都没有使用class ConfirmpaymentController < ApplicationController before_action :authenticate_user! def lookup authorize! :lookup, :confirmpayment @orders=Order.where(:status => 'PENDING') end def confirm authorize! :confirm, :confirmpayment @order=Order.find(params[:order]) @order.payment_id = params[:operacion] @order.confirm_payment_date = DateTime.now() @order.save end def order_params params.require(:order).permit(:order, :operacion) end end ,因为这不是新订单。我正在使用参数来查找正确的顺序并确认它。这样安全吗?或者我错过了什么?

2 个答案:

答案 0 :(得分:1)

关于使用参数的方式,我认为它没有任何问题。但是关于安全问题,您可能想要考虑用户可以通过将Order参数更改为不属于他的内容来更改任何order信息的情况。

在这种情况下,您需要将查询限制为Order,使他只能确认属于他的订单。

答案 1 :(得分:0)

强参数:防止意外暴露不应暴露的内容。它们通常在您创建或更新模型时使用,这样可以避免输入参数,但这是不允许的。

我有一些建议:

  1. 英文最好是编码:operacionoperation
  2. 检查风格 在您的代码ConfirmpaymentController中进行分类 ConfirmPaymentController
  3. 你可以看到:best practices and style prescriptions for Ruby on Rails 4:)