我是ruby on rails的新手,我目前正在开发门户网站的一部分。我遇到了一些我无法找到解决方案的错误。
第一个错误:
ActionController::ParameterMissing (param is missing or the value is empty: user):
app/controllers/admin/manage_users_controller.rb:97:in `user_params'
app/controllers/admin/manage_users_controller.rb:32:in `update'
第二个错误:
ArgumentError (When assigning attributes, you must pass a hash as an argument.):
app/controllers/admin/manage_users_controller.rb:32:in `update'
我的manage_users_controller:
def edit
if request.format.json?
t = @user
@user = {:id => t.id, :email => t.email}
end
respond_to do |format|
format.json { render :json => @user, :status => 200}
format.html
end
end
def update
@user.attributes = user_params
if @user.save
respond_to do |format|
format.json {render :json => {:redirect_to => admin_manage_users_path}, :status => 200 }
end
else
respond_to do |format|
format.json { render :json => {:errMsg => print_out_message('form-update','user'), :errors => @user.errors}, :status => 400 }
end
end
end
def user_params
return params.require(:user).permit(:user_id, :email)
end
对于第一个错误,我不确定为什么缺少参数。 对于第二个错误,我已阅读其他帖子,我需要在user_params中使用require和permit方法。但是,我仍然无法弄清楚究竟是什么问题。
任何帮助都将深表感谢。谢谢。
-------------------------- UPDATE -------------------- ---------
视图中的edit.html.erb
<div class="row" ng-controller="AdminManageUsersCtrl">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="rs-content" data-ng-init="loadUser()">
<div class="portlet listing" cg-busy="loadUser" ng-cloak>
<div class="portlet-body">
<form name="form" ng-submit="uploadUser()" confirm-on-exit>
<%= render "admin/manage_users/form" %>
<div class="row row-space-top-1">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<button type="submit" class="btn red-mint" ng-click="form.$setPristine()">Update</button>
<%= link_to "Cancel".html_safe, admin_manage_users_path, :class => "btn dark btn-outline" %>
<%= link_to 'Remove', URI::unescape(admin_manage_user_path(:users => '{{user.id}}')), :method => :delete, :class => "pull-right btn red btn-outline", :data => {:confirm => 'Are you sure you want to delete this user?', "ng-click"=>"form.$setPristine()"} %>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
和_form.html.erb
<div class="col-md-12 space-1" ng-class="{ 'has-error' : form['email'].$invalid }">
<label class="control-label">Email</label>
<div>
<div class="input-icon right">
<i ng-show="form['email'].$invalid" class="fa fa-warning tooltips" data-original-title="{{errors['email']}}" data-container="body"></i>
<input ng-model= "user.email" name="email" type="text" class="form-control">
</div>
</div>
</div>
app.js中的
$scope.uploadUser = function() {
$scope.errors = [];
AdminManageUsers.$patch('update', $scope.user).then(function(response) {
$window.location.href = response.redirectTo;
}, function(response) {
angular.forEach(response.data.errors, function(errors, key) {
$scope['form'][key].$invalid = true;
$scope.errors[key] = errors.join(', ');
});
alertMessage.danger(response.data.errMsg, 1, 10000);
});
}
这是route.rb中的路由
resources :manage_users, :only => [:index,:destroy], :param => :id
match "manage_users/data" => "manage_users#data", via: [:post], :defaults => { :format => 'json' }
match "manage_users/load" => "manage_users#load", via: [:get], :defaults => { :format => 'json' }
match "manage_users/:id/edit" => "manage_users#edit", via: [:get]
match "manage_users/:id/update" => "manage_users#update", via: [:patch]
抱歉,我发布了很多代码,但我认为它们都是相关的。 说实话,我实际上是在尝试遵循我的导师采用的编码风格,所以他使用angularJS来设计表单,所以我仍然有一些麻烦理解一切如何联系在一起。
答案 0 :(得分:0)
错误本身是由strong_params
方法引起的:
params.require(:user).permit(:user_id, :email)
具体来说, require 参数意味着控制器期望一个以"user"
为主要元素的params哈希:
params: {
"user" => {
"user_id" => "1",
"email" => "test@email.com"
}
}
由于您没有进行上述设置,因此错误消息显示您在params哈希中没有user
。
<强>解决方案强>
确保你通过你的参数传递正确的物体。您应该进行以下设置:
#app/views/admin/manage_users/edit.html.erb
<%= form_for @user do |f| %>
<%= f.text_field :email %>
<%= f.submit %>
<% end %>
与此同时,你所做的一切都是有缺陷的;你最好使用以下内容:
#app/controllers/admin/manage_users_controller.rb
class Admin::ManageUsersController < ApplicationController
respond_to :js, :json, :html
before_action :set_user
def edit
respond_with @user
end
def update
respond_to do |format|
if @user.update user_params
format.json {render :json => {:redirect_to => admin_manage_users_path}, :status => 200 }
else
format.json { render :json => {:errMsg => print_out_message('form-update','user'), :errors => @user.errors}, :status => 400 }
end
end
end
private
def set_user
@user = User.find params[:id]
end
def user_params
params.require(:user).permit(:user_id, :email) #-> Ruby automatically returns the last line of a method
end
end