association between three models Posted: 06 Aug 2016 07:44 AM PDT good day in this working association that I'm trying to do, using nested_form #_form.html.erb in post <%= f.fields_for :addresses do |address| %> <% address.association :state %> <% end %> class Post < ApplicationRecord has_many :addresses, as: :addressable, dependent: :destroy accepts_nested_attributes_for :addresses, allow_destroy: true end class Address < ApplicationRecord belongs_to :states belongs_to :addressable, :polymorphic => true end class State < ApplicationRecord has_many :addresses end print the error: Association :state not found!!! help me! |
rails 5 mysql adapter issue Posted: 06 Aug 2016 07:38 AM PDT Am trying to create a model in rails 5 and have installed mysql-community-server. Also I have installed both mysql and mysql2 gem's. But when i run rails generate model Book getting below error, [root@server-linux library]# rails generate model Book /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require': Could not load 'active_record/connection_adapters/mysql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (LoadError) from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `block in require' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/connection_handling.rb:53:in `establish_connection' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/railtie.rb:125:in `block (2 levels) in <class:Railtie>' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:44:in `each' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/base.rb:324:in `<module:ActiveRecord>' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/base.rb:24:in `<top (required)>' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:338:in `active_record_configured?' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:259:in `disconnect_database' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:97:in `preload' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:143:in `serve' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run' from /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>' from /root/.rbenv/versions/2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /root/.rbenv/versions/2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require' from -e:1:in `<main>' [root@server-linux library]# database.yaml: [root@server-linux library]# cat config/database.yml development: adapter: mysql2 database: library_development username: root password: [password] host: localhost test: adapter: mysql2 database: library_test username: root password: [password] host: localhost production: adapter: mysql2 database: library_production username: root password: [password] host: localhost [root@server-linux library]# Version details: [root@server-linux library]# rails -v Rails 5.0.0 [root@karthick-linux library]# gem list | grep mysql activerecord-mysql-adapter (0.0.1) mysql (2.9.1) mysql2 (0.4.4) [root@server-linux library]# Even I created application with the command rails new library -d mysql Any idea why its failing with mysql adapter connection. |
Convert instance object into JS object in "view" in order to assign into data- html attribute cuts off in middle Posted: 06 Aug 2016 07:19 AM PDT I am looping instance variable "@packages" set in controller and am trying to assign single object into one of the html element by using "data-". For changing ruby object into json i am using ".to_json" as follows: <% @packages.each do |package| %> <% json_package=package.to_json %> <a href="#" data-single-package=<%= json_package %>></a> <% end %> However, on inspecting the anchor tag i found json object is cutting off in one of the property which had a value with space in between it. As you can see, "title" property has value "annarpurna circuit", but ".to_json" is cutting off in space by adding ". How can i make this whole string pass through?? |
Rails nested routing error Posted: 06 Aug 2016 07:28 AM PDT I am trying to build a forum while nesting the routes and I keep running into this error: undefined method `forum_threads_path' for I have nested my routes as so in my routes.rb file resources :forum_subjects do resources :forum_threads, module: :forum_subjects do resources :forum_msgs, module: :forum_threads end end I am trying to call this using form_for in a partial of the _form.html.erb <%= form_for [@forum_subject, @forum_thread] do |f| %> My view files are also located in folders within the one main one: forum_subjects/forum_threads/forum_posts where all the view files are also nested. |
which application_rb file should I edit? Posted: 06 Aug 2016 06:53 AM PDT On my server I did a search with sudo find . -name "application_controller.rb" . There's so many files of that name. Which one should I edit so the changes will be reflected on my server? ./home/myapp/apps/myapp/releases/20140208000704/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140116094931/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140114154804/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140117124202/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140120094758/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140116102758/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140117125636/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140116123905/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140116115403/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140117090645/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140121091622/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140212214841/app/controllers/application_controller.rb ./home/myapp/apps/myapp/releases/20140205001541/app/controllers/application_controller.rb ./home/myapp/apps/myapp/shared/cached-copy/app/controllers/application_controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/best_in_place-1.1.2/test_app/app/controllers/application_co ntroller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/sass-rails-3.2.5/test/fixtures/sass_project/app/controllers /application_controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/sass-rails-3.2.5/test/fixtures/engine_project/app/controlle rs/engine_project/application_controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/sass-rails-3.2.5/test/fixtures/engine_project/test/dummy/ap p/controllers/application_controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/sass-rails-3.2.5/test/fixtures/scss_project/app/controllers /application_controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/devise-2.0.6/test/rails_app/app/controllers/application_con troller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/rest-graph-2.0.1/example/rails3/app/controllers/application _controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/rest-graph-2.0.1/example/rails2/app/controllers/application _controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/railties-3.2.3/lib/rails/generators/rails/app/templates/app /controllers/application_controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/railties-3.2.3/guides/code/getting_started/app/controllers/ application_controller.rb ./home/myapp/apps/myapp/shared/bundle/ruby/1.8/gems/gmaps4rails-1.5.6/spec/dummy/app/controllers/application_co ntroller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/best_in_place-1.1.2/test_app/app/controllers/application_controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/email_spec-1.2.1/examples/rails3_root/app/controllers/application_contr oller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/sass-rails-3.2.5/test/fixtures/sass_project/app/controllers/application _controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/sass-rails-3.2.5/test/fixtures/engine_project/app/controllers/engine_pr oject/application_controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/sass-rails-3.2.5/test/fixtures/engine_project/test/dummy/app/controller s/application_controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/sass-rails-3.2.5/test/fixtures/scss_project/app/controllers/application _controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/devise-2.0.6/test/rails_app/app/controllers/application_controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/rest-graph-2.0.1/example/rails3/app/controllers/application_controller. rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/rest-graph-2.0.1/example/rails2/app/controllers/application_controller. rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.3/lib/rails/generators/rails/app/templates/app/controllers /application_controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.3/guides/code/getting_started/app/controllers/application_ controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails3.0/app/controllers/application_control ler.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails3.2/app/controllers/application_control ler.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails2.3/app/controllers/application_control ler.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails4.0/app/controllers/application_control ler.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails3.1/app/controllers/application_control ler.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails_apps/2.3/empty/app/controllers/applica tion_controller.rb ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails_apps/2.3/mycook/app/controllers/applic ation_controller.rb myapp@myapp:/$ sufo nano ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails_apps/ 2.3/mycook/app/controllers/application_controller.rb -bash: sufo: command not found myapp@myapp:/$ sudo nano ./home/myapp/.rvm/gems/ruby-1.8.7-p374/gems/passenger-4.0.14/test/stub/rails_apps/ 2.3/mycook/app/controllers/application_controller.rb myapp@myapp:/$ |
require "mysql2" giving error "false" in rails terminal, even after the gem is installed? [duplicate] Posted: 06 Aug 2016 06:28 AM PDT This question already has an answer here: i am trying to connect to mysql database from my ruby program. this is my code: def connect_to_mysql p require "mysql" @db_host = "some ip address" @db_user = "user" @db_name = "adms_db" client = Mysql::Client.new(:host => @db_host, :username => @db_user, :database => @db_name) @cdr_result = client.query("SELECT * from your_db_table_name;") @cdr_result.inspect end but the statement p require "mysql2" is returning false . i have mysql2 gem in my gem file. and i even installed mysql2 from the terminal also. gemfile.lock file GEM remote: https://rubygems.org/ specs: actionmailer (4.2.5) actionpack (= 4.2.5) actionview (= 4.2.5) activejob (= 4.2.5) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 1.0, >= 1.0.5) actionpack (4.2.5) actionview (= 4.2.5) activesupport (= 4.2.5) rack (~> 1.6) rack-test (~> 0.6.2) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (4.2.5) activesupport (= 4.2.5) builder (~> 3.1) erubis (~> 2.7.0) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.2) activejob (4.2.5) activesupport (= 4.2.5) globalid (>= 0.3.0) activemodel (4.2.5) activesupport (= 4.2.5) builder (~> 3.1) activerecord (4.2.5) activemodel (= 4.2.5) activesupport (= 4.2.5) arel (~> 6.0) activesupport (4.2.5) i18n (~> 0.7) json (~> 1.7, >= 1.7.7) minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) byebug (9.0.5) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) coffee-script (2.4.1) coffee-script-source execjs coffee-script-source (1.10.0) concurrent-ruby (1.0.2) debug_inspector (0.0.2) erubis (2.7.0) execjs (2.7.0) globalid (0.3.7) activesupport (>= 4.1.0) i18n (0.7.0) jbuilder (2.6.0) activesupport (>= 3.0.0, < 5.1) multi_json (~> 1.2) jquery-rails (4.1.1) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.4) mime-types (>= 1.16, < 4) mime-types (3.1) mime-types-data (~> 3.2015) mime-types-data (3.2016.0521) mini_portile2 (2.1.0) minitest (5.9.0) multi_json (1.12.1) mysql2 (0.3.18) nokogiri (1.6.8) mini_portile2 (~> 2.1.0) pkg-config (~> 1.1.7) pkg-config (1.1.7) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) rails (4.2.5) actionmailer (= 4.2.5) actionpack (= 4.2.5) actionview (= 4.2.5) activejob (= 4.2.5) activemodel (= 4.2.5) activerecord (= 4.2.5) activesupport (= 4.2.5) bundler (>= 1.3.0, < 2.0) railties (= 4.2.5) sprockets-rails rails-deprecated_sanitizer (1.0.3) activesupport (>= 4.2.0.alpha) rails-dom-testing (1.0.7) activesupport (>= 4.2.0.beta, < 5.0) nokogiri (~> 1.6.0) rails-deprecated_sanitizer (>= 1.0.1) rails-html-sanitizer (1.0.3) loofah (~> 2.0) railties (4.2.5) actionpack (= 4.2.5) activesupport (= 4.2.5) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (11.2.2) rdoc (4.2.2) json (~> 1.4) sass (3.4.22) sass-rails (5.0.6) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) spring (1.7.2) sprockets (3.7.0) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.1.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.5) turbolinks (5.0.1) turbolinks-source (~> 5) turbolinks-source (5.0.0) tzinfo (1.2.2) thread_safe (~> 0.1) uglifier (3.0.1) execjs (>= 0.3.0, < 3) web-console (2.3.0) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) PLATFORMS ruby DEPENDENCIES byebug coffee-rails (~> 4.1.0) jbuilder (~> 2.0) jquery-rails mysql2 rails (= 4.2.5) sass-rails (~> 5.0) sdoc (~> 0.4.0) spring turbolinks uglifier (>= 1.3.0) web-console (~> 2.0) BUNDLED WITH 1.11.2 terminal code: anurag@LIBSERV-PC:~/vikas/lutura$ gem install mysql2 Fetching: mysql2-0.4.4.gem (100%) Building native extensions. This could take a while... Successfully installed mysql2-0.4.4 1 gem installed anurag@LIBSERV-PC:~/vikas/lutura$ anurag@LIBSERV-PC:~/vikas/lutura$ rails c Loading development environment (Rails 4.2.5) 2.2.2 :001 > require "mysql2" => false 2.2.2 :002 > |
Why URL is distorted? Posted: 06 Aug 2016 06:55 AM PDT Please help solve the problem. I use rails 5. I create simple site, whitch have follow navigation: kalinin@kalinin ~/rails/test5 $ rails routes Prefix Verb URI Pattern Controller#Action pages_home GET /pages/home(.:format) pages#home pages_about GET /pages/about(.:format) pages#about pages_portfolio GET /pages/portfolio(.:format) pages#portfolio pages_blog GET /pages/blog(.:format) pages#blog pages_contact GET /pages/contact(.:format) pages#contact root GET / pages#home home page have 2 paths: http://localhost:3000/pages/home http://localhost:3000/ home page have js-slider. initialization code: function clearText(field) { if (field.defaultValue == field.value) field.value = ''; else if (field.value == '') field.value = field.defaultValue; } var flashvars = {}; flashvars.xml_file = "photo_list.xml"; var params = {}; params.wmode = "transparent"; var attributes = {}; attributes.id = "slider"; swfobject.embedSWF("flash_slider.swf", "flash_grid_slider", "960", "350", "9.0.0", false, flashvars, params, attributes); ddsmoothmenu.init({ mainmenuid: "templatemo_menu", //menu DIV id orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v" classname: 'ddsmoothmenu', //class added to menu's outer DIV //customtheme: ["#1c5a80", "#18374a"], contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"] }) Problem: on path http://localhost:3000/pages/home this slider is not displays. console output follow error message: GET http://localhost:3000/pages/photo_list.xml 404 (Not Found) But on path http://localhost:3000/ slider is displays without problems. ps: path pages/photo_list.xml is NOT exist. but path public/photo_list.xml is exist. PageController: class PagesController < ApplicationController def home render layout: "home" end def about end def portfolio render layout: "portfolio" end def blog end def contact end end |
rails API active model serializer root node not working Posted: 06 Aug 2016 05:11 AM PDT I have a rails 4.2.5 api app. For some reason the json root node is not included in the response and I don't understand why. curl http://localhost:3000/api/v1/category/science returns {"title":"science","sub_categories":34}% instead of {"category": {"title":"science","sub_categories":34}%} The code: controller class Api::V1::CategoryController < ApplicationController def show category = params[:category] || "sports" @category = Category.where(cat_title: category.capitalize).first respond_to do |format| format.json { render json: @category, serializer: CategorySerializer, root: "category" } end end end serializer class CategorySerializer < ActiveModel::Serializer attributes :title, :sub_categories def title URI::encode(object.cat_title.force_encoding("ISO-8859-1").encode("utf-8", replace: nil).downcase.tr(" ", "_")) end def sub_categories object.cat_subcats end end |
Getting uniqueness on multiple model association Posted: 06 Aug 2016 07:16 AM PDT There are three models: # Table name: activities_people # # activity_id :integer not null # person_id :integer not null # date :date not null # id :integer not null, primary key # # Table name: activities # # id :integer not null, primary key # name :string(20) not null # description :text # active :boolean not null # day_of_week :string(20) not null # start_on :time not null # end_on :time not null Third model is people. Associations: class Activity < ActiveRecord::Base has_many :activities_people has_many :people, through: :activities_people end class ActivitiesPerson < ActiveRecord::Base belongs_to :person belongs_to :activity end class Person < ActiveRecord::Base has_many :activities, through: :activities_people end Issue: I don't know how to create validation method in ativities_person.rb to protect joining on activities, that overlaps timely. Example: person_id:1 join to activity, that will 08.08.2016(date) and last 09:00(start_on) - 10:00(end_on). Same person want save to another activity, that will also be 08.08.2016 and last 09:30(start_on) - 10:30(end_on). Now validation should throw error that he's save to other activity in same time(time ranges overlap). Validation first should check that dates match, next should check time overlaps of activities. What I tried: def check_join_client # check if client joined to some activities in same date as current join activities_date = person.activities_people.where(date: date) if activities_date.any? # get start_on and end_on for current activity and date get_activities = person.activities.where(id: activities_date.pluck(:activity_id)) ol_activities = get_activities.where('((start_on <= :start_on AND end_on >= :end_on) OR (start_on <= :start_on AND end_on <= :end_on AND end_on >= :start_on) OR (start_on >= :start_on AND start_on <= :end_on AND end_on >= :end_on))', start_on: get_activities.pluck(:start_on), end_on: get_activities.pluck(:end_on)) end end My method isn't correct. I'm confuse how to resolve my problem. |
Rails undefined local variable when rendering partial Posted: 06 Aug 2016 06:18 AM PDT I have a piece of code that renders one of two partials like this (I use slim-lang): - case restream.service - when 'youtube' = render partial: 'youtube', locals: { provider: restream.provider } - when 'custom' debug = restream.provider = render partial: 'custom', locals: { provider: restream.provider } In both _youtube.slim and _custom.slim I just call smth like p = provider.name . Moreover, "custom' was created by copy-pasting code from 'youtube'. However, while rendering Rails throws an error: undefined local variable or method 'provider' for #<#<Class:0x00564028bb89d8>:0x007f92d3d0fa80> Trying to resolve this issue I just renamed _custom.slim to _custom1.slim and changed a call to = render partial: 'custom1', locals: { provider: restream.provider} and that worked! Can anyone, please, explain, how does it works and what's wrong with 'custom' name? Is it a reserved name? |
Rails: missing indexes when converting sqlite file into postgres db with pgloader Posted: 06 Aug 2016 06:07 AM PDT For my rails application, I'm converting a local sqlite3 file into a postgres database with pgloader. To do this, I created a postgres database createdb my_database_development and I create a pgload command file load database from 'db/production.sqlite3' into postgresql:///my_database_development with include drop, create tables, create indexes, reset sequences set work_mem to '16MB', maintenance_work_mem to '512 MB'; Now loading the sqlite file into the postgres db works, and I don't think I have any errors. All tables and records are copied, but when I look in my rails application, none of the records have an index (or id). Here is the output of loading the sqlite file into the database simon (master *) Blog-database $ pgloader db/development.sqlite3 postgresql:///my_database_development 2016-08-06T13:57:41.201000+03:00 LOG Main logs in '/tmp/pgloader/pgloader.log' 2016-08-06T13:57:41.222000+03:00 LOG Data errors in '/tmp/pgloader/' 2016-08-06T13:57:41.425000+03:00 WARNING Postgres warning: table "schema_migrations" does not exist, skipping 2016-08-06T13:57:41.626000+03:00 WARNING Postgres warning: table "comments" does not exist, skipping 2016-08-06T13:57:41.627000+03:00 WARNING Postgres warning: table "users" does not exist, skipping 2016-08-06T13:57:41.627000+03:00 WARNING Postgres warning: table "pictures" does not exist, skipping 2016-08-06T13:57:41.828000+03:00 WARNING Postgres warning: table "posts" does not exist, skipping table name read imported errors time ---------------------- --------- --------- --------- -------------- fetch 0 0 0 0.001s fetch meta data 8 8 0 0.050s create, truncate 0 0 0 0.343s ---------------------- --------- --------- --------- -------------- schema_migrations 12 12 0 0.068s comments 23 23 0 0.014s users 3 3 0 0.064s pictures 24 24 0 0.069s posts 4 4 0 0.055s index build completion 0 0 0 0.014s ---------------------- --------- --------- --------- -------------- Create Indexes 4 4 0 0.312s Reset Sequences 0 0 0 0.064s ---------------------- --------- --------- --------- -------------- Total streaming time 66 66 0 0.742s The pgloader logs look like this simon pgloader $ cat pgloader.log 2016-08-06T14:23:53.146000+03:00 NOTICE Starting pgloader, log system is ready. 2016-08-06T14:23:53.223000+03:00 LOG Main logs in 'NIL' 2016-08-06T14:23:53.241000+03:00 LOG Data errors in '/tmp/pgloader/' 2016-08-06T14:23:53.241000+03:00 LOG Parsing commands from file #P"/home/simon/Blog-database/sqlite.loader" 2016-08-06T14:23:53.442000+03:00 NOTICE DROP then CREATE TABLES 2016-08-06T14:23:53.443000+03:00 WARNING Postgres warning: table "schema_migrations" does not exist, skipping 2016-08-06T14:23:53.644000+03:00 WARNING Postgres warning: table "comments" does not exist, skipping 2016-08-06T14:23:53.644000+03:00 WARNING Postgres warning: table "users" does not exist, skipping 2016-08-06T14:23:53.645000+03:00 WARNING Postgres warning: table "pictures" does not exist, skipping 2016-08-06T14:23:53.645000+03:00 WARNING Postgres warning: table "posts" does not exist, skipping 2016-08-06T14:23:53.845000+03:00 NOTICE COPY schema_migrations 2016-08-06T14:23:53.846000+03:00 NOTICE CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version") 2016-08-06T14:23:53.846000+03:00 NOTICE COPY comments 2016-08-06T14:23:53.847000+03:00 NOTICE CREATE INDEX "index_comments_on_post_id" ON "comments" ("post_id") 2016-08-06T14:23:53.847000+03:00 NOTICE COPY users 2016-08-06T14:23:53.847000+03:00 NOTICE CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email") 2016-08-06T14:23:53.847000+03:00 NOTICE CREATE UNIQUE INDEX "index_users_on_reset_password_token" ON "users" ("reset_password_token") 2016-08-06T14:23:53.848000+03:00 NOTICE COPY pictures 2016-08-06T14:23:53.848000+03:00 NOTICE COPY posts 2016-08-06T14:23:54.049000+03:00 NOTICE Reset sequences Is this a rails issue? Could it be that you can't just copy an id because it behaves as a protected attribute? |
cancan + devise + Rails_admin exception false Posted: 06 Aug 2016 04:25 AM PDT gem file gem 'rails_admin', '>= 1.0.0.rc' gem 'cancancan', '~> 1.15' gem 'devise' routes... mount RailsAdmin::Engine => '/admin', as: 'rails_admin' devise_for :users root "pages#index" ................. migrate class AddRoleIdToUser < ActiveRecord::Migration[5.0] def change add_reference :users, :role, foreign_key: true end end db seed ['admin', 'developer', 'manager', 'trainer', 'trainer', 'customer'].each do |role| Role.find_or_create_by({name: role}) end rails_admin.rb config.authorize_with :cancan user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable belongs_to :role #after_create :set_admin before_create :set_default_role #after_create :set_default_role private def set_default_role if User.count == 0 self.role ||= Role.find_by_name('admin') else self.role ||= Role.find_by_name('customer') end end end role.rb class Role < ApplicationRecord has_many :users end ability.rb class Ability include CanCan::Ability def initialize(user) user ||= User.new # guest user (not logged in) if user.role_id == 1 can :manage, :all can :dashboard end end end application_controller.rb rescue_from CanCan::AccessDenied do |exception| #redirect_to root_path, :alert => exception.message flash[:error] = "bạn không phải là quản trị viên" redirect_to '/', :alert => exception.message end i have been set default: 1st user is admin have role_id point to role.name: "admin" and another is "customer" and setting cancan rule like that. i create 2 User: 1 admin and 1 customer then goto "/admin" they throw exception You are not authorized to access this page (but i admin have role_id=1). and goto root_path may you help me solve that? |
If operator doesn't work Posted: 06 Aug 2016 04:57 AM PDT From RUBY ON RAILS TUTORIAL, Michael Hartl.Chapter 7 I have one problem.If operator doesn't work I have tried to do new app,but still nothing application.html.erb: <nav> <ul class="nav navbar-nav navbar-right"> <li><%= link_to "Home", root_path %></li> <% if logged_in? %> <li><%= link_to "Users", '#' %></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"> Account <b class="caret"></b> </a> <ul class="dropdown-menu"> <li><%= link_to "Profile", current_user %></li> <li><%= link_to "Settings", '#' %></li> <li class="divider"></li> <li> <%= link_to "Log out", logout_path, method: "delete" %> </li> </ul> </li> <% else %> <li><%= link_to "Log in", login_path %></li> <% end %> </ul> </nav> session_helper.rb: module SessionsHelper # Logs in the given user. def log_in(user) session[:user_id] = user.id end def remember(user) cookies.permanent.signed[:user_id] = user.id cookies.permanent[:remember_token] = user.remember_token end def current_user if (user_id = session[:user_id]) @current_user ||= User.find_by(id: user_id) elsif (user_id = cookies.signed[:user_id]) user = User.find_by(id: user_id) if user && user.authenticated?(:remember, cookies[:remember_token]) log_in user @current_user = user end end end def logged_in? !current_user.nil? end def forget(user) user.forget cookies.delete(:user_id) cookies.delete(:remember_token) end # Logs out the current user. def log_out session.delete(:user_id) @current_user = nil end end users_controller.rb: class UsersController < ApplicationController before_action :correct_user, only: [:edit, :update] def index @users = User.all end def show @user = User.find(params[:id]) @microposts = @user.microposts(page: params[:page]) end def new @user = User.new end def create @user = User.new(user_params) if @user.save flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end def edit @user = User.find(params[:id]) end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end end I guess logged_in doesn't work, but I could not find a mistake. When I defining Log out method, the button doesn't change from 'Log in' |
Delayed Job debug on production logs Posted: 06 Aug 2016 03:29 AM PDT I'm setting up background tasks with Rails 5 and DelayedJob for simple email notifications. Looking into my production logs and it's coming up with the following item: [2016-08-05T20:22:15.094172 #10616] DEBUG -- : Delayed::Backend::ActiveRecord::Job Load (1.0ms) UPDATE "delayed_jobs" SET locked_at = '2016-08-06 10:22:15.092656', locked_by = 'delayed_job host:jiably pid:10616' WHERE id IN (SELECT "delayed_jobs"."id" FROM "delayed_jobs" WHERE ((run_at <= '2016-08-06 10:22:15.091986' AND (locked_at IS NULL OR locked_at < '2016-08-06 06:22:15.092073') OR locked_by = 'delayed_job host:vps-server-name pid:10616') AND failed_at IS NULL) ORDER BY priority ASC, run_at ASC LIMIT 1 FOR UPDATE) RETURNING * There is no jobs in the queue and when I test my app's email notification the job seems to be processed fine. Is this normal? It seems to be repeating frequently and adding to the log file. I do have logrotate setup but unsure if it's a configuration I did or miss to cause this. Thanks for shedding some light into this. |
Slow request ruby on rails/mongoid Posted: 06 Aug 2016 03:17 AM PDT How can I optimize this querie with many records ? (50k records) Username.all.group_by.map(&:country).uniq.compact.map(&:capitalize).sort The logs show me things I do not understand, can you explain these logs, thank you. My log: MONGODB | localhost:27017 | usernames_development.killCursors | STARTED | {"killCursors"=>"usernames", "cursors"=>[168847037365]} MONGODB | localhost:27017 | usernames_development.killCursors | SUCCEEDED | 0.000130491s MONGODB | localhost:27017 | usernames_development.find | STARTED | {"find"=>"usernames", "filter"=>{}, "sort"=>{"created_at"=>-1}} MONGODB | localhost:27017 | usernames_development.find | SUCCEEDED | 0.008368436s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.393699925s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.47113242699999996s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.37317152600000003s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.30737007099999997s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.27134638s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.450067092s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.43426460699999997s MONGODB | localhost:27017 | usernames_development.getMore | STARTED | {"getMore"=>170143577253, "batchSize"=>0, "collection"=>"usernames"} MONGODB | localhost:27017 | usernames_development.getMore | SUCCEEDED | 0.059751312s |
How to use bootstrap modal with rails? Posted: 06 Aug 2016 03:41 AM PDT I would like to implement a boostrap modal in my rails application. The button is displaying but when I click on it nothing appears : / Trigger the modal with a button %button.btn.btn-info.btn-lg{"data-target" => "#myModal", "data-toggle" => "modal", :type => "button"} Open Modal / Modal #myModal.modal.fade{:role => "dialog"} .modal-dialog / Modal content .modal-content .modal-header %button.close{"data-dismiss" => "modal", :type => "button"} × %h4.modal-title Modal Header .modal-body %p Some text in the modal. .modal-footer %button.btn.btn-default{"data-dismiss" => "modal", :type => "button"} Close I think I miss something. Those are my gems installed : source 'https://rubygems.org' gem 'bootstrap-sass', '~> 3.3.6' gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git' gem 'haml', '~> 4.0', '>= 4.0.7' gem 'foundation' gem 'rails', '4.2.6' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.1.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 2.0' ... my application layout : !!! %html %head %title HealthyBox = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true = javascript_include_tag 'application', 'data-turbolinks-track' => true = csrf_meta_tags application.css.scss @import "bootstrap-sprockets"; @import "bootstrap"; |
Rails - Trying to pass param via AJAX to Devise sessions#new action to pre-populate email attribute Posted: 06 Aug 2016 02:23 AM PDT I have this line in my javascript: $.ajax({url: '/users/sessions/new?user_email=' + userEmail}); and I have this code in ~/app/controllers/users/sessions_controller.rb: def new @user_email = params[:user_email] super end and in ~/config/initializers/devise.rb I have: config.scoped_views = true and I have this in ~/app/views/users/sessions/new.html.haml: %h2 Sign In .sign-in-form-wrapper = simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| .form-inputs = f.input :email, required: true, value: @user_email, readonly: true = f.input :password, required: true, autofocus: true = f.input :remember_me, as: :boolean if devise_mapping.rememberable? .form-actions = f.button :submit, "Sign In" = render 'users/shared/links' If I output the value of @user_email in the new action, I get the correct value that I'm passing. But within the new action, after @user_email is assigned the value and it hits super , nothing happens. The sign in form isn't rendered and I can't figure out why. I'm seeing this in the log: INFO -- : Started GET "/users/sign_in?user_email=b.wayne@gotham.com" for 127.0.0.1 at 2016-08-06 02:13:58 -0700 INFO -- : Processing by Users::SessionsController#new as */* INFO -- : Parameters: {"user_email"=>"b.wayne@gotham.com"} INFO -- : Rendered users/shared/_links.html.haml (0.5ms) INFO -- : Rendered users/sessions/new.html.haml within layouts/application (14.5ms) Is there something about it being processed as */* ? Should it be html ? And if so, why would it not be? Thanks in advance for any insight that can be provided. |
Is there any way in Phoenix generate only controller, without a model? Posted: 06 Aug 2016 04:38 AM PDT Is there any way in Phoenix generate only controller, without a model? In rails I'd do that by rails g controller Home index about |
Ruby on Rails image_tag not displaying the image Carrierwave Posted: 06 Aug 2016 01:26 AM PDT Im trying to get a profile picture to display for users to with no luck. Ive installed the carrierwave gem in the gemfile along with mini_magick . Image uploader class AvatarUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick storage :file def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end version :thumb do process :resize_to_fit => [80, 80] end def extension_white_list %w(jpg jpeg gif png) end user.rb class User < ActiveRecord::Base ... mount_uploader :avatar, AvatarUploader end show.html.erb ... <%= image_tag @user.avatar.url(:thumb) if @user.avatar?%> <%= link_to "Edit", edit_user_path(@user)%> controller (the alart is being displayed, instead of the image) def update if @user.update(user_params) redirect_to @user else flash.now[:alert] = "Something went wrong. Please try again." render :edit end end private def check_authorization unless current_user.id == params[:id].to_i redirect_to root_url end end def set_user @user = User.find(params[:id]) end def user_params params.require(:user).permit(:username, :avatar) end end |
Why is capistrano asset precompile throwing ci/reporter/rake/rspec error? Posted: 06 Aug 2016 04:39 AM PDT Capistrano deploy is throwing following error, on assets precompile step. rake aborted! LoadError: cannot load such file -- ci/reporter/rake/rspec /home/ubuntu/github/my_app/releases/20160806071639/Rakefile:4:in `require' /home/ubuntu/github/my_app/releases/20160806071639/Rakefile:4:in `<top (required)>' /home/ubuntu/github/my_app/shared/bundle/ruby/2.0.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/bin/bundle:23:in `load' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/bin/bundle:23:in `<main>' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in `eval' /home/ubuntu/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in `<main>' (See full trace by running task with --trace) I ran following command, cap staging deploy So, I tried running ~/.rvm/bin/rvm default do bundle exec rake assets:precompile in release folder to check if I've missed something in my deployment file. But it failed there as well. But when I ran same command, ~/.rvm/bin/rvm default do bundle exec rake assets:precompile from my deployment directory (the place from which I'm running cap deploy command), it runs successfully. Following are the environment settings, Ruby version = 2.0.0 Rails version = 3.2.21 Capistrano version = 3.4.0 My deploy.rb file (I've changed github url), # config valid only for current version of Capistrano lock '3.4.0' set :application, 'my_app' set :repo_url, 'git@github.com:xyz/my_app.git' set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master" set :deploy_root, "/home/ubuntu/github/" set :deploy_to, "#{fetch(:deploy_root)}my_app" set :bundle_flags, "--local --no-cache" set :scm_passphrase, "" set :use_sudo, false set :ssh_options, {:forward_agent => true} set :copy_exclude, [ '.git' ] set :rails_env, "staging" set :keep_releases, 1 set :run_bundle_install, true set :run_precompile, true set :run_db_migrate, false set :run_rspec, false set :stage, :production server "127.0.0.1", user: "ubuntu", roles: %w{web app} set :linked_files, %w{config/database.yml config/database.yml} # Default branch is :master # ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp # Default deploy_to directory is /var/www/my_app_name # set :deploy_to, '/var/www/my_app_name' # Default value for :scm is :git # set :scm, :git # Default value for :format is :pretty # set :format, :pretty # Default value for :log_level is :debug # set :log_level, :debug # Default value for :pty is false set :pty, true # Default value for :linked_files is [] # set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml') # Default value for linked_dirs is [] # set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system') # Default value for default_env is {} # set :default_env, { path: "/opt/ruby/bin:$PATH" } # Default value for keep_releases is 5 # set :keep_releases, 5 namespace :deploy do desc "Restart sidekiq" task :restart_sidekiq do run <<-CMD cd fetch(current_path) && script/sidekiq restart -e #{rails_env} CMD end desc "Restart application" task :restart do on roles(:app), in: :sequence, wait: 5 do execute :touch, release_path.join("tmp/restart.txt") end end after :restart, :clear_cache do on roles(:web), in: :groups, limit: 3, wait: 10 do # Here we can do anything such as: # within release_path do # execute :rake, 'cache:clear' # end end end #after :finishing, "deploy:restart" after :finishing, "deploy:restart_sidekiq" after :finishing, "deploy:cleanup" end My Capfile, # Load DSL and set up stages require 'capistrano/setup' # Include default deployment tasks require 'capistrano/deploy' # Include tasks from other gems included in your Gemfile # # For documentation on these, see for example: # # https://github.com/capistrano/rvm # https://github.com/capistrano/rbenv # https://github.com/capistrano/chruby # https://github.com/capistrano/bundler # https://github.com/capistrano/rails # https://github.com/capistrano/passenger # require 'capistrano/rvm' # require 'capistrano/rbenv' # require 'capistrano/chruby' require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' # require 'capistrano/passenger' # Load custom tasks from `lib/capistrano/tasks` if you have any defined Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } Gemfile snapshot, gem 'capistrano', '3.4.0' gem 'sshkit', '1.11.2' gem 'net-scp', '1.2.1' gem 'capistrano-rails', '1.1.7' gem 'capistrano-bundler', '1.1.4' gem 'capistrano-rvm', '0.1.2' Do let me know, if you need more info. |
Searchkick aggregations Posted: 06 Aug 2016 12:43 AM PDT I have the following two aggregations in Rails using searchkick gem. When I used any of them separately, it works like a charm. But I want to "merge" them together and cannot figure out how to do it :(. Aggregation 1: aggs: { price_vat: { ranges: [{to: 5}, {from: 5, to: 10}, {from: 10}] } } Aggregation 2: aggs: ['property_options.property_id', 'property_options.id', 'property_options.value_cs', 'property_options.value_en', 'manufacturer_id'] Is there any expert on this who knows how to do it? Really appreciated. Thank you and have a good weekend, Miro. |
facebook could not retrieve data from url in rails app Posted: 06 Aug 2016 12:25 AM PDT I am working on this rails web application where I have implemented facebook link share for some of the urls. It seems to be working fine for most of them like eg: travelibro.com, travelibro.com/destinations, travelibro.com/destinations/germany, travelibro.com/blog/interview-with-a-wanderer-steph-parker, travelibro.com/monish-shah/journeys/bangalore-day-trip-2149. It is not working for itineraries. eg: travelibro.com/editor/itineraries/weekend-in-bath-955. If you go to above url and check head section it shows all og tags as required. I have a partial named _fb_og_tags.html.haml like this %meta{:content => "#{FbAppId}", :property => "fb:app_id"}/ -if local_assigns[:name].present? %meta{:content => name , :property => "og:name"}/ -if local_assigns[:name].present? %meta{:content => image, :property => "og:image"}/ %meta{:content => "website", :property => "og:type"}/ %meta{:content => title, :property => "og:title"} %meta{:content => url, :property => "og:url"}/ -if local_assigns[:description].present? .present? %meta{:content => description, :property => "og:description"}/ In my view (itineraries/show.html.haml) I have this - content_for :og_meta_tags do = render :partial => "home/fb_og_tags", :locals => {:name => "#{@itinerary.name}", :image => "http://d12bbk7gyz1ay8.cloudfront.net/gallery_pics/original/2154/banner-3.png", :title=> "Travelibro - Itinerary: #{@itinerary.name}", :url => destinations_url, :description => "Whether a luxury traveller, a backpacker, a budget-traveller, or any other kind, TraveLibro features thousands of unique itineraries created by our editor, travel agents and users providing great insights for your next vacation."} In application.html.haml I have this <!doctype html> %html %head{:prefix => "og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# website: http://ogp.me/ns/website#"} %link{:href => "/assets/TL-fevicon-271315ee4f437bb5941e8dab25977e9f.ico", :rel => "shortcut icon", :type => "image/vnd.microsoft.icon"} %link{:href => "http://travelibro.com", :rel => "canonical"} %meta{:content => "app-id=########", :name => "apple-itunes-app"} %meta{:content => "app-id=com.google.android.youtube", :name => "google-play-app"} %link{:href => "apple-touch-icon.png", :rel => "apple-touch-icon"}/ = yield :og_meta_tags = render :partial => "layouts/metatag" Can someone help me with this please. |
Multiple file input not working on mobile Posted: 05 Aug 2016 11:29 PM PDT My multiple image upload works fine on desktop but no luck with mobile. When choosing images to upload on mobile it doesn't allow me to choose multiple files. I've tried with multiple devices and it works on none of them. <div class="form-group"> <label>Select photos</label> <%= file_field_tag :file, multiple: true %> </div> |
carrierwave "url" always returns a physical path Posted: 06 Aug 2016 03:42 AM PDT I'm using and I wonder, why does it return an physical path instead of url? @picture.image.url # or @picture.image_url # or @picture.image.path all return a physical full path in my file system instead of a url. And on a page the image isn't displayed for this reason. |
how to do different alerts in different sides of the channel in private_pub? Posted: 06 Aug 2016 12:03 AM PDT I am trying to write a chat application in rails using private_pub gem, I have set up almost everything, but I have a problem showing different things in different sides, the simplified question is this: I want to show 2 different alerts in each side, that is for example when the form is submitted, in the sender side I want to have alert("x"); and in the recipient side I want to have alert("y"); But I always get this I am also using devise gem, but when I use current_user.id to make a decision it always returns the user who submitted the form. it seems a same thing is always happening in both sides, I wonder how can I do different things there? |
Where does hamburger jquery button code go in Rails 4? Posted: 05 Aug 2016 10:20 PM PDT I have a hamburger button that causes a sidebar to appear and disappear. I'm using rails and jquery. I have included the gem jquery-rails. application.js //= require jquery //= require jquery_ujs //= require turbolinks //= require_tree . When I include the code directly in my view as: <script> $('.hamburger').on('click', function () { if ($('.sidebar').hasClass('open')) { $('.sidebar').removeClass('open'); } else { $('.sidebar').addClass('open'); }}); </script> it works! When I take away the script and put it in my xxx.coffee file which I've renamed to xxx.js, it doesn't work. I looked at the webpage code and have <script src="/assets/xxx.self-numbersandletters.js?body=1" data-turbolinks-track="true"></script> I can't put together where I'm making a mistake. I think it might be that it comes together like: <script $('.hamburger').on('click', function () { if ($('.sidebar').hasClass('open')) { $('.sidebar').removeClass('open'); } else { $('.sidebar').addClass('open'); }});> </script> but I guess I have to read more about the asset pipeline or convert it to coffee script. |
Rails How to get Posts ordered by number of Comments - not to return Active Record Posted: 05 Aug 2016 10:09 PM PDT How can I get the posts sorted by the number of comments? (most comments first) posts has_many comments comments belong_to posts |
Get id for nested MVC? Posted: 06 Aug 2016 12:25 AM PDT <%= @dueler.id %> is showing the wrong integer. For example, duel has two duelers 34 and 35 . The current_user is 78 , which dueler 34 is user 78 . By making sure the current_user is a dueler of the duel then he will be empowered to update his dueler status. duels/show Duel <%= @duel.id %> = 20 Dueler <%= @dueler.id %> = 10 # How to get 34 instead? User <%= current_user.id %> = 78 rails c pry(main)> Dueler.last id: 34, # This is what it should say since 78 is the current_user user_id: 78, challenge_id: 295, duel_id: 20, accept: nil> pry(main)> Dueler.last id: 35, user_id: 150, challenge_id: 290, duel_id: 20, accept: nil> pry(main)> Duel.last id: 20, consequence: "TEST", reward: "TEST", created_at: Fri, 05 Aug 2016 21:38:31 EDT -04:00, updated_at: Fri, 05 Aug 2016 21:38:31 EDT -04:00> duels_controller def show @duel = Duel.find(params[:id]) @duelers = Duel.duelers.find(params[:id]) # What should this be instead? end I thought maybe something like this would work: def show @duel = Duel.find(params[:id]) @duelers = Duel.duelers.find(params[:id]) # Get hash of duelers i.e. 34 & 35 @dueler = @duelers.find(current_user = Dueler.user_id) # If current_user is equal to the user_id of dueler then that dueler is thee dueler, i.e. 34 end |
rake aborted! NoMethodError: undefined method `[]' for nil:NilClass (rails5+heroku) Posted: 05 Aug 2016 11:06 PM PDT I'm trying to compile my assets locally so i can make sure there is no issues when pushing to heroku. Locally my view looks perfect, but on heroku its not loading all my assets. When I run RAILS_ENV=production bundle exec rake assets:precompile I get the below error, any ideas? rake aborted! NoMethodError: undefined method `[]' for nil:NilClass /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/sass_processor.rb:278:in `sprockets_context' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/sass_processor.rb:124:in `asset_path' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/sass_processor.rb:137:in `asset_url' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/sass_processor.rb:209:in `font_url' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/script/tree/funcall.rb:143:in `_perform' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/script/tree/node.rb:58:in `perform' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:397:in `visit_prop' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/base.rb:36:in `visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:160:in `block in visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/stack.rb:79:in `block in with_base' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/stack.rb:115:in `with_frame' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/stack.rb:79:in `with_base' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:160:in `visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:504:in `block (2 levels) in visit_directive' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:504:in `map' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:504:in `block in visit_directive' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:181:in `with_environment' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:503:in `visit_directive' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/base.rb:36:in `visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:160:in `block in visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/stack.rb:79:in `block in with_base' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/stack.rb:115:in `with_frame' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/stack.rb:79:in `with_base' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:160:in `visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/base.rb:52:in `block in visit_children' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/base.rb:52:in `map' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/base.rb:52:in `visit_children' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:169:in `block in visit_children' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:181:in `with_environment' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:168:in `visit_children' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/base.rb:36:in `block in visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:188:in `visit_root' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/base.rb:36:in `visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:159:in `visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/visitors/perform.rb:8:in `visit' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/root_node.rb:36:in `css_tree' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/tree/root_node.rb:20:in `render' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sass-3.4.22/lib/sass/engine.rb:281:in `render' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/sass_compressor.rb:48:in `call' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/sass_compressor.rb:28:in `call' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:75:in `call_processor' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:57:in `block in call_processors' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:56:in `reverse_each' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:56:in `call_processors' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/loader.rb:134:in `load_from_unloaded' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/loader.rb:60:in `block in load' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/loader.rb:44:in `load' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/cached_environment.rb:20:in `block in initialize' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/cached_environment.rb:47:in `load' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/base.rb:66:in `find_asset' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/base.rb:73:in `find_all_linked_assets' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/manifest.rb:142:in `block in find' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:228:in `block in stat_tree' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:212:in `block in stat_directory' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:209:in `each' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:209:in `stat_directory' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:227:in `stat_tree' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:105:in `each' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:105:in `block in logical_paths' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:104:in `each' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:104:in `logical_paths' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/manifest.rb:140:in `find' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/sprockets/manifest.rb:185:in `compile' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-rails-3.1.1/lib/sprockets/rails/task.rb:68:in `block (3 levels) in define' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-3.7.0/lib/rake/sprocketstask.rb:147:in `with_logger' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/gems/sprockets-rails-3.1.1/lib/sprockets/rails/task.rb:67:in `block (2 levels) in define' /Users/admin/.rvm/gems/ruby-2.3.1@global/gems/rake-11.2.2/exe/rake:27:in `<top (required)>' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/bin/ruby_executable_hooks:15:in `eval' /Users/admin/.rvm/gems/ruby-2.3.1@ryansportfolio/bin/ruby_executable_hooks:15:in `<main>' Tasks: TOP => assets:precompile (See full trace by running task with --trace) |
ActiveModel::ForbiddenAttributesError When Creating New `Task` Posted: 05 Aug 2016 09:22 PM PDT I am getting an ActiveModel::ForbiddenAttributesError when trying to create a new task . I think I have eliminated all of the stupid errors, so I would love help figuring it out. My tasks_controller is as follows: class TasksController < ApplicationController def index @tasks = current_user.Task.all end def create @task = Task.new(params[:task]) <<<<<<<<<<<THIS LINE @task.user = current_user if @task.save flash[:notice] = "Your exercise results were saved!" redirect_to tasks_path else render :action => 'new' end end def new @task = Task.new end def edit @task = Task.find(params[:id]) end def update @task = Task.find(params[:id]) if @task.update_attributes(params[:task]) <<<<<<<<<<<THIS LINE redirect_to tasks_path end def destroy @task = Task.find(params[:id]) @tasks.destroy redirect_to tasks_path end def show @task = Task.find(params[:id]) end private def task_params params.require(:task).permit(:name, :reps, :weight, :comments, :user_id) end end end On the two indicated lines I have tried both params[:task] and 'task_params'. The first throws up the ForbiddenAttributesError ; the latter throws up undefined local variable or method task_params' for #`. Here's my tasks#new erb: <%= form_for @task do |f| %> <div class="col-xs-12"> <%= f.label "Exercise Name" %> <%= f.text_field :name, class: "form-control" %> </div> <div class="col-xs-6"> <%= f.label "Reps" %> <%= f.number_field :reps, class: "form-control" %> </div> <div class="col-xs-6"> <%= f.label "Weight" %> <%= f.text_field :weight, class: "form-control" %> </div> <div class="col-xs-12"> <%= f.label "Comments/Notes" %> <%= f.text_field :comments, class: "form-control" %> </div> <div class="text-center"><%= f.submit "Record Exercise", class: "btn-primary" %></div> <% end %> Finally, my schema for tasks is as follows, so I know the names of the variables are right: create_table "tasks", force: :cascade do |t| t.string "name" t.integer "user_id" t.integer "reps" t.integer "weight" t.text "comments" t.datetime "created_at", null: false t.datetime "updated_at", null: false end Can anyone guide me in fixing this "the Ruby way"? |
No comments:
Post a Comment