Getting an empty space when trying to use gmaps4rails - ruby-on-rails

So I followed the tutorial and screencast but can't seem to get this to work. I can see the div id's called map container and gmaps4rails_map where presumably, the map is supposed to be but nothing else.
I put in the yield :head and yield :script in the header and footer of the application layout view and #charger = Charger.all.to_gmaps4rails (Charger is my model) in the chargers_helper.html.erb file (I want to use in User show view).
I also put <%= gmaps4rails(#charger) %> in the show.html.erb view.
How do I get this to work?
-Update-
I just got it to work. To fix the problem I put in the #charger = ... code in a method in the helper and called the method instead of #charger.
Now the map shows up but I still cannot see the insert new marker code like in the screencast - I don't need it for my app but am curious as to what happened to it.

In the screencast, I create model + controller with a scaffold generator. That's why I have ready to use links.
Besides, I create buttons during the screencast. Nothing is made behind the scene: the whole app is created from scratch :)

Related

How to find where a view partial is used?

How do you go about easily finding out where a Rails view partial is used?
In what views, controllers etc.
This is handy when working on an app that someone else wrote. You don't necessarily know what views are using a particular partial, or where to find where the partial is used when navigating the app in the browser.
Currently, I am using the Sublime Text editor to project-wide search for the partial name "form" or "_form", or for "render ", but this gives an unnecessary amount of useless results.
You could try putting this caller inside the partial, and then running your test suite:
#haml
- p caller[x]
#erb
<%- p caller[x] %>
I used x because you'll have to play around with which index you're calling to get useful information.
There's no built-in solution, but I wrote a gem to try to solve this exact problem: https://github.com/Negotiatus/Partial-Finder
It's a rake task that will recurse through your project and attempt to create the full render chain (and routes!) for a given partial. Hope it helps!

pjax -- must links be inside the pjax-container?

I am using https://github.com/rails/pjax_rails.
I want to have my links inside a "permanent" portion of the page. I.e. in my layout I have
<%= link_to "Some Action", some_action_path %>
Then inside the view:
<div data-pjax-container>Content to be replaced</div>
Here is my javascript where I invoke pjax:
('[data-pjax-container]').pjax('a');
[You may note that this is different than the invocation method in the readme, but as a reported issue points out, the method in the readme doesn't work at all.]
This is not working (the link reloads the entire page).
If I move the link inside the div with the data-pjax-container attribute, it works (the page is not reloaded and only the container is updated).
I have not seen any examples where the link was actually outside of the container. Can anyone tell me how to get this to work?
I was probably focusing too much on the pjax-rails readme (not great). I went to the source (https://github.com/defunkt/jquery-pjax) which led me to changing my js to this:
$(document).pjax('a', '[data-pjax-container]')
..which got me back on the right track.

How do I send data from a controler to a view in Ruby on Rails

I have a simple controller that was created through a scaffold and has a "Show" function.
In the view that was created through the scaffold, I have an image that appears only on a certain condition.
I want to set or evaluate the condition in the controller and send it to the view.
I am not sure how to do that or whether this is the correct approach.
The condition should generally be dealt with in the view file using erb, or haml. If you update your question with the condition, then I'll see about updating my answer to reflect it. For now, I'll use a common condition.
Say you only want to show an image if an object is featured. Let's imagine there is a featured field in your object that acts as a flag (1,0).
If this object is say an Article, we can then check the condition in the view file. The controller would obtain the article from the model:
-# articles_controller show action
#article = Article.find(params[:id])
..
-# views/articles/show.html.erb
<% if #article.featured? %>
// show image here
<% end %>
Remember this is an example condition that is not necessarily correct. It is just to illustrate my initial approach.
I wouldn't suggest you use javascript to hide/show depending on this condition, because you are then putting your logic in javascript, when it can be easily managed from within your view files.
If the condition is complex, you would then move it to the model, and perform something like:
if #article.some_complex_condition?
..rather than having that complex condition in your controller file. This allows you to reuse the condition away from the specific controller and makes it more testable.
If you just want to show and hide an image based on a certain condition, than you can do that with JQuery. You shouldn't put anything in the controller that is view-centric.
You can also get the id of whatever data element is in 'show' and pass it to the JavaScript.
JQuery has show() and hide() methods that would work for you. Here's the documentation on the hide method: http://api.jquery.com/hide/
Basically, if you had a certain id for your image, you'd do something like this:
$(document).ready(function() {
$("#myImage").hide();
if (some_condition === true) {
$("#myImage").show();
}
});
You can put that code in your application.js file.
I whipped up a simple JsFiddle demonstrating a way to show and hide with buttons:
http://jsfiddle.net/phillipkregg/92RDS/
Of course, the code may be different depending on what you are trying to do.
If you need to get the 'id' of the object in the 'show' view, than you can put a script tag at the bottom of your show view like this:
<script type="text/javascript">
var my_show_object = <%= #some_object.id %> //this will be the specific id of whatever the object is that you passed from the controller
alert(my_show_object); //this helps with debugging to see if you have the right id
</script>
If you need more than the id, and you want to get the entire Rails object and represent it as Javascript - just do this at the bottom of your rails 'show' view:
<script type="text/javascript">
var my_object = <%= #your_rails_object.to_json %>;
console.log(my_object); //This code allows you to look in the console and see exactly what your object looks like.
</script>
Just like the last one, #your_rails_object is the variable that you have in your show view in the controller. This will return it as a json object so that you can get the id or whatever properties it has.
Also, if you are just learning Rails, I would recommend this online book: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
That's how I learned Rails - the book is excellent and free.

How do you handle widgets in rails?

StackOverflow, for example, has a user's reputation displayed up top. Clearly, this is grabbed from the database, and it's displayed on every page. Of course, this isn't in every controller action on every page, because that would be incredibly redundant.
How do you handle this kind of situation in rails? The only way I can think of it is to use before_filters to pass the models into the page, but that just seems like abuse of that feature. There seems to be the cells gem that does what I want, but I'd imagine this is a common problem and there must be a simple solution for it in rails without having to resort to plugins or gems.
What you are looking for is the layout. In rails this is where you define headers, footers, and sidebars that frame your site. Look for app/views/layouts/application.html.erb in your generated rails code. Towards the bottom you will see:
<body>
<%= yield %>
</body>
The yield is where rest of the app gets invoked. Everything before and after the yield will appear on every page. So, using your example, you might query the database and set the instance variable #reputation in the application controller:
#reputation = User.find( current_user ).reputation
then display it in the layout like this:
<body>
<%= #reputation %>
<%= yield %>
</body>
This is covered thoroughly in the book "Agile Web Development With Rails". If you are going to develop in Rails I recommend getting the latest edition.
I would just make a partial with the widget in it and render it in the layout(s) where you want it to appear. Let it do whatever it needs to do, eg connect to the db, run some js to connect to an external site, etc.
If you're concerned about optimisation then deal with it when it becomes a problem.
I guess, you can put the code you need into a view helper. And then render some partial, like it was said before, in the layouts where you want it to appear, calling helper's function.
Look here:
Rails view helpers in helper file

Still having a hard time with RoR MVC approach

I suppose it should do justice to state what I think I know so far as well as what I've done:
1) I created the app and did my first db migration; I now have my dev, test and production databases. The dev db has a table called 'wines'.
2) I made a scaffold which created the necessary files.
3) The basic index/update/destroy methods are set up and I can browse the pages.
4) From what I gather, the ActiveRecord class "Wine" automatically inherits properties from the database? Each column is a property and each row in the table 'wines' is a potentially instantiated object which is called from the wine_controller script.
The problem I'm having now is that I want to create a common layout that all controllers use. The only things that will change will be the page title, potentially some <link> tags in the header, the <body> attributes (javascript onload events most likely) and whatever lies inside the <body> tag.
I find myself looking up functions that will do what I want (like "favicon_link_tag", "stylesheet_link_tag" and "auto_discovery_link_tag"...) but I can't find the right place to PUT them! I know this has something to do with my lack of understanding of how things are executed/inherited. For example if I were to declare #pageTitle in application_controller.rb and use #pageTitle in ApplicationHelper it won't work. Or even using "stylesheet_link_tag" in application_controller.rb throws an error. I'm just not getting something.
How does each thing relate to another in terms of chronological execution, scope, etc.?
In your "app/views" directory there is a folder called "layouts." By default there should be an "application.html.erb" file in there, but if there isn't you can create it.
Your "application" layout file is the default layout file used by any view. However, if you want a particular controller to use a different view, you can override this. See this railscast, and this one is helpful too.
The main thing to understand is the content from any particular view will show up wherever the yield method appears in your application layout. The main 'yield' block gets the view file specified by your controller action, but you can mark anything inside any view to be passed to another yield block instead. For instance, the "title" example you gave could be passed to the head of your application layout. See this railscast for a detailed example of that.
For more, you should read the Rails Guide, and you might want to consider picking up a Rails starter book.
I got my feet wet with "Beginning Rails 3," which was a phenomenal introduction to the framework. A couple days with that book and it was all making sense to me, and I was developing faster than I ever had before. Rails rocks once you get to know it, but it's definitely worth going through a book.
Please continue to ask questions, I'll help if I can :)
-EDIT- To answer your question about control flow, it basically works like this:
Your browser sends a GET request for a particular URL.
The router takes that request, matches it to a controller action, triggers that controller action, and provides the controller any parameters associated with the request. For instance: if you requested example.com/posts/123?color=red this would trigger the SHOW action of your posts_controller, and would pass {:color => 'red'} to the params hash. You would access that using params[:color]
The controller action does its thing, and when it's done it renders output. By default it renders whatever view is located in app/<controller_name>/<action_name>, and will whichever file matches the extension appropriate to the request (ie an AJAX request would trigger <action_name>.js.erb and a GET request would trigger <action_name>.html.erb.
You can override this using the render method, for example by passing render 'foo/bar' to render using the view for FooController, Bar action instead of your current action.
Note that no matter what you render, the data available to the view is whatever is in the specific controller action the router triggered, not the controller action that would 'normally' render that view.
The view file is parsed using the data from the controller that called it. If you have any content_for methods then the view code that is inside the content_for block will go where you tell it, otherwise everything else will go to the main YIELD block in your application layout (or whatever layout your controller specified instead).
The application layout is parsed, and the content from the view is inserted into the appropriate areas.
The page is served to the user.
That's a simplification in some ways, but I think it answers your question. Again, feel free to keep asking :)

Resources