[英]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. 我是Rails的新手,我使用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. 我想传递我从表单中获取的ID,用户名和密码(在Rails上使用ruby),并将其传递给我的php codeigniter代码,该代码将其插入数据库并取回并显示为JSON输出。
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) 我已经完成了后一部分,其中我的php代码将发布数据插入到phpmyadmin数据库中并进行了获取(用于邮递员POST来传递ID,用户名和密码的值)
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.
我想发布我创建的Rails表单中的ruby数据,但是我不知道如何。
My rails app runs on localhost:3000
and php on localhost:8080
我的Rails应用程序在
localhost:3000
上运行,而php在localhost:8080
I created a rails app without database by using -O
and created the form for taking id
, username
and password
input. 我使用
-O
创建了一个不带数据库的Rails应用程序,并创建了接受id
, username
和password
输入的表单。
The php to database interaction is done and successful. PHP与数据库的交互已完成且成功。 Kindly help in the rails part that makes API call to php controller.
请在使API调用php控制器的rails部分中提供帮助。 Below is the code for reference.
以下是供参考的代码。
UsersController code: UsersController代码:
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 routes.rb文件
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. 我能够成功地从Rails到php文件进行api调用,并将变量存储在数据库中。 Thank you @ficuscr & @tadman for the resources.
感谢@ficuscr&@tadman提供的资源。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.