问题描述
如何从JavaScript侦听器传递变量(id):
How can I pass a variable (id) from the JavaScript listener:
Gmaps.map.callback = function() {
...
...
google.maps.event.addListener(marker, 'click', function() {
var id = this.currentMarker;
alert(id);
});
}
}
到ruby中的实例变量(@foo) on-rails控制器
To the instance variable (@foo) in ruby-on-rails controller
def create
@foo = ???
...
end
在视图中正确创建关系( form):
to correctly create a relationship in the view (form):
<%= form_for user.relationships.build(:followed_id => @foo.id) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
谢谢!
推荐答案
如果我理解正确,一个好方法是使用AJAX将id提交给你的动作并使用动作来渲染你的表格。
If I am understanding correctly, a good way to do this is to use AJAX to submit the id to your action and use the action to render your form.
在你的javascript中看起来像这样:
That would look something like this in your javascript:
jQuery.ajax({
data: 'id=' + id,
dataType: 'script',
type: 'post',
url: "/controller/action"
});
您需要一条路线:
post 'controller/action/:id' => 'controller#action'
然后在您的操作中,您可以获取该ID并将表单呈现为此类似:
Then in your action you can grab that id and render your form something like this:
def action
@user = User.relationships.build(:followed_id => params[:id])
render :viewname, :layout => false
end
然后你可以在部分或者其他任何地方为@user构建一个表单
Then you can just build a form for @user in a partial or whatever
<%= form_for @user do |f| %>
你必须在那里填写详细信息,但这应该会让你非常接近。
You'll have to fill in the details there, but that should get you pretty close.
这篇关于将JavaScript变量传递给ruby-on-rails控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!