私はrubyon railsを初めて使用し、ruby onrailsを使用して簡単なログインおよび登録アプリを作成しました。
フォームから取得したID、ユーザー名、パスワードを(Ruby on Railsを使用して)渡し、それをphp codeigniterコードに渡します。このコードは、データベースに挿入してフェッチし、JSON出力として表示します。
phpコードが投稿データをphpmyadminデータベースに挿入してフェッチする後半の部分を実行しました(postman POSTを使用してID、ユーザー名、パスワードの値を渡しました)
ただし、postmanは使いたくありません。作成したRubyon Railsフォームのデータを投稿したいのですが、どうすればよいかわかりません。
私のRailsアプリは実行されlocalhost:3000
、PHPはlocalhost:8080
私が使用してデータベースせずにアプリをレールに作成-O
して服用するためのフォームを作成しid
、username
そしてpassword
入力。
PHPとデータベースの相互作用が完了し、成功しました。phpコントローラーへのAPI呼び出しを行うrailsの部分を手伝ってください。以下は参照用のコードです。
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
ユーザーモデル
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
ルート.rbファイル
get '/' => 'users#index'
post '/users' => 'users#create'
RailsからphpファイルへのAPI呼び出しを正常に行い、変数をデータベースに保存することができました。リソースをありがとう@ ficuscr&@ tadman。
これが私のコードです:
ユーザーコントローラー
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
ユーザーモデル
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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加