admin管理员组文章数量:1330641
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);
});
}
}
To the instance variable (@foo) in ruby-on-rails controller
def create
@foo = ???
...
end
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 %>
Thanks!
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);
});
}
}
To the instance variable (@foo) in ruby-on-rails controller
def create
@foo = ???
...
end
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 %>
Thanks!
Share asked Apr 9, 2012 at 22:00 Ilya CherevkovIlya Cherevkov 1,8032 gold badges18 silver badges51 bronze badges 2-
2
you should simply do an ajax call GET or POST instead of the
alert
– apneadiving Commented Apr 9, 2012 at 22:12 - thanks, it seems to resolve my problem! – Ilya Cherevkov Commented Apr 11, 2012 at 5:08
1 Answer
Reset to default 6If 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.
That would look something like this in your javascript:
jQuery.ajax({
data: 'id=' + id,
dataType: 'script',
type: 'post',
url: "/controller/action"
});
You'll need a route:
post 'controller/action/:id' => 'controller#action'
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
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.
本文标签: Passing JavaScript variable to rubyonrails controllerStack Overflow
版权声明:本文标题:Passing JavaScript variable to ruby-on-rails controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742221322a2435459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论