Java Script/Ajax implementation in Ruby on Rails


I need to populate my json response to form fields.I have device_ip and device_model json values in my controller.(Please see “200” validation in below Code).I need to populate those values into my form device.html.erb. My form has text field Device ip and Device model where json values needs to be populated.How to implement in rails? Do i need to write Javascript/ajax call for this?

def search
  @device =[:search])

  if @device.present?
    flash[:notice] = 'Device is available,Please add a device'
    redirect_to :action => :new

  if @device.nil?
    client =
    devAdd = params[:search].to_s
    @dataModel = "Device.deviceno"
    response = client.get_call(devAdd, @dataModel)
    http_status = response.code.to_s

  if http_status == "200"
    device_Ip = JSON.parse(response.body)["parameters"][0]["value"]
    device_Model = JSON.parse(response.body)["parameters"][1]["value"]

    flash[:notice] = 'Device is online'
    redirect_to :action => :new
  elsif http_status == "520"
    error_message = JSON.parse(response.body)["message"]
    flash[:notice] = error_message
    redirect_to :action => :new

Below is my form code:

<%= form_for(@device) do |f| %>
  <div class="container-fluid">
  <div class="row well-sm">
  <div class="col-sm-5">
    <%= f.label "Device IP:"%> <%= f.text_field :device_ip %>
    <%= f.label "Device Model:"%> <%= f.text_field :device_model %>
   <div class="container-fluid">
   <div class="row well-sm">
    <div class="col-sm-2">
     <%= f.submit class: "btn btn-info"%>



This is Rails 101 question that any tutorial should respond to. You’re using @device, so that’s where you find your value, e.g., @device[:device_ip] or @device[:device_model] depending on your model.

Your code has some issues and will not work as-is, e.g.,

if @device.present?
  # do stuff
else # here, equivalent to @device.nil?
  # use clientImpl (note the unconventional case, it should be ClientImpl
  # but then what client? Naming is important. You probably want to extract
  # code to a service:
  @device = DeviceWebService.from_web(params[:search])