简体   繁体   English

我正在尝试在其余客户端中使用post方法将json数据传递到Rails控制器

[英]i'm trying to pass json data to rails controller using post method in rest client

Here i'm trying to save json data to sqlite database using rails controller, but i'm not getting json data to controller parameters 在这里,我正在尝试使用Rails控制器将json数据保存到sqlite数据库,但是我没有将json数据保存到控制器参数中

In a specific controller I have the below list of params: 在特定的控制器中,我具有以下参数列表:

Parameters: {"person"=>"{\"name\":\"akhil\",\"profession\":\"it\",\"address\":\"hyderabad\",\"mobilenum\":67588}"}

Controller 控制者

def createPerson

  puts "parameters are : "+params[:person].to_s
  user_params = ActiveSupport::JSON.decode(params[:person])
  puts "parameters name:"+user_params[:name].to_s
  @person = Person.new(name: user_params[:name], profession: 
  user_params[:profession], address: user_params[:address], mobilenum: 
  user_params[:mobilenum]) 
  @person.save

end  

It is showing below error 它显示下面的错误

(no implicit conversion of nil into String) (不将nil隐式转换为String)

I'm getting the nil value in user_params[:name].to_s 我在user_params [:name] .to_s中得到nil值

Could you please help me to solve this 你能帮我解决这个问题吗

Seems like all you need to do is to create a new Person record after submitting a form. 似乎您需要做的就是提交表单后创建新的“ Person记录。 Well, probably you would want to use strong params and make a little refactor, so your controller will look something like this: 好吧,可能您想使用强大的参数并进行一些重构,因此您的控制器将如下所示:

class PersonsController < ApplicationController
  # you can name your action simply `create`, so you can use resource routing
  def create
    # most probably you don't need to make person an instance variable
    person = Person.new(person_params)
    # and here it is a good practice to check if you really saved your person
    if person.save
      # do something to tell user that the record is saved, e.g.
      flash[:success] = 'person has been saved'
    else
      # show user why your person record is not saved, e.g.
      flash[:error] = "person cannot be saved: #{person.errors.full_messages.to_sentence}"
    end
  end

  private

    # and here is the method for your permissible parameters
    def person_params
      params.require(:person).permit(:name, :profession, :address, :mobilenum)
    end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将JSON数据作为发布方法从Rails控制器发送到Web服务 - Send JSON data as post method from rails controller to a web service Rails 4试图解析控制器中的POST JSON - Rails 4 trying to parse POST JSON in controller 在Rails中,我试图将属性传递给Model的“ new”方法,然后传递给“ create method”。 最有效的方法是什么? - In Rails, I'm trying to pass an attribute to a Model's “new” method, then to the “create method”. What's the most efficient way to do this? Rails 6:仅当我使用我的控制器中的方法时才渲染部分内容 - Rails 6: Render a partial if only I'm using a method from my controller 使用Rest Client的Rails 5发布文件 - Rails 5 post file with Rest Client Rails Ajax调用:将JSON传递给单独的控制器方法 - Rails Ajax Call : Pass JSON to separate controller method Rspec控制器测试-如何将参数传递给我正在测试的方法 - Rspec controller test - how to pass arguments to a method I'm testing 当我尝试使用Mongoid更新带有嵌入式文档的用户时,未定义的方法__metadata在Rails 4中进行设计 - Undefined method `__metadata' when I'm trying to update a User with embedded document using mongoid, devise in Rails 4 我正在尝试使用GET方法发送值,但是Rails坚持要求调用POST方法 - I am trying to send values using GET method, but Rails insist to call the POST method Webservice POST不使用Rails 2.3.8中的rest客户端显示请求的参数 - Webservice POST not displaying requested parameters using rest client in rails 2.3.8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM