I amreading about the backbone.js and make a demo application for the CRUD operation from by goggling some links.
and see the structure of the backbone.js which initial created by installing the gem 'backbone-rails',
But here my query is like when we have normal application like without using of any .js, we have some options to check the flow of the application, like for example we can write 'exit' keyword in controller and check the methods which is calling by routes and can inspect and see all the records and parameters which fetched by model and parameters which input by user .
In using of the backbone.js, i am confusing that from where the view is coming and is there any way to see it's line by line flow means
first go to model and for query data can here we can show it in console and see what is going on.
For debug the backbone applications place 'debugger' in your js code and the js execution will stop there if you use chrome. In other browser I am not sure if works but in chrome I used this technique many times... And you can step line by line you can check variables and so on.
For debugging rails use these gems
gem 'pry'
gem 'pry-nav'
Then in your controller where you want to inspect the variables and the calling stack put binding.pry
Like here:
class UsersController <...
def index
binding.pry
#users = Users.all
end
end
Related
I created an engine which provides an ui component as a cell. The corresponding gem (criteria_operator-ui_component) contains nearly no code inside the lib folder, because for cells to function properly I had to work inside the assets path. The base file of the gem looks like this:
require 'criteria_operator/ui_component/engine'
require 'cells/rails'
module CriteriaOperator
module UiComponent
# Your code goes here...
end
end
The engine doesn't contain much, either:
module CriteriaOperator
module UiComponent
class Engine < ::Rails::Engine
require 'jquery-rails'
require 'criteria_operator'
isolate_namespace CriteriaOperator::UiComponent
end
end
end
To me, it looks like the gem couldn't even know about the cell, but as far as I know I'm not allowed to include anything from outside the lib folder. Also, testing the cell in the dummy application within the project is working fine.
Now I'm using this engine inside a real Rails application. In the gemfile, I included the following:
gem 'criteria_operator'
gem 'cells' # i added these three, because `bundler list` didn't show me
gem 'cells-rails' # `cells-rails` and `cells-erb` even though they are listed
gem 'cells-erb' # as dependencies for the engine
gem 'criteria_operator-ui_component'
I mounted the routes
mount CriteriaOperator::UiComponent::Engine => '/criteria_operator-ui_component'
and tried using the cell CriteriaOperator::UiComponent::CriteriaEditor like I did in the dummy application. Inside erb:
cell('criteria_operator/ui_component/criteria_editor', #op)
or from code:
include Cell::RailsExtensions::ActionController
def whatever
cell(CriteriaOperator::UiComponent::CriteriaEditor, #op).call()
end
The error is ActionView::Template::Error (uninitialized constant CriteriaOperator::UiComponent::CriteriaEditor).
What am I doing wrong? Am I just missing something when using the engine, or is the engine itself implemented the wrong way? And if that's the case, why does the dummy application work? I'm totally stuck, this is my first time creating a Rails Engine as well as my fist time working with cells...
The full code of the engine (including the dummy application) can be found on GitHub (this isn't supposed to be any advertisement, it's just in case anyone needs additional information).
You're calling CriteriaOperator::UiComponent::CriteriaEditor but that class/module does not seem to exist.
CriteriaOperator::UiComponent::Engine works OK because it's defined in the engine itself.
I'm guessing that your sample application works because it's using the view-based invocation like cell('criteria_operator/ui_component/criteria_editor') which presumably works with the javascript? You can't use the "code" version without defining the cell as a class like this:
https://github.com/trailblazer/cells#cell-class
If I'm testing my Javascript code, I often use console.log to write messages to the browser console. I find it a convenient place to check for these messages.
Is it possible to output messages to the browser console from Ruby files within your Rails project? (E.g. from a method in the model)
You can use console.log() in your views:
#view.html.erb
<script>
console.log("Message");
</script>
If you want to log in your Model, try:
Rails.logger.debug "message!!"
Simple. Just call puts 'your debug message' and it will be printed to where the server is logging. For instance, if you are running the rails server only by running rails s on a terminal, the output of puts will be on this same terminal. If you need more 'power' to debug, you should consider using IDE's like RubyMine to debug your code. Thus, you can place breakpoints and see all the application state.
Not really, as cstrutton mentioned printing to the browser log is not possible. But this is something I tend to do which may help others in the same situation.
Say for example, I only want to display my debug params on my page if my :commit == "Search". I would do something like this:
In my helper file.
def view_logging
if params[:commit] == "Search"
debug params
else
# add whatever you want here if not true
end
end
Now inside my view page, I would just simply add the helper method defined.
<%= view_logging %>
I'm new to Ruby on Rails and I'm looking at an application that has a variable called current_teacher. I cannot seem to find where this is set. Everywhere I look the code seems to read from it but where is it set. Is this one of those things that Rails does for you. There is a mode and a table called teachers, so I'm sure this has something to do with it.
I'm very confused by statements like the following, can someone tell me how Rails does this?
if current_teacher.can_request_fieldtrip
Suppose you have a controller like :
class ClientsController < ApplicationController
def new
if current_teacher.can_request_fieldtrip
# code
end
end
end
Here is debugging tips :
(a) put this in your Gemfile and do bundle install :
`gem 'pry-rails', :group => :development`
(b) Put the line binding.pry just before the if statement.
(c) Start rails server using rails s.
(d) Hit the browser like http://localhost:3000/new
(e) Now you will be in the Pry console. Just do in the console,
method(:current_teacher).source_location
And the above line tell you where the method has been defined.
Documentation of Method#source_location
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native)
Rails does not support authentication by itself, however there are a lot of 'add-ons' that rails can use. These 'add-ons' are called gems. This can be a little confusing because you can't actually see their code inside your project folder.
If you open a file called "Gemfile" (it should be in your project folder) you can see a list of gems that you use. Try searching their names on google, you will probably find official web page that contains it's documentation. That way can learn what they do and how to use them.
current_teacher method smells like "Devise" gem
https://github.com/plataformatec/devise
I'm not sure about can_request_fieldtrip, this could be a custom method defined in Teacher model.
I'm building a API and want the show page for a user to be found by 'uid' instead of the record ID
I have this in my controller
def show
respond_with User.find_by_uid(params[:uid])
end
When I go to localhost/api/v1/users/8888888 Its returns "Null"
Finding by ID seems to work fine, am I doing something wrong here?
I tried put this in the rails console and it worked
User.find_by_uid("8888888")
I'm new to rails
Thanks
have you tried visiting:
localhost/api/v1/users?uid= 8888888 instead of the url you are using currently, except you are handling that correctly rails would have no knowledge of the uid param alternatively you could add this to your config/routes.rb file
get 'users/:uid', to: 'users#show'
With the hope that your controller is called UsersController then you can call localhost/api/v1/users/8888888 in your browser and it should behave as expected
Rather than just giving you the answer, I'll provide a tip on debugging Ruby applications (including Rails).
Get the pry gem and the pry-debugger gem and include them in your Rails application (there's plenty of posts around Google on how to include pry and pry-debugger in Rails).
put 'binding.pry' (without the quotes) at the beginning of your show method. In the console where your server runs, when show gets executed, execution will halt/pause at binding.pry. Type the following in the pry console to see what is available in the rails params hash.
pry> params
(this will print out the contents of params)
I would start my troubleshooting here, and post the contents of params and any relevant server logging here if you still can't figure it out.
edit
I don't have enough rep to comment, yet. Only really been on this site and using it a day or two.
I have never worked with web services and rails, and obviously this is something I need to learn.
I have chosen to use hpricot because it looks great.
Anyway, _why's been nice enough to provide the following example on the hpricot website:
#!ruby
require 'hpricot'
require 'open-uri'
# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
# change the CSS class on links
(doc/"span.entryPermalink").set("class", "newLinks")
# remove the sidebar
(doc/"#sidebar").remove
# print the altered HTML
puts doc
Which looks simple, elegant, and easy peasey.
Works great in Ruby, but my question is: How do I break this up in rails?
I experimented with adding this all to a single controller, but couldn't think of the best way to call it in a view.
So if you were parsing an XML file from a web API and printing it in nice clean HTML with Hpricot, how would you break up the activity over the models, views, and controllers, and what would you put where?
Model, model, model, model, model. Skinny controllers, simple views.
The RedHandedHomePage model does the parsing on initialization, then call 'def render' in the controller, set output to an instance variable, and print that in a view.
I'd probably go for a REST approach and have resources that represent the different entities within the XML file being consumed. Do you have a specific example of the XML that you can give?