Showing posts with label rubygems. Show all posts
Showing posts with label rubygems. Show all posts

Wednesday, January 31, 2018

How do I include a Jquery UI slider in my view?

Leave a Comment

I'm trying to incorporate a slider (http://simeydotme.github.io/jQuery-ui-Slider-Pips/#installation) into my view, which is supposedly part of jquery-ui. I have these in my Gemfile

gem 'jquery-rails' gem 'jquery-ui-rails' 

And when i open Gemfile.lock I see

jquery-rails (4.3.1)   rails-dom-testing (>= 1, < 3)   railties (>= 4.2.0)   thor (>= 0.14, < 2.0) jquery-ui-rails (6.0.1)   railties (>= 3.2.16) 

But although I have this HTML on my page

<div class="slider"></div> 

and I include this Javascript

$('.slider').slider().slider('pips'); 

I get the JS console error

Uncaught TypeError: $(...).slider is not a function 

when my page loads. The documentation says I have to include jQuery 2.1.1 and Jquery UI 2.1.1. I can't tell if I'm doing that properly or not.

Edit: Including content of app/assets/application.js in response to answer given ...

//= require jquery //= require jquery_ujs //= require turbolinks //= require_tree . 

2 Answers

Answers 1

Seems everything is ok, you need some customization like on the assets/application.js top of the file add sorting like

//= require jquery //= require jquery_ujs 

Make sure jquery not rendering twice and your slider library include properly then edit your slider JS like below

(function($){     "use strict";     $(document).on('ready', function(){         $('.slider').slider().slider('pips');     }); }); 

Or like below

$(document).on('turbolinks:load', function(){     $('.slider').slider().slider('pips'); }); 

For refactoring you can use this js on the same page underneath where your slider using <script type="text/javascript"> .... </script> tag

Restart the server after implementing this

Hope it helps

Answers 2

Try this instead: Include the js from within the html

<script src="//code.jquery.com/jquery-3.3.1.min.js"></script> <script src="//code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> 

See if it works after that.

Read More

Thursday, September 15, 2016

pdf-writer-1.1.8/lib/pdf/writer.rb:712: invalid multibyte char (US-ASCII)

Leave a Comment

I am using ruby version 1.9.3 and getting error when I start thin server

    /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.12.4/lib/bundler/runtime.rb:100:in `require': /usr/local/lib/ruby/gems/1.9.1/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:712: invalid multibyte char (US-ASCII) (SyntaxError) /usr/local/lib/ruby/gems/1.9.1/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:712: invalid multibyte char (US-ASCII) /usr/local/lib/ruby/gems/1.9.1/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:712: syntax error, unexpected $end, expecting keyword_end     content = "%PDF-#{@version}\n%âãÃÓ\n"                                     ^     from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.12.4/lib/bundler/runtime.rb:100:in `rescue in block in require'     from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.12.4/lib/bundler/runtime.rb:77:in `block in require'     from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.12.4/lib/bundler/runtime.rb:70:in `each' 

I got solution from here pdf-writer invalid multibyte char (US-ASCII) expecting keyword_end content = "%PDF-#{@version}\n%âãÏÓ\n" Rails 3

I have changed my gemfile pdf-writer to

gem 'pdf-writer', :git => 'git://github.com/metaskills/pdf-writer.git'

Now when I run bundler to install the pdf-writer gem I get timeout error as below

github.com[0: 192.30.252.123]: errno=Connection timed out fatal: unable to connect a socket (Connection timed out) Retrying git clone 'git://github.com/metaskills/pdf-writer.git' "/usr/local/lib/ruby/gems/1.9.1/cache/bundler/git/pdf-writer-ce9b6a7a72845526358421df666643f35691567f" --bare --no-hardlinks --quiet due to error (4/4): Bundler::Source::Git::GitCommandError Git error: command `git clone 'git://github.com/metaskills/pdf-writer.git' "/usr/local/lib/ruby/gems/1.9.1/cache/bundler/git/pdf-writer-ce9b6a7a72845526358421df666643f35691567f" --bare --no-hardlinks --quiet` in directory /apps/hours_report_3/current has failed. github.com[0: 192.30.252.123]: errno=Connection timed out fatal: unable to connect a socket (Connection timed out) Git error: command `git clone 'git://github.com/metaskills/pdf-writer.git' "/usr/local/lib/ruby/gems/1.9.1/cache/bundler/git/pdf-writer-ce9b6a7a72845526358421df666643f35691567f" --bare --no-hardlinks --quiet` in directory /apps/my_dashboard/current has failed. 

1 Answers

Answers 1

So this might be a couple different issues. For the 1st error, you could add explicit encoding to the top of /usr/local/lib/ruby/gems/1.9.1/gems/pdf-writer-1.1.8/lib/pdf/writer.rb-

# encoding: utf-8 

This would be hacking into an older library and brittle at best, as your Gemfile would not be portable to other machines.

For the 2nd error, it looks like there might be a conflict between the existing gem in cache and the remote one that you are attempting to install, since they have the same name.

You could try uninstalling it locally to clear the cache-

gem uninstall pdf-writer 

And then bundle it again-

bundle install 

Ruby 1.9.3 reached end of life in February 2015, so this is not an ideal situation, but given the constraints, this should get you going.

Read More

Monday, May 2, 2016

How to get list of all countries and cities in rails?

Leave a Comment

Is there a country and city gem where user will be able to select a country and based on selected country he will select a city?

I get multiple solution but they all support states not cities

gem 'country_select' gem 'countries' gem 'carmen-rails' 

I want a list of all countries on dropdown and when I select any country then all cities of country will appear in cities dropdown. Is this possible through any gem?

4 Answers

Answers 1

Use the aptly named city-state gem.


As seen by the README, you can do:

CS.cities(state, country) 

If you want all cities in a given country, regardless of state, you could do:

CS.states(country).keys.flat_map { |state| CS.cities(state, country) } 

To list all countries:

CS.countries 

The cool thing is that it uses data from MaxMind so you can be sure the list will be up to date. To refresh your list, you can run:

CS.update 

The only drawback is that it requires rails (active support specifically) to run, but this isn't a problem in your case.

Answers 2

You can try jQuery plugin - bootstrap state picker

After including plugin inside your application

You can use it like this(example from http://bootstrapformhelpers.com/state):

<select id="countries_states1" class="form-control bfh-countries" data-country="US"></select>  <select class="form-control bfh-states" data-country="countries_states1"></select> 

Answers 3

You can use country picker jQuery plugins of Bootstrap as following:

<select class="form-control bfh-countries" data-country="US"></select> 

Answers 4

My recommendation is that unless you can be 100% certain the list is accurate and complete (and I'm skeptical that that is possible), don't do it.

I've registered on web sites in Singapore and other places where I had to select from a dropdown list of cities that did not include my town (and many other) correct towns (I live in the USA). I was forced to select an incorrect value pretty far away because the list was quite incomplete and it would not permit me to not select anything.

At the very least, if you want to provide choices, permit the user to enter a value not included in those choices.

Read More

Tuesday, March 29, 2016

Using the Rails gem 'Mailboxer' and the sender_id is always zero

Leave a Comment

Just integrated Mailboxer and I'm running into one problem. When I send a new message the sender_id in the mailboxer_notifications table is always zero instead of the id of the sender.

I'm using the friendly_id gem for usernames and I think this is where the problem is but I cant find out how to fix it (if it even is the problem).

messages/new.html.erb

<% provide(:title, "New Message") %>  <%= form_tag user_messages_path, method: :post do %>    <div class="form-group">     <%= label_tag 'message[subject]', 'Subject' %>     <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>   </div>    <div class="form-group">     <%= label_tag 'message[body]', 'Message' %>     <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>   </div>   <div class="form-group">   <%= label_tag 'recipients', 'Choose recipients' %>   <%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %> </div>    <%= submit_tag 'Send', class: 'btn btn-primary' %> <% end %> 

messages_controller.rb

class MessagesController < ApplicationController   before_action :authenticate_user    def new     @user = current_user     @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]   end    def create     @user = current_user     recipients = User.where(id: params['recipients'])     conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation     flash[:success] = "Message has been sent!"     redirect_to user_conversation_path(@user, conversation)   end    private    def authenticate_user     unless ((current_user.id = params[:user_id]) unless current_user.nil?)       flash[:error] = 'Looks like your not supposed to be there'       redirect_to login_path     end   end end 

routes.rb

resources :users do   resources :conversations, only: [:index, :show, :destroy] do     member do       post :reply       post :restore       delete :empty_trash       post :mark_as_read     end   end   resources :messages, only: [:new, :create] end 

I mostly followed this tutorial when integrating the gem. I have no idea why it isn't saving the user id properly when they send a new message.

update The reason I suspect friendly_id is causing it so save the sender_id as 0 is because when I don't use friendly id in the url e.g. users/1/messages/new it works fine but users/example-user/messages/new it doesn't save the user id when creating a new message

1 Answers

Answers 1

I am not quite sure if I follow your authenticate_user implementation:

def authenticate_user   unless ((current_user.id = params[:user_id]) unless current_user.nil?)     flash[:error] = 'Looks like your not supposed to be there'     redirect_to login_path   end end 

You may want to simplify it.

If you are trying to compare current_user.id to params[:user_id], you need to use current_user.id == params[:user_id], not current_user.id = params[:user_id]. Its ==, not =.

Read More