[英]Render Dynamic Variables in JSON Feed with Ruby on Rails
I'm just looking at creating an API for our customers to interact with our main Rails 3 application. 我只是在为我们的客户创建一个API,以便与我们的主要Rails 3应用程序进行交互。
For one part, I wanted the json response to contain some a form. 一方面,我希望json响应包含某种形式。 Basically some form code that we control and appears on their site. 基本上是一些我们控制并在其网站上显示的表单代码。
I have this working ok (simplified): 我可以正常工作(简体):
def login_form
@response = Form.find_by_something(params[:something_else])
if @response
render :status=>200, :json=>{:response => @response}
end
end
And on the client side, I can fetch: 在客户端,我可以获取:
@logins = HTTParty.get("#{set_auth_url}/api/v1/logins.json?auth_token=xyz&something_else=abc").parsed_response
And display the form 并显示表格
= @logins["response"].html_safe
The problem is, the form code has some dynamic variables: 问题是,表单代码具有一些动态变量:
For example: 例如:
<div class="login">
<FORM name="form1" METHOD="get" action="<%= request.path %>?">
<INPUT TYPE="HIDDEN" NAME="chal" VALUE="<%= params['challenge'] %>">
<% if @current_location['success_url'] %>
<INPUT TYPE="HIDDEN" NAME="userurl" VALUE="/success">
<% end %>
<input type="hidden" name="UserName" placeholder="Username" value="<%= @user['username']['username'] %>">
<input type="submit" name="login" value="Login" class="btn">
</div>
I'm wondering if this is a good idea? 我想知道这是否是个好主意? And if so, how can I break those variables out in the html?? 如果是这样,我该如何在html中分解这些变量?
If it's not, could someone recommend a nicer way to go about it. 如果不是这样,有人可以推荐一种更好的方法来解决它。
Are those variables known on server-side when preparing the response? 准备响应时,这些变量在服务器端是否已知? In that case, I'd try around with running them through templating engine :json=> { :response => ERB.new(@response).result(binding) }
- client would receive only the html applicable in his case. 在那种情况下,我将尝试通过模板引擎来运行它们:json=> { :response => ERB.new(@response).result(binding) }
-客户端只会收到适用于他的情况的html。
Why not use Javascript Template for this as in Jquery Template or Mustache or Handlebar 为什么不像在Jquery模板,Mustache或Handlebar中那样使用Javascript模板
so keep the form in the on the client side like this 所以像这样将表单保存在客户端
I will describe it using Mustache over here .... 我将在这里使用小胡子来描述它。
...
<script id="template" type="text/template">
<div class="login">
<FORM name="form1" METHOD="get" action={{request_path}}>
<INPUT TYPE="HIDDEN" NAME="chal" VALUE={{challenges}}>
{{boolean_value}}
<INPUT TYPE="HIDDEN" NAME="userurl" VALUE="/success">
{{/boolean_value}}
<input type="hidden" name="UserName" placeholder="Username" value={{user_name}}>
<input type="submit" name="login" value="Login" class="btn">
</div>
</script>
</html>
Next part is getting the response in form of ajax 下一部分以ajax形式获取响应
The using any Javascript Framework I guess it can be achieved I'm using jQuery over here 我想使用任何Javascript框架都可以实现,我在这里使用jQuery
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
// make ajax call over here
success: function(data) {
// JSON DATA
var template = $('script#template').html();
$([container where html is place]).html(Mustache.to_html(template,data));
}
})
})
Hope this Help 希望这个帮助
Not sure how would you want though of plain Form html tag intead of form_tag since it does not contain an authenticity token 不知道要如何获取form_tag的纯HTML标记标签,因为它不包含真实性令牌
Hope this help 希望这个帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.