vendredi 8 mai 2015

What does this code, that looks like JavaScript object do?

@options = { query: {site: service, page: page} }

What does this do? In javascript this, I think would be object definition, however I cannot find what exactly it is in ruby. This would be the full code:

class StackExchange
  include HTTParty
  base_uri 'api.stackexchange.com'

  def initialize(service, page)
    @options = { query: {site: service, page: page} }
  end

  def questions
    self.class.get("/2.2/questions", @options)
  end

  def users
    self.class.get("/2.2/users", @options)
  end
end

Rails delete functionality is not working

I am completely new to Ruby On Rails, and going through this guide to build a basic application.

When I am trying to implement the delete functionality as mentioned in the document, I am seeing the show page.

I have below method in my controller:

def destroy
  @article = Article.find(params[:id])
  @article.destroy

  redirect_to articles_path
end

and below line in my page:

<td><%= link_to 'Destroy', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>

Now when I click on the link, I am seeing below URL in browser:

http://localhost:3000/articles/1

Now now in this case I am seeing the show screen instead of getting an alert message and then deleting the record from my page.

I have followed this SO post - Rails 4 link_to Destroy not working in Getting Started tutorial

and verified that

//= require jquery
//= require jquery_ujs

elements are there in my application.js

Please tell me where I am doing mistake?

ActiveRecord 'where' clause using function

I am trying to apply filters on an ActiveRecord::Relation. I would like to apply a 'where' clause (or the equivalent) to make the following call.

record = Record.all
record.where(record.remaing_units > 5)

I know this would be easy to get coding a fonction and passing it 2 arguments but I would like to know if there is a cleaner way to do it, more 'where' like.

Cucumber: How to implement feature directory specific support/hooks.rb functionality?

We're using Cucumber as our test harness

We have a folder structure like this:

- automation
    - api
        - api1.feature
        - api2.feature
    - gui
        - gui1.feature
        - gui2.feature
    - step_definitions
        - api_steps.rb
        - gui.steps.rb
    - support
        - hooks.rb
    - cucumber.yml
    - env.rb
    - Gemfile
    - Rakefile

I need to have different actions occurring in my hooks.rb file (or support directory in general) for the api tests vs. the gui tests. For my api tests, I need to authorize through our restapi and get an auth cookie. For my gui tests, I need create a selenium browser instance.

I need this to work both when I execute everything by issuing 'cucumber' alone from the 'automation' level of this folder structure AND when I execute a single feature file by doing:

    $ cucumber gui/gui1.feature -r features

So my questions are regarding how best to do that.

  1. Can I/should I create some kind of conditional within my hooks.rb file in the 'Before do' block to perform different before actions based on which directory the feature is executing from?
  2. OR Can I/should I create separate support/hooks.rb directories/files within each of the directories 'api' and 'gui'? (effectively will Cucumber recognize and selectively utilize multiple support directories?)

Thanks!

How to extract bike listings using information from 3taps API

Developing a Ruby on Rails app using information from the 3 taps API. The information I want is from Craigslist. Want to extract information on bikes for sale in the UK.

the following gives me the category information for bikes for sale

{
            "code": "SBIK",
            "group_code": "SSSS",
            "group_name": "For Sale",
            "name": "Bicycles"
        },

so in my browser i add the following

http://ift.tt/1JUkbYx

{
"success": true,
"anchor": 2110058535,
"postings": [
{
"id": 2109916051,
"source": "CRAIG",
"category": "SBIK",
"external_id": "5010763041",
"external_url": "http://ift.tt/1EV9Yqu",
"heading": "WTB:  Tri-bike...56-58cm",
"timestamp": 1430838700,
"annotations": {
"source_map_yahoo": "http://ift.tt/1JUka6W",
"source_continent": "USA",
"latlong_source": "In Posting",
"source_heading": "WTB:  Tri-bike...56-58cm",
"source_cat": "sss",
"proxy_ip": "104.236.116.193:22225",
"source_state": "North Carolina",
"phone": "5010763041",
"source_account": "5hz2n-5010763041@sale.craigslist.org",
"gzip_data_size": "4170",
"source_loc": "wilmington",
"source_subcat": "bia|bik",
"html_data_size": "12631",
"source_map_google": "http://ift.tt/1EV9Yqw"
},

but when i add the country code -which is GBR i get nothing i have added http://ift.tt/1JUkbYz

but get error {"success":false,"error":"level params not supported in polling"}

when i do a location search it works using the metro field

http://ift.tt/1EV9Yqy

"locations": [
{
"bounds_max_lat": 57.21864,
"bounds_max_long": -2.06182,
"bounds_min_lat": 57.10158,
"bounds_min_long": -2.22161,
"code": "GBR-ABD",
"full_name": "Aberdeen, United Kingdom",
"short_name": "Aberdeen"
},
{
"bounds_max_lat": null,
"bounds_max_long": null,
"bounds_min_lat": null,
"bounds_min_long": null,
"code": "GBR-BAT",
"full_name": "Bath, United Kingdom",
"short_name": "Bath"
},

but when i add this to my polling search , i cannot get this to return any values for bikes for sale in the uk?

http://ift.tt/1JUka6Y

also used &location.city=GBR-MAN to find bikes in manchester which returns error

i get {"success":false,"error":"level, country params not supported in polling"}.

ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`

I am trying to implement a "comment" feature as part of my assignment for an project I am building.

Earlier in the course we created a comment table and had the Faker gem generate fake comments.

My instructions are as follows:

Comments must be associated with users, so add a user_id foreign key to the comments table. Remember to add an index to it too;

Update the User model so you can call user.comments, and the Comment model so you can call comment.user;

Modify the seeds.rb file to create valid comments when you run db:reset;

Initially I tried to run my rails generate commands but kept running into this error:

▶ rake db:migrate
== 20150508143445 CreateComments: migrating ===================================
-- create_table(:comments)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "comments" already exists: CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "comments" already exists: CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
/Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
SQLite3::SQLException: table "comments" already exists
/Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

So I discovered that I needed to delete the old table from the start of the course. I did this in the console.

ActiveRecord::Base.connection.execute("drop table comments")

It seemed to work. I was then able to run these in terminal.

▶ rake db:migrate
== 20150508143445 CreateComments: migrating   ===================================
-- create_table(:comments)
   -> 0.0015s
== 20150508143445 CreateComments: migrated (0.0016s) ==========================

== 20150508152354 DropComments: migrating =====================================
== 20150508152354 DropComments: migrated (0.0000s) ============================

▶ rails g migration AddCommentToUsers commed_id:integer:index
  invoke  active_record
  create    db/migrate/20150508155200_add_comment_to_users.rb

▶ rake db:migrate
== 20150508155200 AddCommentToUsers: migrating ================================
-- add_column(:users, :commed_id, :integer)
   -> 0.0010s
-- add_index(:users, :commed_id)
   -> 0.0012s
== 20150508155200 AddCommentToUsers: migrated (0.0023s) =======================

models/comment.rb

class Comment < ActiveRecord::Base
  belongs_to :user
end

models/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
  has_many :posts
  has_many :comments

  # CarrierWave method for attribute functionality
  mount_uploader :avatar, AvatarUploader

  # These methods check the role of a user in the database
  def admin?
    role == 'admin'
  end

  def moderator?
    role == 'moderator'
  end
end

migrations/create_comments.rb

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :description

      t.timestamps null: false
    end
  end
end

migrations/add_comment_to_users.rb

class AddCommentToUsers < ActiveRecord::Migration
  def change
    add_column :users, :commed_id, :integer
    add_index :users, :commed_id
  end
end

In my seeds.rb I have changed

# Create Comments
50.times do 
  Comment.create!(
      # user: users.sample, # we have not yet associated Users with Comments
    post: posts.sample,
    description: Faker::Lorem.paragraph
    )
end

to

# Create Comments
50.times do 
  Comment.create!(
    user: users.sample,
    description: Faker::Lorem.paragraph
    )
end

Now when I run rake db:reset I get this error.

▶ rake db:reset
-- create_table("advertisements", {:force=>:cascade})
   -> 0.0047s
-- create_table("answers", {:force=>:cascade})
   -> 0.0010s
-- add_index("answers", ["question_id"], {:name=>"index_answers_on_question_id"})
   -> 0.0014s
-- create_table("comments", {:force=>:cascade})
   -> 0.0010s
-- create_table("posts", {:force=>:cascade})
   -> 0.0010s
-- add_index("posts", ["topic_id"], {:name=>"index_posts_on_topic_id"})
   -> 0.0010s
-- add_index("posts", ["user_id"], {:name=>"index_posts_on_user_id"})
   -> 0.0014s
-- create_table("questions", {:force=>:cascade})
   -> 0.0010s
-- create_table("summaries", {:force=>:cascade})
   -> 0.0010s
-- add_index("summaries", ["post_id"], {:name=>"index_summaries_on_post_id"})
   -> 0.0010s
-- create_table("topics", {:force=>:cascade})
   -> 0.0036s
-- create_table("users", {:force=>:cascade})
   -> 0.0015s
-- add_index("users", ["commed_id"], {:name=>"index_users_on_commed_id"})
   -> 0.0009s
-- add_index("users", ["comment_id"], {:name=>"index_users_on_comment_id"})
   -> 0.0012s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
   -> 0.0015s
-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true})
   -> 0.0017s
-- initialize_schema_migrations_table()
   -> 0.0034s
rake aborted!
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
/Users/jon/code/Bloccit/db/seeds.rb:45:in `block in <top (required)>'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `<top (required)>'
-e:1:in `<main>'
Tasks: TOP => db:setup => db:seed
(See full trace by running task with --trace)

Here is the full stack trace:

ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute.rb:138:in `with_value_from_database'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb:39:in `write_from_user'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:74:in `write_attribute_with_type_cast'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:56:in `write_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:96:in `write_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:373:in `[]='
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:83:in `replace_keys'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/singular_association.rb:17:in `writer'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:123:in `user='
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:559:in `init_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:281:in `initialize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/persistence.rb:50:in `create!'
/Users/jon/code/Bloccit/db/seeds.rb:45:in `block in <top (required)>'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `<top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/engine.rb:547:in `load_seed'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/jon/code/Bloccit/bin/rake:8:in `<top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
-e:1:in `<main>'
Tasks: TOP => db:setup => db:seed

I am stumped here. I thought by deleting the old table and running the new generate commands + migrating everything would be in order. Apparently this is not the case.

What am I doing wrong here?

edited to include line 45 of my seeds.rb

Comment.create!(

Get path to capistrano shared path from ruby

I know that I can get the location of my rails app with Rails.root:

> Rails.root
 => #<Pathname:/var/www/app-name/releases/20150507181426>

I am looking for the function to call to get capistrano's shared folder, which in this case is found here:

/var/www/app-name/shared/

I need to be able to get the path from within ruby code. Thanks in advance.