使用强参数时如何从 fields_for 提交中获取单个属性的值? Rails 4
How to get value of a single attribute from a fields_for submission when using strong parameters? Rails 4
如果问题的标题可能有点令人困惑,我深表歉意,但我真的很难用更好的词来表达它。
我正在尝试访问 fields_for
提交中的单个属性 在 控制器中。
我的表单如下所示:
= form_for @match, :url => matches_search_path, remote: true do |f|
= f.select :sport_id, options_from_collection_for_select(Sport.all, "id", "name"), {:prompt => "Select Sport..."}, class: "form-control"
= f.fields_for @match.build_location do |g|
= g.text_field :zip, :class => "form-control", :value => @user.location.zip, :placeholder => "Zip"
= f.text_field :max_players, :class => "form-control", :placeholder => "Player Limit (in total)"
= f.submit "Search", :class => "btn btn-info"
MatchesController 是这样的:
class MatchesController < ApplicationController
before_filter :current_user
def index
@match = Match.new
@user = current_user
end
def new
@match = Match.new
@match.build_location
end
def create
@user = User.find(session[:user_id])
@match = @user.matches.build(match_params)
if @match.save
redirect_to :root, :notice => "Match created."
else
render :new, :alert => "Failed to create match."
end
end
def search
# Get all matches for the searched sport
@results = Match.sport_id(match_params[:sport_id])
#This is where the problem is:
@coordinates = Geocoder.coordinates(match_params[:location][:zip])
@results = @results.select {|result| result.location.distance_from(@coordinates)}
respond_to :js
end
private def match_params
params.require(:match).permit(:sport_id, :name, :description, :choose_teams, :keeping_score, :max_players, :time, location_attributes: [:address, :city, :state, :zip])
end
end
路线没问题:
# Matches
resources :matches
post 'matches/search', as: :matches_search
表单提交对MatchesController
中的search
方法的ajax调用就好了。问题是我得到:
undefined method `[]' for nil:NilClass
行
@coordinates = Geocoder.coordinates(match_params[:location][:zip])
我尝试用谷歌搜索这个问题,但我发现最接近这个问题的是 here,在尝试访问 "hash within a hash" 时使用:
params[:outer_hash][:inner_hash]
当我使用 params[:location][:zip], match_params[:location][:zip]
或 match_params[:location_attributes][:zip]
时不起作用。
我已确认 表单中的信息已被 发送至控制器。我似乎无法正确访问它。
我到底做错了什么?
编辑:
这是表单的回复:
(我屏蔽了authenticity_token)
{"utf8"=>"✓", "authenticity_token"=>"XXXXXXXXXXXXXXXXXXXXXXX",
"match"=>{"sport_id"=>"4",
"location"=>{"zip"=>"11211"},
"max_players"=>""},
"commit"=>"Search"}
我认为正在发生的事情是您的 match_params
没有让 location
参数(我猜匹配 has_one
位置)。更改行:
@coordinates = Geocoder.coordinates(match_params[:location][:zip])
至:
@coordinates = Geocoder.coordinates(params[:match][:location][:zip])
或者更好的是,将您的 match_params
更改为:
params.require(:match).permit(:sport_id, :name, :description, :choose_teams, :keeping_score, :max_players, :time, location: [:address, :city, :state, :zip])
如果问题的标题可能有点令人困惑,我深表歉意,但我真的很难用更好的词来表达它。
我正在尝试访问 fields_for
提交中的单个属性 在 控制器中。
我的表单如下所示:
= form_for @match, :url => matches_search_path, remote: true do |f|
= f.select :sport_id, options_from_collection_for_select(Sport.all, "id", "name"), {:prompt => "Select Sport..."}, class: "form-control"
= f.fields_for @match.build_location do |g|
= g.text_field :zip, :class => "form-control", :value => @user.location.zip, :placeholder => "Zip"
= f.text_field :max_players, :class => "form-control", :placeholder => "Player Limit (in total)"
= f.submit "Search", :class => "btn btn-info"
MatchesController 是这样的:
class MatchesController < ApplicationController
before_filter :current_user
def index
@match = Match.new
@user = current_user
end
def new
@match = Match.new
@match.build_location
end
def create
@user = User.find(session[:user_id])
@match = @user.matches.build(match_params)
if @match.save
redirect_to :root, :notice => "Match created."
else
render :new, :alert => "Failed to create match."
end
end
def search
# Get all matches for the searched sport
@results = Match.sport_id(match_params[:sport_id])
#This is where the problem is:
@coordinates = Geocoder.coordinates(match_params[:location][:zip])
@results = @results.select {|result| result.location.distance_from(@coordinates)}
respond_to :js
end
private def match_params
params.require(:match).permit(:sport_id, :name, :description, :choose_teams, :keeping_score, :max_players, :time, location_attributes: [:address, :city, :state, :zip])
end
end
路线没问题:
# Matches
resources :matches
post 'matches/search', as: :matches_search
表单提交对MatchesController
中的search
方法的ajax调用就好了。问题是我得到:
undefined method `[]' for nil:NilClass
行
@coordinates = Geocoder.coordinates(match_params[:location][:zip])
我尝试用谷歌搜索这个问题,但我发现最接近这个问题的是 here,在尝试访问 "hash within a hash" 时使用:
params[:outer_hash][:inner_hash]
当我使用 params[:location][:zip], match_params[:location][:zip]
或 match_params[:location_attributes][:zip]
时不起作用。
我已确认 表单中的信息已被 发送至控制器。我似乎无法正确访问它。
我到底做错了什么?
编辑:
这是表单的回复: (我屏蔽了authenticity_token)
{"utf8"=>"✓", "authenticity_token"=>"XXXXXXXXXXXXXXXXXXXXXXX",
"match"=>{"sport_id"=>"4",
"location"=>{"zip"=>"11211"},
"max_players"=>""},
"commit"=>"Search"}
我认为正在发生的事情是您的 match_params
没有让 location
参数(我猜匹配 has_one
位置)。更改行:
@coordinates = Geocoder.coordinates(match_params[:location][:zip])
至:
@coordinates = Geocoder.coordinates(params[:match][:location][:zip])
或者更好的是,将您的 match_params
更改为:
params.require(:match).permit(:sport_id, :name, :description, :choose_teams, :keeping_score, :max_players, :time, location: [:address, :city, :state, :zip])