我完全不熟悉rails并为我的公司编写应用程序,这将有助于更好地跟踪我公司的每个成员参加的MeetUps。
我已经通过rails generate scaffold User...
创建了一个简单的CRUD应用程序,并且还创建了一个客户端来连接到MeetUp API(http://www.meetup.com/meetup_api/)。当有人在我的应用程序中创建一个只有他们的名字/姓氏和MeetUp memberId的新用户时,我想点击MeetUp API并确保他们输入的memberId既有效又要他们想要输入的成员ID。 从API中撤回此信息后,我想显示一个包含帐户信息的确认框,以便他们验证,点击“确定”或“取消”,然后创建新用户或告诉他们尝试另一个memberId。这可能吗?下面的相关代码(对格式化道歉):
创建用户(users_controller.rb)
def create
@user = User.new(user_params)
respond_to do |format|
if @user.hasMeetUpAccount # Render confirmation here
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
else
format.html { render :new, notice: 'No MeetUp account found for #{@user.meetupid}' }
end
end
end
user.rb
class User < ActiveRecord::Base
require 'HTTPClient'
def hasMeetUpAccount
return HTTPClient.hasMeetUpAccount(["member_id", self.meetupid])
end
end
HTTPClient类
require 'httparty'
class HTTPClient
include HTTParty
def self.GetData(method, method_args)
HTTParty.get("#{BASE_URI}/#{method}?#{method_args}")
end
def self.hasMeetUpAccount(*args)
local_args = SetUpArgs(args)
data = GetData("members", local_args)
return data
end
谢谢!
答案 0 :(得分:2)
据此我明白,如果用户存在,您需要显示确认框,否则执行其他操作。 你可以试试这个。
##create action
respond_to do |format|
if @user.hasMeetUpAccount
format.html
format.js
else
format.html { render :new }
end
end
##create.html.erb or any partial inside create.html.erb(_dialog.html.erb)
<!-- your view logic -->
//get the account info from the controller and save it in this hidden field and use it in the js below to show name/any other info that you need
<input id="account_name" type="hidden" value="<%= @account.name %>">
<script type='text/javascript'>
//show the dialog on page load
$(function () {
//method to show confirm box
function show_confirm_box(){
var account_name=$("#account_name").val();
if(confirm("Is"+account_name+" your account ?")){
##run ajax or any other thing that you want to do
}else{
return false;
}
}//method ends
//call above function when page is loaded and when you have the account name after 3 seconds,you can also use lenght() to verify if account_name hidden element is not empty
timeout = setTimeout('show_confirm_box()', 3000);
//clear timeout
clearTimeout(timeout);
});
</script>