Sorry I am a bit of a Rails newbie but have been trying to get gmaps4rails to work with twitter bootstrap and have hit a bit of a dead end. Nothing seems to resolve it or point in the right direction after hours of Googling.I have gone through all the steps before with an un-bootstrapped rails app and it worked, but for some reason the map wont display in the TBS version.
Just to note I used the rails3-bootstrap-devise-cancan template in case there are any known issues with that.
I have included this stylesheet link at the top of my view:
<head>
<% content_for :head do %>
<%= stylesheet_link_tag :gmaps4rails -%>
<% end %>
</head>
…..
<%= gmaps4rails(#json) %>
In the head of the application.html..erb
<%= yield :head %>
In the controllers index action:
#json = Outlet.all.to_gmaps4rails
and in the model:
acts_as_gmappable :position => :location
def gmaps4rails_address
address
end
gmaps4rails.css is in the stylesheets folder so should be accessible.
Gemfile
gem 'gmaps4rails'
gem 'twitter-bootstrap-rails'
I also added this gem
gem 'thin'
Because i was getting another error about the content-length but I'm pretty sure it was unrelated to the gmaps problem (maps don't show up before or after installing this gem)
Also, when I load the app in firefox I can see from the activity in the bottom left of the screen that it appears to be connecting successfully to the Google Maps API and no errors are displayed in the terminal.
This is the terminal output.
Started GET "/outlets" for 127.0.0.1 at 2013-01-17 21:26:24 +0000
Processing by OutletsController#index as HTML
Outlet Load (0.2ms) SELECT "outlets".* FROM "outlets"
CACHE (0.0ms) SELECT "outlets".* FROM "outlets"
Rendered /Users/mikemccann/.rvm/gems/ruby-1.9.3-p362#myapp/gems/gmaps4rails-1.5.6/app/views/gmaps4rails/_gmaps4rails.html.erb (1.1ms)
Rendered outlets/index.html.erb within layouts/application (4.2ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.1ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
Rendered layouts/_navigation.html.erb (188.2ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 212ms (Views: 208.7ms | ActiveRecord: 1.6ms)
Really stumped by this one so any help is greatly appreciated!!!
Mike
Are you explicitly giving your map container div a height? What happens when you look at the HTML in the developer console? Is anything Gmaps related showing in the map container div?
Related
I was trying to add fragment caching to speed up website performance.
Now, I tested it in development mode, so I change this
#environments/developments.rb
config.action_controller.perform_caching = true
And in erb
<% #projects.each do |project| %>
<% cache project do %>
<%= link_to "#{project.name}", category_project_path(#category, project) %>
<br>
<% end %>
<% end %>
However, it seems not work. Every time I refresh the page, it showed query again in terminal.
I thought it would only query once in first time, or I've misunderstood the concept of cache?
Processing by CategoriesController#show as HTML
Parameters: {"id"=>"3306"}
Category Load (0.4ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT 1 [["id", 3306]]
Project Load (0.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."category_id" IN (3306)
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "projects"."id") FROM "projects" WHERE "projects"."category_id" = $1 [["category_id", 3306]]
Project Load (1.0ms) SELECT DISTINCT "projects".* FROM "projects" WHERE "projects"."category_id" = $1 LIMIT 10 OFFSET 0 [["category_id", 3306]]
Cache digest for app/views/categories/show.html.erb: 9b54e2d9c7ce230e3f6a333f00d549da
Read fragment views/projects/3670-20160715055333331671000/9b54e2d9c7ce230e3f6a333f00d549da (0.2ms)
Cache digest for app/views/categories/show.html.erb: 9b54e2d9c7ce230e3f6a333f00d549da
Read fragment views/projects/3677-20160715055334439274000/9b54e2d9c7ce230e3f6a333f00d549da (0.2ms)
Cache digest for app/views/categories/show.html.erb: 9b54e2d9c7ce230e3f6a333f00d549da
Read fragment views/projects/3678-20160715055334446172000/9b54e2d9c7ce230e3f6a333f00d549da (0.2ms)
Cache digest for app/views/categories/show.html.erb: 9b54e2d9c7ce230e3f6a333f00d549da
Read fragment views/projects/3689-20160715055334536421000/9b54e2d9c7ce230e3f6a333f00d549da (0.1ms)
Rendered categories/show.html.erb within layouts/application (8.7ms)
Completed 200 OK in 31ms (Views: 26.0ms | ActiveRecord: 2.3ms)
According to your logs it looks like data is product is populated from the cache and no query is fired for product.
views/projects/3670-20160715055333331671000/9b54e2d9c7ce230e3f6a333f00d549da
The number in the middle is the product_id followed by the timestamp value in the updated_at attribute of the product record. Rails uses the timestamp value to make sure it is not serving stale data. If the value of updated_at has changed, a new key will be generated(fires query to database). Then Rails will write a new cache to that key, and the old cache written to the old key will never be used again.
Hope this help!
I want to build a shopping cart for my website. I've installed redis/hiredis but when I start the server, if I go on a page where the shopping cart whould show a value (current items in cart), or if I want to go on the shopping cart page, the server crashes giving the following information:
Started GET "/cart" for 127.0.0.1 at 2015-05-19 13:43:33 +0300
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by CartsController#show as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
/home/svuser/.rvm/rubies/ruby-2.2.0/bin/ruby: symbol lookup error: /home/svuser/.rvm/gems/ruby-2.2.0/gems/hiredis-0.4.5/lib/hiredis/ext/hiredis_ext.so: undefined symbol: rb_thread_select
In my index page, I create a link to the shopping cart using the following instructions :
<%if signed_in?%>
<li>
<%= link_to cart_path do%>
<i class="fi-shopping-cart"></i>
My Cart
<%end%>
Is there any way to work around this issue ? I am still new to rails and I couldn't figure it out on my own.
I faced a similar problem. In my case, I use Ruby 2.2.2p95 and old version gem hiredis - '0.4.5'. It uses method rb_thread_select, which was removed in Ruby 2.2
So bundle update hiredis helps me.
So I am using mini-profiler and it gives me some nice stats.
However, one thing I have noticed is that I have gotten a lot of the SQL calls down to a minimum and now the biggest thing is the rendering of the various partials and HTML.
For instance, here are 2 different examples of issues I am facing:
Mini-Profiler
GET http://localhost:3000/ 14.0 +0.0
Executing action: index 9.5 +9.0
Rendering: home/index 7.8 +16.0
Rendering: home/_row 7.9 +22.0
Rendering: layouts/application 1038.7 +32.0
Rendering: layouts/_navigation 6.0 +586.0
Development.log
Started GET "/" for 127.0.0.1 at 2013-05-17 18:00:26 -0500
Processing by HomeController#index as HTML
Item Load (0.6ms) SELECT "items".* FROM "items" WHERE "items"."is_approved" = 't'
ActsAsTaggableOn::Tag Load (0.6ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 13 AND "taggings"."taggable_type" = 'Item' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
Rendered home/_row.html.erb (8.0ms)
Rendered home/index.html.erb within layouts/application (15.7ms)
Rendered layouts/_social_media.html.erb (0.5ms)
(0.4ms) SELECT items.id FROM "items"
ActsAsTaggableOn::Tag Load (0.7ms) SELECT tags.*, taggings.tags_count AS count FROM "tags" JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM "taggings" INNER JOIN items ON items.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Item' AND taggings.context = 'tags') AND (taggings.taggable_id IN(13)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id ORDER BY count LIMIT 5
Rendered layouts/_navigation.html.erb (6.1ms)
Rendered layouts/_site_nav.html.erb (0.6ms)
Rendered layouts/_messages.html.erb (0.2ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 1069ms (Views: 1066.0ms | ActiveRecord: 2.3ms)
The output from Mini-Profiler at the top, shows that the main application.html.erb is adding significantly to load time.
Here is another example of another app where the views are the ones that take the most time to render:
Started GET "/" for 127.0.0.1 at 2013-05-17 17:55:01 -0500
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Processing by HomeController#index as HTML
Category Load (0.2ms) SELECT "categories".* FROM "categories" LIMIT 6
Banner Load (0.2ms) SELECT "banners".* FROM "banners" INNER JOIN "banner_types" ON "banner_types"."id" = "banners"."banner_type_id" WHERE (banner_types.name = 'Featured')
Banner Load (0.2ms) SELECT "banners".* FROM "banners" INNER JOIN "banner_types" ON "banner_types"."id" = "banners"."banner_type_id" WHERE (banner_types.name = 'Side')
Product Load (0.3ms) SELECT "products".* FROM "products"
Vendor Load (0.3ms) SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" IN (12, 11, 10)
Vendor Load (0.2ms) SELECT "vendors".* FROM "vendors"
User Load (0.2ms) SELECT "users".* FROM "users"
Rendered home/_popular_products.html.erb (16.1ms)
Rendered home/_popular_stores.html.erb (2.4ms)
Rendered home/index.html.erb within layouts/application (26.4ms)
Piggybak::Sellable Load (0.2ms) SELECT "sellables".* FROM "sellables" WHERE "sellables"."id" = 1 LIMIT 1
Rendered layouts/_login_nav.html.erb (8.2ms)
Rendered layouts/_navigation.html.erb (0.8ms)
CACHE (0.0ms) SELECT "vendors".* FROM "vendors"
Rendered layouts/_store_dropdown.html.erb (2.2ms)
Rendered layouts/_header.html.erb (18.1ms)
Rendered layouts/_messages.html.erb (0.3ms)
Rendered layouts/_footer.html.erb (0.9ms)
Completed 200 OK in 242ms (Views: 209.8ms | ActiveRecord: 1.9ms)
Granted, this particular time is just 209.8ms, but it has been as high as 5,000ms at various loading times.
How can I optimize the rendering of these views & partials? Or what tool can I use to at least figure out what is causing the long load time so I can chip away at it slowly?
You are profiling this in development. Be aware that because of the config.cache_classes setting, you app is reloaded with every request in that environment. Profiling with that setting set to true might give you different (and usually faster) results.
In one of your comments, you mention a Vendor.all.each snippet. I would check how long that actually takes. Just wrap it in a results = Benchmark.measure do block and see what results.real gives you. Let's say it finishes in 50 ms.
You have three options, generally:
If the 50ms is unsatisfactory, read the partial rendering result from a static file that you regenerate in certain intervals. This can be done by using the rails caching mechanisms or by putting it into a file that you regenerate with a cronjob. Either way, this involves some kind of delay in respect to new vendors that might have been created between intervals.
Or, if 50ms is fine for you, you can cast them to JSON (which is fast, have a look at https://github.com/ohler55/oj) and as suggested before, pull it with AJAX and render the html with javascript. This has the advantage that the actual rendering of the html is done within the browser. The browser is faster doing it and it usually has less load to cope with.
If you're unhappy with 50 ms and you can't live with slightly outdated information on your page, you would have to start doing option 2 but without ActiveRecord, so with the mysql ruby adapter directly. Or you make sure that whenever a new Vendor is created or an old one updated, you add JSON into a text column of the respective vendor, perhaps with some after_validation callback. So that you can at least do
vendors = Vendor.select(:json_cache).map{|v| v.json_cache}.join(',')
vendors = "[#{vendors}]"
render :json => vendors
... not exactly beautiful but blazing fast.
I hope, this is what you wanted to know.
In your second example, I see queries for:
All banners of type "Featured"
All banners of type "Side"
All products
All vendors
All users
If your partials have to iterate through huge collections of objects, it will slow down rendering times considerably.
Watch for iterators that call collections like Vendor.all.each do |v|. Model class methods should never be called in the view. Build a collection in the controller of objects you need, then send to the view in an instance variable.
Hey guys i dunno how to put this i have done a bit ror..I kinda changed all my work to my new lap and i have win 7 and i am usin ror 3.2.11 for most of my projects...The problem is that when i do
<%= link_to("Delete", user, :method => :delete, :confirm => "Are you sure that you want to delete this user") %>
it renders as
Delete
but when i click this link it actually does a get request here are the server logs
Started GET "/users/3" for 127.0.0.1 at 2013-03-20 06:21:40 +0530
Processing by UsersController#show as HTML
Parameters: {"id"=>"3"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "3"]]
Rendered users/show.html.erb within layouts/application (0.0ms)
Rendered layouts/_header.html.erb (2.0ms)
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered layouts/_body.html.erb (2.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 19ms (Views: 19.0ms | ActiveRecord: 0.0ms)
3 things are require for this to work (your code is correct)
Gemfile must contain
group :assets do
gem 'jquery-rails'
end
views/layouts/application.html.(erb or haml) must contain in the header tag
<%= javascript_include_tag 'application' %>
assets/javascripts/application.js must contain
//= require jquery
//= require jquery_ujs
I've tried looking at other answers for this but I can't seem to figure out why my redirect isn't working.
So I'm using Devise with Rails 3.1, and I'm making a shopping site. Visitors aren't allowed to add things to their cart unless they are signed in. This is what I'm having trouble with: if they aren't signed in, I want to redirect them to the Items index page. Here's what I have:
class ItemsController < ApplicationController
def add_to_cart
#item = Item.find(params[:id])
if current_user
#item.update_attributes(:cart_id => #current_cart.id)
redirect_to :back
else
redirect_to categories_path, notice: 'You must sign in to add an item to your cart.'
end
end
.
.
.
end
As of right now, when I click the link to add to cart, the method gets executed (I can see Rails loading and defining #item in the server log), and it reaches the 'else' statement, but no redirect happens.
I've already generated scaffolding for the index, new, etc. (all the RESTful actions). Also, I'm sure that I'm reaching the add_to_cart method because I've tried debugging with some puts statements. What's happening here?
EDIT:
Also, another weird thing which may be of use... The server seems to try to execute this method twice, and tries to 'get' categories twice:
Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.3ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 26ms
Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.2ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 25ms
Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 35ms (Views: 28.5ms | ActiveRecord: 4.2ms)
Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 37ms (Views: 30.6ms | ActiveRecord: 4.2ms)
EDIT 2 (as requested by Delba)
resources :items do
member do
get 'add_to_cart'
end
end
EDIT 3: changing the else statement to respond to javascript
respond_to do |format|
format.js { redirect_to items_path, notice: 'You must sign in to add an item to your cart.' }
end
For anyone who may need answers to this question, simply replace redirect_to statements with the following:
respond_to do |format|
format.js
end
Then, in your views under items, make a add_to_cart.js.erb page, consisting of javascript to make notices, or do whatever. Here's what I put in mine:
alert("Need to be signed in")
EDIT: Also, for the part where it executes twice: this is somewhat unrelated, but for some reason by default Rails includes duplicate Javascripts. Specifically, look at application.js: it says require jquery and require jquery_ujs. Disable one of these and you're home free.
To disable one of these javascripts:
Go to assets/application.js
Remove the comments (the // ) before require jquery, require tree .
This way, Rails doesn't assume the default and instead includes only jquery and whatever other javascripts you have in assets/javascripts