根据控制器不工作状态检查表格数据参数

时间:2014-01-06 09:22:27

标签: ruby-on-rails ruby rails-activerecord

我的控制器中有一个由before_filter触发的简单操作,它检查用户是否在相应的活动记录/模型表单提交中选择了某个区域。遗憾的是,它不起作用,而忽略了if声明中我试图查看值是否为"不等于"到某个区域,分别是params[:country_id]params[:state_id]。实际工作的唯一部分是检查正确的计划@plan == 'plus'

控制器:

before_filter :check_region, :only => :create

def check_region
    @plan = params[:plan]
    if @plan == 'plus' && params[:country_id] != '1039' && params[:state_id] != ['52', '53', '60']
      redirect_to page_path('plans')
      flash[:notice] = "Unfortunatley the plan isn't available at this time in the region you selected."
    else

    end
  end

plus计划类型上提交表单时,我还会包含rails日志中的摘录。

Started POST "/users" for 127.0.0.1 at 2014-01-06 01:16:50 -0800
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"eYfsGY7vnItcRRyLEnFA9wJlkdZA5DxjPF/eQdwNm9M=", "plan"=>"plus", "user"=>{"fullname"=>"Example User", "email"=>"exampleuser@emailaddress.net", "country_id"=>"1039", "state_id"=>"52", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}
Redirected to http://localhost:3000/plans
Filter chain halted as :check_region rendered or redirected
Completed 302 Found in 3ms (ActiveRecord: 0.0ms)

因此,在这种情况下,因为country_idstate_id的值是" 1039"和" 52"它应该没有触发取消表单提交的重定向(并且会改为使用create方法)。

2 个答案:

答案 0 :(得分:2)

在上面的代码中你访问了params [:country_id]和params [:state_id]但是如果你检查传递的params它在用户范围内,所以它应该是params [:user] [:state_id]和params [:用户] [:COUNTRY_ID]

答案 1 :(得分:0)

没有正确访问参数,你还需要检查数值是否在数组中

if @plan == 'plus' && params[:user][:country_id] != '1039' && params[:user][:state_id].in?['52', '53', '60']