Mohamed Yakout
Mohamed Yakout

Reputation: 3036

JavaScript code on rails console

I use binding.pry in somewhere in Cabypara code for debugging, and i want to check the value of html element using jQuery.

I can't use debugger, then check the value from browser, because this code is Cabybara code for testing as example:

When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
  select(value, :from => field)
  binding.pry
end

How can i check the value of this field by jQuery code as $("##{field}").val() on rails console ?

Upvotes: 0

Views: 609

Answers (2)

SMAG
SMAG

Reputation: 798

You can call binding.pry while Rails is rendering your view, even in the javascript portion of the view.

This is not going to give you access to the the client side code. Like @apneadiving and @mohamed-yakout stated you can't access the client from the server, but it can give the the access to all of the server side information that is available at that moment in the rendering process.

erb:

<script>
  // some javascript...

  "<% binding.pry %>" 
</script>

// Note: You can not do this from `.js` files that are assets of the view without 
// adding the `.erb` extension to the javascript files.

This may be helpful in checking values being utilized by JQuery or Javascript and verifying that they are being built correctly at this step in the process. Ex: verifying the collection being used to generate the rows of a table

In your case you could verify the value of field, but not the value of the element found by the id being passed by the field variable.

 $("##{field}").val()

This can be helpful when the result of "##{field}" is giving an #unexpected result instead of an #expected one, since you can't access the server side code from the client to determine the rendering problem.

Note: This translates to Slim as well

javascript:
  // some javascript...

  "#{binding.pry}"

Upvotes: 0

Mohamed Yakout
Mohamed Yakout

Reputation: 3036

This answer depends on @apneadiving's comment:

Rails console used for server side only, not for client side, these helper links:

  1. Ruby-on-Rails is server side.
  2. Rails console is useful for testing out quick ideas with code and changing data server-side without touching the website.

Upvotes: 1

Related Questions