控制器操作中未读取参数

时间:2019-01-08 04:29:49

标签: ruby-on-rails parameters

大家好,我设置了状态选择器,最终目的是让用户选择一个州,下面的选择标签使用城市状态的宝石https://github.com/loureirorg/city-state来填充该州的城市

我在请求中获得了正确的参数,但控制器方法未读取它们

registrations-controller.rb

def new
  super
  @cities = CS.get(:us,params[:state]) 
  puts @cities                 
end

这不是选择:state参数

new.html.erb

 <%= select_tag :state,options_for_select(CS.states(:us)), 
  {:class => "signup-input-container--input", :id => "state-picker"} %>

routes.rb

devise_scope :family do
  get 'states', to: 'families/registrations#new'
end

main.js

  var state = document.getElementById("state-picker");
  state.addEventListener("change", function() {
     $.ajax({
        url: "/states?state=" + state.value,
        type: "GET"
     })
  })

有人知道为什么会这样吗?我还记录了参数并得到

 "<ActionController::Parameters {"state"=>"Colorado", "controller"=>"families/registrations", "action"=>"new"} permitted: false> " 

2 个答案:

答案 0 :(得分:0)

docs建议cities方法想要该状态的符号。您正在使用simplified get方法,但它可能还希望使用符号而不是城市名称作为String(这是我们在您的params中看到的内容)。 / p>

因此main.js可能具有以下内容:

 $.ajax({
    url: "/states?state=" + state.key, // 'state.value' was the string of the state name, we want the two-letter code.
    type: "GET"
 })

您可能还必须更改此行

@cities = CS.get(:us,params[:state]) 

对此

@cities = CS.get(:us,params[:state].to_s) 

答案 1 :(得分:0)

如果您在permitted: false中获得了ActionController::Parameters,可能是因为您没有像Strong Parameters所要求的那样在控制器中将参数(在这种情况下为:state列入白名单

从文档中

  

具有强参数的操作控制器参数除非已列入白名单,否则禁止在Active Model质量分配中使用。

示例:

private
  def person_params
    params.require(:person).permit(:name, :age)
  end