I have created a mechanize task in rails. it is very simple
task :estimateone => :environment do
require 'mechanize'
mechanize = Mechanize.new
page = mechanize.get('https://www.theurbanlist.com/brisbane/a-list/50-brisbane-cafes-you-should-have-eaten-breakfast-at')
page.css('ol li a').each do |link|
link.click
end
end
but I get this error, what am I doing wrong?
rake aborted!
NoMethodError: undefined method `click' for #<Nokogiri::XML::Element:0x00007ffb4b281830>
/Users/jeremybray/RubymineProjects/OpportunityFinder/lib/tasks/getlead.rake:8:in `block (2 levels) in <top (required)>'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:204:in `block in each'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:203:in `upto'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:203:in `each'
/Users/jeremybray/RubymineProjects/OpportunityFinder/lib/tasks/getlead.rake:7:in `block in <top (required)>'
/Users/jeremybray/.rvm/gems/ruby-2.4.2#global/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `eval'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `<main>'
As click is a method of Mechanize, you should use your instance of it as the receiver of the click method, not the link, that's a Nokogiri one.
As the doc states, it receives the element, clicks it and returns the fetched page.
Try with:
-- snip --
page.css('ol li a').each do |link|
mechanize.click(link)
end
Page#links_with will return links, css will return html elements:
page.links_with(css: 'ol li a').each do |link|
link.click
end
Related
I am trying to test my application and I continually get the following error:
Error:
ContractsControllerTest#test_should_get_show:
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError
Error:
ContractsControllerTest#test_should_get_show:
NoMethodError: undefined method `each' for nil:NilClass
Below is the code on my contract controller and I have similar code like this on all the controllers.
def index
#contract = Contract.all.paginate(page: params[:page], :per_page => 70)
end
def show
#contract = Contract.find(params[:id])
end
def new
#contract = Contract.new
end
def create
#contract = Contract.new(located)
if #contract.save
flash[:success] = "A record has been successfully added"
redirect_to contracts_path
else
render 'new'
end
end
def located
params.require(:contract).permit(:contract_name, :contract_status, :services_rendered, :contract_value, :award_year)
end
# editing a record in the contract from cotract table
def edit
#contract = Contract.find(params[:id])
end
def update
#contract = Contract.find(params[:id])
if #contract.update_attributes(located)
flash[:success] = "Contract form updated"
redirect_to contracts_path
else
render'edit'
end
end
def destroy
Contract.find(params[:id]).destroy
flash[:success] = "A record has been successfully deleted"
redirect_to contracts_path
end
Below is the full trace of the error when I run the command rails db:fixtures:load --trace
rails aborted!
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:72:in `validate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:49:in `raw_rows'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:37:in `config_row'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:27:in `model_class'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:791:in `block (2 levels) in
read_fixture_files'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixture_set/file.rb:15:in `open'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:790:in `block in read_fixture_files'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:789:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:789:in `each_with_object'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:789:in `read_fixture_files'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:598:in `initialize'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:529:in `new'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:529:in `block (2 levels) in
create_fixtures'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:526:in `map'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:526:in `block in create_fixtures'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/connection_adapters/postgresql/referential_integrity.rb: 22:in `disable_referential_integrity'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/fixtures.rb:523:in `create_fixtures'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-
5.0.5/lib/active_record/railties/databases.rake:207:in `block (3 levels) in
<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-
5.0.5/lib/rails/commands.rb:18:in `<top (required)>'
/home/ubuntu/workspace/final_project/bin/rails:9:in `require'
/home/ubuntu/workspace/final_project/bin/rails:9:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/client/rails.rb:28:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/client/rails.rb:28:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/client/command.rb:7:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in
`run'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.2/bin/spring:49:in `<top
(required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/binstub.rb:31:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-
2.0.2/lib/spring/binstub.rb:31:in`<top (required)>'
/home/ubuntu/workspace/final_project/bin/spring:15:in `require'
/home/ubuntu/workspace/final_project/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:fixtures:load
(See full trace by running task with --trace)
My application containts 12 controllers, the same error occurs in each of the controllers. Also, I have 10 tables in my schema some of which contain references / relations to each other.
I don't really know what is wrong and I have be struggling with this problem for about four days now. I would be grateful if anyone could help me with any code or information that could help me to trace the source of the error. I would also be grateful if you could provide information for exactly where I should place the code to trace and solve this error.
Thanks
One of your fixture files in test/fixtures has bad formatting. The problem does not appear to be with your controllers or tests since it is happening for every single one of them. All of the fixtures get loaded into the test database before every single test, even if some are not used for the current test. So if there is an error in one of these files it will get raised for every test.
According to the line from the activerecord source that is throwing the error at the top of the stack trace you've provided, ActiveRecord is expecting every entry in your YAML fixture file to be a key which references a hash.
Each entry in a fixture should represent a model instance. The top level key is used as a name to reference that instance (rails usually names them one and two when generating the fixture template for a new model). Here is an example YAML fixture with one good entry and a few different types of bad ones.
# This will produce a hash associated to key :hash_entry.
# This is the correct type of entry, all others that follow are incorrect for rails fixtures.
hash_entry:
key1: value1
key2: value2
# This will produce the string "not a hash" associated to key :string_entry
string_entry: not a hash
# This will produce the array ["also", "not","a","hash"] associated to key :array_entry
array_entry:
- also
- not
- a
- hash
# This will produce nil associated to key :nil_entry
nil_entry:
You need to examine your fixtures files in test/fixtures and look for any that have the bad formatting as described above. Here is a rake task that will help you identify which files and entries need to be corrected. First run rails g task fixtures check_format, and place this code inside of the rake file that gets generated at lib/tasks/fixtures.rake.
namespace :fixtures do
desc "Looks for bad fixture files"
task check_format: :environment do
fixtures_dir = Rails.root.join("test", "fixtures")
fixture_files = Pathname.glob("#{fixtures_dir}/**/*.yml")
fixture_files.each do |file|
fixture = YAML.load(IO.read(file))
fixture.each_pair do |name, entry|
puts "Bad fixture entry #{name}: #{entry.inspect} in fixture #{file}" unless entry.is_a? Hash
end
end
end
end
Then run rails fixtures:check_format and the offending files and entries will be printed out on the command line for you to find and correct.
I have problems with error message ./spec/models/lib/parsers/s_reality_cz/matcher_spec.rb:12:in `block (2 levels) in ' in every rspec test. What I'm doing wrong? Using rspec-rails 3.5. Thanks
Code:
require 'rails_helper'
RSpec.describe Parsers::SRealityCz::Matcher, :type=> :model do
before do
#doc = File.open("spec/fixtures/srealitycz_profile.html") { |f| Nokogiri::HTML(f) }
end
let(:parser) { described_class }
it "returns total price" do
expect(parser.title(#doc)).to eq "Prodej bytu 1+kk 40 m²"
end
end
Rspec output:
Randomized with seed 37464
expected: "Prodej bytu 1+kk 40 m²"
got: "Prodej bytu 1+kk 40 m²"
(compared using ==)
./spec/models/lib/parsers/s_reality_cz/matcher_spec.rb:12:in `block (2 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
1 example, 1 failure, 0 passed
Finished in 0.139173354 seconds
Your strings look the same, but they probably have different encodings.
Convert them both to the same encoding (e.g. UTF-8), and compare them.
I am trying to create two custom rake tasks in in Rails 4 app. They are supposed to read the agent_card table and based on the logic, fire off an email when the time is triggered. This is my first time creating a rake task and I am not getting an error, therefore I don't know if I am doing it right. I would assume when using the mailer that letter opener would work once the mailer is fired (should it all be working correctly), but I am not sure. I have posted all relevant code below. Thanks in advance!
.rake file
namespace :agent_cards do
desc 'Sends an email to an agent with a license expiring 2 months from today'
task license_expire_agent: :environment do
Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
AgentCard.all.each do |agent_card|
if agent_card.real_estate_license_expires_at == Date.today + 2.months
LicenseExpireMailer.license_expire_agent(#agent_card, #agent).deliver_later
end
end
end
end
namespace :agent_cards do
desc 'Sends an email to an the agent development manager when a license expires 1 week from today'
task license_expire_mgr: :environment do
Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
AgentCard.all.each do |agent_card|
if agent_card.real_estate_license_expires_at == Date.today + 7
LicenseExpireMailer.license_expire_mgr(#agent_card, #agent).deliver_later
end
end
end
end
license_expire_agent_mailer.rb
class LicenseExpireMailer < ActionMailer::Base
default from: "Mike <help#mike.com>"
def license_expire_agent(agent, agent_card)
#agent = agent
#agent_card = agent_card
mail to: "mike#mike.com", subject: 'Your license is about to expire!'
end
end
license_expire_mgr_mailer.rb
class LicenseExpireMailer < ActionMailer::Base
default from: "Mike <help#mike.com>"
def license_expire_mgr(agent, agent_card)
#agent = agent
#agent_card = agent_card
# Head of Agent Development
mail to: "mike#mike.com", subject: "#{#agent.name}'s License Expiring"
end
end
When I try to test the rake task I enter
rake agent_cards:license_expire_agent
or
rake agent_cards:license_expire_mgr
Error
NameError: uninitialized constant LicenseExpireMailer
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:7:in `block (3 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1#global/gems/activerecord- 4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1#global/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:5:in `block (2 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => agent_cards:license_expire_agent
Your mailer file name does not match with the mailer class name as per Rails convention.
Mailer class should be placed in this path:
app/mailers/license_expire_mailer.rb
Hi I am trying to scrape a web page "take the links" go to that links and "to scrape it" too.
require 'rubygems'
require 'scrapi'
require 'uri'
Scraper::Base.parser :html_parser
web = "http://......"
def sub_web(linksubweb)
uri = URI.parse(URI.encode(linksubweb))
end
scraper = Scraper.define do
array :items
process "div.mozaique>div", :items => Scraper.define {
process "p>a", :title => :text
process "div.thumb>a", :link => "#href"
result :title, :link,
}
result :items
end
uri = URI.parse(URI.encode(web))
scraper.scrape(uri).each do |pag|
link_full = uri + pag.link.to_str
puts pag.title
sub_web(link_full)
puts
end
And I have the following error
e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/sss/web/app/views/admin/topics/webconector.rb
Title 1
http://mydomain/user34/top5
/Users/sss/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/uri/common.rb:304:in `escape': undefined method `gsub' for #<URI::HTTP:0x007fa07cb01e08> (NoMethodError)
from /Users/sss/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/uri/common.rb:623:in `escape'
from ../app/views/admin/topics/conectaweb.rb:11:in `sub_web'
from ../app/views/admin/topics/conectaweb.rb:34:in `block in <top (required)>'
from ../views/admin/topics/conectaweb.rb:29:in `each'
from ../app/views/admin/topics/conectaweb.rb:29:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Process finished with exit code 1
try using uri = URI.parse(URI.encode(linksubweb.to_s)) this should work. The problem is that method requires a string argument so you have to first convert the URI::HTTP object into string.
I got error on RSpec running my helper test on Rails Main Application (with further plugins)
1) MenuHelper maintence menu
Failure/Error: before { menu = build_menu_maintence() }
NameError:
undefined local variable or method `main_app' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x007f92f561f280>
# ./app/helpers/menu_helper.rb:37:in `eval'
# (eval):1:in `block in build_menu_items'
# ./app/helpers/menu_helper.rb:37:in `eval'
# ./app/helpers/menu_helper.rb:37:in `block in build_menu_items'
# ./app/helpers/menu_helper.rb:23:in `each'
# ./app/helpers/menu_helper.rb:23:in `build_menu_items'
# ./app/helpers/menu_helper.rb:15:in `build_menu'
# ./app/helpers/menu_helper.rb:48:in `build_menu_maintence'
# ./spec/helpers/menu_helper_spec.rb:11:in `block (3 levels) in <top (required)>'
I had success with the following in a mountable Engine:
def main_app
Rails.application.class.routes.url_helpers
end
main_app.root_path