pass javascript variable to erb code for checking condition

28 views
Skip to first unread message

Sai Ch

unread,
Mar 10, 2015, 7:59:03 AM3/10/15
to rubyonra...@googlegroups.com
controller/course_controller.rb:
def edit
@course = Course.includes(holes:
:beacons).find(params[:id])
end

views/course/edit.html.erb


<%= form_for (@course), remote: true do |cb| %>

<%= cb.text_field :course_name, class: “name" %>

<div id="holes">
<% (cb.object.holes).each do |i| %>
<a href=“#" id="hole-<%= i.id %>" class="holeid"
value="<%= i.id %>"><%= i.id %> </a>
<div class="circular circle-green circle-left"></div>
<% end %>
</div>

<%= cb.fields_for :holes do |hb| %>
<% if (hb.object.id) == (cb.object.holes.first.id) %>

<h3> <%= "#{hb.object.id} - #{hb.object.name} " %></h3>

<%= hb.fields_for :beacons do |bb| %>
<div class=“tip"> <%= bb.label :CaddyTip %></div>
<div class=“ctip"> <%= bb.text_area :caddy_tip
%></div>

<div class=“shot"> <%= bb.label :BestShot %></div>
<div class=“bshot"> <%= bb.text_area :best_shot
%></div>

<div class=“be"> <%= bb.label :Be_Aware %></div>
<div class=“beaware"> <%= bb.text_area :be_aware
%></div>

<% end %>#beacons form close

<% end %> #if close

<% end %> # holes form close
<% end %>#course form close



<script>
var selected = [];
var clickedholeid = $(".holeid").on('click', function(e) {
e.preventDefault();
alert($(e.currentTarget).html());
})
</script>


I want to pass user clicked hole-id to check if condition to display it
beacons within the page.My app is about Golf course game.
In this a course have 18 holes and each hole have 3
beacons(tip,approach,beaware) respectively. I alert user clicked hole id
in console but now i want to pass that value to if condition after holes
form in above code. Without that condition its display all 18 holes
beacons in edit page.(18 * 3)
So I need to stop that and default it display douse first hole beacons
after that if user perform click

Attachments:
http://www.ruby-forum.com/attachment/10575/0x_alt__1_.jpg


--
Posted via http://www.ruby-forum.com/.

Hassan Schroeder

unread,
Mar 10, 2015, 8:15:19 AM3/10/15
to rubyonrails-talk
On Tue, Mar 10, 2015 at 4:57 AM, Sai Ch <li...@ruby-forum.com> wrote:

> I want to pass user clicked hole-id to check ...

If you want to evaluate a *client-side* variable value using Ruby on
the server you'll have to send it via AJAX. Otherwise include logic
in your JavaScript so the client can handle the checking.

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Sai Ch

unread,
Mar 10, 2015, 10:09:08 AM3/10/15
to rubyonra...@googlegroups.com
function holeid(){
$.ajax({
type: "POST",
url: "course/edit/",
data: "Hole data="+ id,
success: function(id){
alert("Hole ID:"+ id );
}
});

James Davis, PhD

unread,
Mar 11, 2015, 8:04:36 AM3/11/15
to rubyonra...@googlegroups.com

Take a look at this gem: https://rubygems.org/gems/gon

Ryan Bates also did a Railscast about it.

Robby O'Connor

unread,
Mar 11, 2015, 8:06:39 AM3/11/15
to rubyonra...@googlegroups.com

I would advise you to pay the $9...he's not charging recurring thereafter...it's worth it.. some of the info has not changed

--Rob
Sent from my cell, please excuse any typos.

On Mar 11, 2015 8:05 AM, "James Davis, PhD" <james.d...@gmail.com> wrote:

Take a look at this gem: https://rubygems.org/gems/gon

Ryan Bates also did a Railscast about it.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/64f3e8a9-0ca8-4634-933e-504b874dcf11%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages