当参数存在时ActionController :: ParameterMissing

时间:2018-08-31 05:08:38

标签: ruby-on-rails reactjs

因此,无论出于何种原因,我都会收到此错误。它说参数“ User”丢失了,但是我可以在那里很清楚地看到它。另外,我不确定为什么“ user”参数被列出两次,第二个为空,但是我认为这是造成它的原因之一。

Started POST "/users" for 127.0.0.1 at 2018-08-30 21:56:48 -0700                                                       
Processing by UsersController#create as HTML               
  Parameters: {"params"=>{"user"=>{"username"=>"fsda", "first_name"=>"fdsa", "last_name"=>"fds", "email"=>"fdsa", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}, "headers"=>{"Content-Type"=>"application/json"}, "user"=>{}}                                                        
Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)     



ActionController::ParameterMissing (param is missing or the value is empty: user):                                     

app/controllers/users_controller.rb:18:in `user_params'    
app/controllers/users_controller.rb:4:in `create'          

这是从单独的服务器(React客户端)发出呼叫的代码段

 25     ax.post('/users', {                                                                                            |~                                                                                                                     
 26       params: {                                                                                                    |~                                                                                                                     
 27         user: this.state                                                                                           |~                                                                                                                     
 28       },                                                                                                           |~                                                                                                                     
 29       headers: {                                                                                                   |~                                                                                                                     
 30         "Content-Type": 'application/json'                                                                         |~                                                                                                                     
 31       }                                                                                                            |~                                                                                                                     
 32     })           

ax在一个单独的文件中定义:

  1 import axios from 'axios';                                                                                    
  2                                                                                                                    
  3 export default axios.create({                                                                                
  4   baseURL: 'http://localhost:3000'                                                                                
  5 })  

这是Rails控制器

 3   def create                                                                                                                                                                                                                              
  4     @user.new(user_params)                                                                                                                                                                                                                
  5     if @user.save                                                                                                                                                                                                                         
  6       json_response(@user, :created)                                                                                                                                                                                                      
  7     end                                                                                                                                                                                                                                   
  8   end                                                                                                                                                                                                                                     
  9                                                                                                                                                                                                                                           
 10   def show                                                                                                                                                                                                                                
 11     @user = User.find(params[:id])                                                                                                                                                                                                        
 12     render json: @user                                                                                                                                                                                                                    
 13   end                                                                                                                                                                                                                                     
 14                                                                                                                                                                                                                                           
 15   private                                                                                                                                                                                                                                 
 16                                                                                                                                                                                                                                           
 17   def user_params                                                                                                                                                                                                                         
 18     params.require(:user).permit(:username, :email, :first_name, :last_name, :password_digest)                                                                                                                                            
 19   end     

很抱歉格式化不正确。除非有人知道更好的方法,否则从vim复制粘贴确实很尴尬。

解决方案:

@user.new(user_params)更改为@user = User.new(user_params

params: { user: {...} }更改为user: {...}

0 个答案:

没有答案