简体   繁体   中英

How do I pass parameters (eg. id, username, password) from my ruby on rails model to my php codeigniter controller

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM