我一直试图弄清楚这个错误已经有一段时间了,并且已经看过类似的问题,但似乎仍无法解决这个问题。
这是添加用户权重的表单:
<%= form_for @weight do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :weight, placeholder: "Enter weight" %>
</div>
<div class="field">
<%= f.hidden_field :date, :value => Time.now %>
</div>
<%= f.submit "Add weight", class: "btn" %>
<% end %>
控制器:
class WeightsController < ApplicationController
before_action :correct_user, only: [:create, :update, :destroy]
def new
@weight = Weight.new
end
def create
@weight = current_user.weights.build(weight_params)
if @weight.save
flash[:success] = "Weight added!"
redirect_to current_user
else
flash[:notice] = "Can not add weight"
redirect_to current_user
end
end
def update
@weight = current_user.weights.order("created_at").last
if @weight.update_attributes(weight_params)
flash[:success] = "Weight updated"
redirect_to current_user
else
flash[:notice] = "Can not update weight"
redirect_to current_user
end
端
def edit
@weight = Weight.find(params[:id])
end
def show
@weight = current_user.weights.order("created_at").last
end
private
def weight_params
params.require(:weight).permit(:weight, :date, :user_id)
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
端
我一直收到这个错误:
Completed 500 Internal Server Error in 13ms (ActiveRecord: 4.4ms)
2016-03-24T02:58:16.916469+00:00 app[web.1]:
2016-03-24T02:58:16.916496+00:00 app[web.1]:
ActionView::Template::Error (First argument in form cannot contain nil or be empty):
2016-03-24T02:58:16.916497+00:00 app[web.1]: 1: <%= form_for @weight do |f| %>
2016-03-24T02:58:16.916498+00:00 app[web.1]: 2: <%= render 'shared/error_messages', object: f.object %>
2016-03-24T02:58:16.916499+00:00 app[web.1]: 3: <div class="field">
2016-03-24T02:58:16.916499+00:00 app[web.1]: 4: <%= f.label :weight, placeholder: "Enter weight" %>
2016-03-24T02:58:16.916500+00:00 app[web.1]: app/views/shared/_add_weight_form.html.erb:1:in `_app_views_shared__add_weight_form_html_erb___3897731957600552245_69888716309360'
请注意,我还有一个重量的编辑表格:
<%= form_for @weight do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :weight, placeholder: "Add weight" %>
</div>
<div class="field">
<%= f.hidden_field :date, :value => Time.now %>
</div>
<%= f.submit "Update Weight", class: "btn btn-primary" %>
<% end %>
这是我的新错误:
Started POST "/weights" for 98.7.88.139 at 2016-03-24 03:48:15 +0000
2016-03-24T03:48:15.107703+00:00 app[web.1]: Processing by WeightsController#create as HTML
2016-03-24T03:48:15.107835+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"rpBU/21hOohssd2Cqbme8rZ0PbsxC3rz9cCW/kLEMJbZa4F6qFjBIRMSX9wpZSu/joOfvDgqLIitEshO6cL/kQ==", "weight"=>{"weight"=>"55", "date"=>"2016-03-24 03:48:11 +0000"}, "commit"=>"Add weight"}
2016-03-24T03:48:15.110855+00:00 app[web.1]: User Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", nil]]
2016-03-24T03:48:15.112601+00:00 app[web.1]: Completed 404 Not Found in 5ms (ActiveRecord: 1.3ms)
2016-03-24T03:48:15.114746+00:00 app[web.1]:
2016-03-24T03:48:15.114753+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
2016-03-24T03:48:15.114754+00:00 app[web.1]: app/controllers/weights_controller.rb:55:in `correct_user'
答案 0 :(得分:1)
以下是代码应该如下的代码:
新表格:
<%= form_for :weight do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :weight, placeholder: "Enter weight" %>
<%= hidden_field_tag :id, :value => current_user.id %>
</div>
<div class="field">
<%= f.hidden_field :date, :value => Time.now %>
</div>
<%= f.submit "Add weight", class: "btn" %>
<% end %>
编辑表单:
<%= form_for @weight do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :weight, placeholder: "Add weight" %>
</div>
<div class="field">
<%= f.hidden_field :date, :value => Time.now %>
<%= hidden_field_tag :id, :value => current_user.id %>
</div>
<%= f.submit "Update Weight", class: "btn btn-primary" %>
<% end %>
在这种情况下,通过将id字段作为隐藏字段添加到表单中,您将无法获得user.id nil error for create&amp;更新方法和您的before_action过滤器将正常工作。 或强> 你可以简单地删除更新&amp;从before_action过滤器列表创建方法,如:
before_action :correct_user, only: [:destroy]
答案 1 :(得分:0)
您要求控制器在创建时验证用户。但是在后期路线中没有params[:id]
,即使有,也会有重量。
因此,如果您想验证用户,您可以在用户下嵌套权重,也可以使用会话来查找当前用户。 (假设你正在使用Devise,对吧?)
因此,产生错误的原因是before_action
过滤器。更改逻辑,或删除创建操作的过滤器