I'm new to Rails, I'm trying to get the "browser" gem working.
(https://github.com/fnando/browser)
I was hoping someone had some example code they code post. I installed the gem by putting gem "browser", require: "browser/browser" in my gemfile.
I ran gem install browser
So I don't really know how to use the gem to actually get the users browser info. I tried just doing this as a test in the view <%= browser.full_version %> but I get Template::Error (undefined local variable or method browser'
any example code on what to put in a view/modal/controller would be appreciated, thanks.
You need to instantiate the object. So you can do something like this:
def index
#browser = Browser.new(request.env["HTTP_USER_AGENT"])
end
Then you can call the object in your view:
Device: <%= #browser.device.name %><br>
Platform: <%= #browser.platform.name %><br>
Info: <%= #browser.to_s %><br>
PS. I tried using this gem but found useragent which seemed to better display the information I wanted.
You can simply install the gem and "call it" in your view.
For example:
<% if browser.device.mobile? || browser.platform.android? %>
To install this gem you need:
add gem declaration to Gemfile
run bundle install from the console
restart your development server
P.S. when you are starting to use any gem, you should carefully read gem's README first. In 95% cases they are documented good enough.
Related
I am using chartkick (added gem 'chartkick' in my Gemfile) and have defined it in my views/layouts/application.html.erb like this:
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
But on running the server, it results in an error:
Sprockets::Rails::Helper::AssetNotPrecompiled in Main#index
chartkick.js
I am using rails 5.0.7. I tried following the way mentioned in https://chartkick.com/#rails-5-sprockets. But still, it results in the same error.
Can you help me in finding what I'm doing wrong?
Or If anyone could tell me what direction should I head to, that would be awesome as well.
UPDATE: Adding config.assets.check_precompiled_asset = false fixes the issues.
But, adding this line means it will not verify/check precompiled_assets. Which is not a good thing, is it?
I am using form_for tag and Its working in Rails 3.0.4 environment.
But when I tried to update my project to Rails 4.It gives following errors
wrong number of arguments (3 for 2). Here is my code
<%= form_for #email, :url => alerts_path do |f| %>
<% end %>
Try to remove things that may try to change things in views.
In my case the problem was with client_side_validations gem
Hope this helps.
Not sure why yet, but removing the "meta_search" gem from my Gemfile fixed this exact problem for me on Rails 4.
If you google this problem you may find this GitHub issue that attributes it to the "client_side_validations" gem, but using the suggested "rails-4-quick-fixes" branch of that repo didn't fix the issue for me.
I think that the error with simple_form_for was a red herring, as I was getting the same error even using Rails built in form_for, and was getting wrong number of arguments (3 for 2) regardless of how many arguments I actually passed into either method.
meta_search hasn't had a new release since February 2, 2012, so I can only assume something in the gem wasn't behaving nicely with Rails 4. If I discover what the problem was specifically, I'll update this answer. It seems kind of lame to have to "just stop using" meta_search, but that's as much as I can offer so far. Thanks!
I can't see any issue with your form_for, can you post backtrace of exception ? may be another gem is overriding default form_for ?
Your syntax is correct.
Try changing it like this:
<%= form_for(#email, url: alerts_path) do |f| %>
#form fields goes here
<% end %>
I tried to see if there is any change in source-code for form_for in Rails 4.0 API; It's still the same.
Have a look at Line 262 at https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb
Let me know if it works
Remove gem 'meta_search' from your gem file, and look into using gem ransack instead. https://github.com/activerecord-hackery/ransack
I am making a rails(3.0.11) application using youtube_it api. My index action of videos controller contains the basic code of getting the client object:
#yt_client ||= YoutubeIt::Client.new(:username => 'my uname', :password => 'my pwd',:dev_key => 'my devkey')
I have installed the gem using gem install youtube_it, included it in the app's gem file gem 'youtube_it' and ran bundle install. But when i restart the server and go to the index page,
i get this Error
"uninitialized constant VideosController::YoutubeIt"
What am i doing wrong?
Try:
YouTubeIt
With a capital T
Hope that helps
I think you need to do
require 'youtube_it'
in either the application controller or in some lib or config file
Hope it helps
you have a typo, the correct sintaxis is
client = YouTubeIt::Client.new
and if you still have problem you can try this way
client = ::YouTubeIt::Client.new
cheers!
After authenticating with simplegeo, I am simply trying to perform this:
SimpleGeo::Client.get_context(37.772445,-122.405913)
Turn the result into #variable and display the #variable on my "view" page.
Any suggestions? Thanks!
I'm working on a gem right now called SimpleGeo-Rails. It should make the whole process of working with the SimpleGeo Places a lot more rails-like. Just add the following to your Gemfile and then checkout the readme.
gem 'simplegeo-rails', :git => 'https://github.com/mgadda/simplegeo-rails.git'
Is BlueCloth compatible with Rails 3? I can't make it work, maybe someone uses it?
There is supposed to be a helper called 'markdown' available in the views after requiring 'bluecloth', but this doesn't seem to be available.
I'm upgrading an app to rails3 right now and it worked fine for me. I use a helper function called "format" in templates though the code below also provides a markdown function (in rails3 you'll have to use that with raw()). Here's the contents of my [project]/app/helpers/application_helper.rb
module ApplicationHelper
# Format text for display.
def format(text)
sanitize(markdown(text))
end
# Process text with Markdown.
def markdown(text)
BlueCloth::new(text).to_html
end
end
Like a previous poster said, you'll also need
gem 'bluecloth'
in your [project]/Gemfile. My template looks like:
<p><%= format #post.body %></p>
With the markdown function it would be:
<p><%= raw(markdown(#post.body)) %></p>
So I use the format function. Rename the functions however you want.
I've created a fresh Rails 3 app and in the Gemfile I added:
gem 'bluecloth', '>= 2.0.0'
Then opened the console:
ruby-1.8.7-p302 > BlueCloth.new('**hello**').to_html
=> "<p><strong>hello</strong></p>"
So it appears to be working, at least for me.
You could also try Rdiscount which I am not shure but I think is based on the same C library, or at least has similar benchmarks.
You should be more specific in how is it not working: Does it raises an error? Doesn't it renders html? etc...
What you could do, not saying it is pretty, is creating an initializer in your rails project and put the following in it:
require 'bluecloth'
class String
def markdown
BlueCloth.new(self).to_html
end
end
This should enable the markdown method on every string object.
I'd suggest switching to RDiscount over BlueCloth. It's a drop in replacement and is better on all counts.
http://github.com/rtomayko/rdiscount