或者,这似乎正在发生。我的生产环境中出现错误,我无法在开发过程中重新创建。基本上,我正在尝试将Booker与预订相关联。如果布克不存在,我想邀请他/她。生产中丢失参数?
# GET /bookings/new
def new
@booking = Booking.new
authorize @booking
@booking.venue = Venue.new
@booker = User.new
end
相关创建代码;
def create
@booking = Booking.new(booking_params)
authorize @booking
booker_found = set_booker
而且set_booker私有方法
def set_booker
logger.info "booker: #{booking_params.inspect}"
# set existing user as booker or prepare inviting a new user
booker_found = false
@booker = User.find_by_email(booking_params[:user][:email])
等 最后一行是我在生产中出现错误,因为booking_params[:user]
不存在。我试图重置我的数据库在开发环境和代码工作正常。然而在制作中我总是会得到NoMethodError (undefined method '[]' for nil:NilClass)
这是我的相关表单代码;
<%= simple_form_for(@booking, :wrapper => :bootstrap3) do |f| %>
<%= f.simple_fields_for @booker do |user_form| %>
<%= user_form.input :email, label: "Booker e-mail" %>
<% end %>
这是记录仪在开发中显示的内容;
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lala", "booking"=>{"name"=>"Mah Booking", "date"=>"02-10-2014", "venue_name"=>"Meeeeeeeeeeee", "venue_id"=>"", "user"=>{"email"=>"[email protected]"}, "artist_fee"=>"0.00"}, "commit"=>"Create Booking", "locale"=>"en"}
booker: {"date"=>"02-10-2014", "name"=>"Mah Booking", "artist_fee"=>"0.00", "venue_id"=>"", "venue_name"=>"Meeeeeeeeeeee", "user"=>{"email"=>"[email protected]"}}
这是从我的production.log
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yada", "booking"=>{"name"=>"tttt", "date"=>"02-10-2014", "venue_name"=>"meee", "venue_id"=>"", "user"=>{"email"=>"[email protected]"}, "artist_fee"=>"0.00"}, "commit"=>"Create Booking", "locale"=>"en"}
booker: {"name"=>"tttt", "date"=>"02-10-2014", "artist_fee"=>"0.00", "venue_id"=>""}
我不知道为什么顺序有所不同,另外,它似乎却“切断”的venue_id,这显然是造成后错误。有没有人见过这样的行为?
编辑: 这是我的私人booking_params方法
def booking_params
params.require(:booking).permit(*policy(@booking || Booking).permitted_attributes)
end
而且权威人士政策;
def permitted_attributes
[:status,
:date,
:name,
:get_in_time_string,
:soundcheck_time_string,
:dinner_time_string,
:show_time_string,
:show_time_end_string,
:artist_fee,
:venue_id,
:venue_name,
:act_ids => [],
user: [ :id, :email ]
]
end
就像我说的,相同的代码在开发中工作正常。我可以重现问题的唯一方法是从我的permitted_attributes
方法中删除用户参数,但后来实际上我得到了“未经许可的参数”错误。为“用户”定义允许的属性的正确方法是什么?完全失去了这一个。
'params'哈希有一个'booking'键,其中包含一个Hash,其中包含键'user'的键/值对。你强大的参数配置必须是不正确的,并且不允许访问这些参数? (就像@Niall指出的那样) – MrYoshiji 2014-09-25 14:13:43
你在服务器上使用与开发中相同的ruby版本吗? – nicohvi 2014-10-05 04:59:06