1

I am new to ruby on rails and I created a simple login & registration app using ruby on rails.

I want to pass the id, username & password that I take from the form (using ruby on rails) and pass it to my php codeigniter code that inserts it to the database and fetches it back and displays as JSON output.

I have done the latter part where my php code inserts a post data to the phpmyadmin database and fetches it (used postman POST to pass value of id, username and password)

However, I no longer want to use postman. I want to post the data from the ruby on rails form that I created but I am unaware how.

My rails app runs on localhost:3000 and php on localhost:8080

I created a rails app without database by using -O and created the form for taking id, username and password input.

The php to database interaction is done and successful. Kindly help in the rails part that makes API call to php controller. Below is the code for reference.

UsersController code:

class UsersController < ApplicationController

    def index
    end

    def create
        user = User.new(token: user_params).credentials
    end

    private 
        def user_params
            params.require(:user).permit(:id, :username, :password)
        end
end

User model

class User

    def initialize(attributes={})
        @token ||= attributes[:token]
    end

    def credentials
        #not aware of the code needed here to communicate
           # or pass parameters to my php controller i.e. the API call
           #that needs to be made to my php file. 
    end
end

routes.rb file

get '/' => 'users#index'
post '/users' => 'users#create'
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
  • 1
    Think you need to look at [`net/http`](https://ruby-doc.org/stdlib-2.6.4/libdoc/net/http/rdoc/Net/HTTP.html). Basically you are taking about an API. Maybe read up on best practices for REST APIs. – ficuscr Aug 29 '19 at 16:10
  • Possible duplicate of [Submit POST data from controller to another website in Rails](https://stackoverflow.com/questions/1195962/submit-post-data-from-controller-to-another-website-in-rails) – ficuscr Aug 29 '19 at 16:12
  • For HTTP calls, consider Net::HTTP (core) or something a little more robust like [Faraday](https://github.com/lostisland/faraday). You can also step things up a bit and use [ActiveResource](https://github.com/rails/activeresource) to provide a more Rails-like experience. – tadman Aug 29 '19 at 17:07
  • The connection worked. However, I am getting a 500 response i.e. an internal server error # my_connection = Net::HTTP.new('localhost', 8080) post_data = @token puts "The post_data value is: #{post_data.inspect}" request = my_connection.request_post('/restapitrial/index.php/Users/insert/format/json', post_data.to_s.to_json) puts "The token value is: #{@token.inspect}" puts request.inspect puts request.body I think that my parameters should be sent in json format but how idk? – Harshal Jani Aug 30 '19 at 16:19

1 Answers1

0

I was able to successfully make an api call from rails to php file and store the variables in my database. Thank you @ficuscr & @tadman for the resources.

Here is my code:

Users Controller

class UsersController < ApplicationController
    def index
    end

    def create
        @users = User.new(token: user_params).credentials
        redirect_to '/dashboard'
    end

    private 
        def user_params
            params.require(:user).permit(:id, :username, :password).to_hash
        end
end

User Model

class User

    def initialize(attributes={})
        @token ||= attributes[:token]
    end

    def credentials
        my_connection = Net::HTTP.new('localhost', 8080)
        request = my_connection.post('/restapitrial/index.php/Users/insert/', @token.to_json, "Content-Type" => "application/json")
    end
end