Rails sitemap generator, undefined method post_path? - ruby-on-rails-3

I am using the following gem:
https://github.com/kjvarga/sitemap_generator
I have a posts model and have used post_path(post) in other parts of the application as it is based on Enki.
However in the sitemap file:
Post.all.each do |post|
sitemap.add post_path(post), :lastmod => post.updated_at
end
This returns the error when running the rake task rake sitemap:refresh:
rake aborted!
undefined method `post_path' for #<SitemapGenerator::Interpreter:0x279efd0>
And:
Post.all.each do |post|
sitemap.add posts_path(post), :lastmod => post.updated_at
end
Returns no errors. Can anyone shed any light on this or do I need to provide more of the code?

sitemap_generator actually includes all the helper methods in the create block, so you should be able to access posts_path.
I had a similar problem, and my answer also made me feel stupid. I was storing sitemap.rb in config/initializers, when it should be stored in the config/ directory. That meant it was running on startup and failing because the url helpers weren't properly loaded (and, incidentally, when I called rake sitemap:refresh, the sitemap was being generated twice - once in the initializer and again as the rake task!)

I feel a bit stupid on this one, it was simply because post_path was a helper and not accessible, so I simply had to move it into the method directly.

Related

Getting uninitialized constant (NameError) inside decorators when running rspec tests via rake

In my app I use an engine (blogit) to which I want to add some changes / behaviours.
I followed the guides on how to override engine controllers/models and added the following:
The Code
In config/initializer/blogit.rb
# Requires extension ruby files in lib/blogit.
Dir[Rails.root.join("lib/blogit/*.rb")].each {|f| require f}
In lib/blogit/engine.rb
module Blogit
class Engine < ::Rails::Engine
isolate_namespace Blogit
config.to_prepare do
Dir.glob(Rails.root + "app/decorators/**/blogit/*_decorator*.rb").each do |c|
require_dependency(c)
end
end
end
end
In app/decorators/controllers/blogit/comments_controller_decorator.rb
Blogit::CommentsController.class_eval do
def create
Rails.logger.info "decorated controller action"
# ... overridden stripped ...
end
end
In app/decorators/models/blogit/comment_decorator.rb
Blogit::Comment.class_eval do
belongs_to :user
end
To be mentioned:
I have also created a migration to add a user reference to the comments model, since my app uses devise and I only want logged_in users to be able to comment. (Therefore I don't need the standard behaviour, so I'm going to override it.)
The Problem
If I run rake I get the weird error:
/Users/Kassi/.rvm/rubies/ruby-1.9.3-p392-railsexpress/bin/ruby -S rspec ./spec/controllers/home_controller_spec.rb ./spec/models/user_spec.rb
/Users/Kassi/demo/app/decorators/controllers/blogit/comments_controller_decorator.rb:3:in `<top (required)>': uninitialized constant Blogit::CommentsController (NameError)
However, if I run the first line that rake mentions by hand (.../ruby -S ...), all tests are being run successfully.
In my project I'm using guard with spork. Running guard will also let the tests pass without any error.
The app itself runs fine, i.e. it starts without errors and I'm able to comment as I want. My decorator action code is being executed.
So what's different when running rake?
Why does it break?
Note:
Adding require "blogit" or require "blogit/comments_controller" doesn't help. It actually can't find the controller using require.
A Demo Application
Since this problem is part of a bigger project, I created a new app from scratch for testing purposes that contains only the relevant stuff: basic rails app, rspec, devise, blogit and the decorators.
It can be found here: https://github.com/kassi/decorator_demo_rspec (git://github.com/kassi/decorator_demo_rspec.git)
Another repo using testunit (which is working!) can be found here: https://github.com/kassi/decorator_demo_testunit (git://github.com/kassi/decorator_demo_testunit.git)
Instead of performing the require statements yourself, you can try activesupport-decorators that does it for you.

Use Ruby Extension (.bundle) in Rails Applications

I have a built a Ruby Extension for C/Objective-C and now would like to use it in a Ruby on Rails web application but everytime I try to require it, WEBrick crashes! What I have is:
I have a Ruby Class: MyRubyObject which is tied to a C class (MyExtension).
MyRubyObject has been defined with one method: myRubyMethod which is tied to a function in my C class, MyExtension: myextensionmethod which just returns a string.
myextensionmethod essentailly calls another library which performs a load of work (multiple threads and what not) but finally returns a string
after compiling everything I get a MyRubyObject.bundle file. Using IRB, I can:
require './MyRubyObject'
p = MyRubyObject.new
p.myRubyMethod
=> "Result!"
This all works absolutely fine and now I want to use it in a RoR app
I put the MyRubyObject.bundle in lib in my RoR app and then from a controller class I do a require ./lib/MyRubyObject.bundle and WEBrick crashes!!!!
Any help would be greatly appreciated!!!
Cheers
I found a solution where from here: ruby-ldap gem not work in rails3 app, but work in rails console whereby I
Put a 'require File.expand_path('../../lib/MyRubyObject', __FILE__)' in config/environment.rb file (after 'require File.expand_path('../application', __FILE__)')
Then in my controller file I just put a require 'MyRubyObject' and was then able to instantiate normally: p = MyRubyObject.new ...
So I presume this was a related issue to WEBrick loading things in an incorrect order based upon the above link but I will keep an eye on this as we'll move servers most likely and this issue may pop it's ugly head up again!
Hope this helps others!
Thanks

Paperclip Rspec tests : why missing attr_accessor?

I had a set of Rspec User Model tests that were passing, until I had the paperclip attribute "profile picture".
Everything is fine with paperclip, it works great, but I would like my tests also to be updated to pass. I just added this line to the #attr of the user_spec file:
:avatar => File.new (Rails.root + "public/images/Default_profile_picture.jpg")
in a before_each loop that creates a standard set of working attribute for the user model.
When I run the tests, ALL the tests give me the error message:
User model missing required attr_accessor for 'avatar_file_name' ...
Putting paperclip's fields as attr_accessor in the model makes the problem disappear in the test, but I don't think I should do this in my model (it does not work properly if I do).
Does anyone has an idea why I got these error messages when testing ? ... (I included the config line Shoulda::Matchers and the require paperclip lines in the spec/helper)
Thanks for any answer you may post !
It was simply that I forgot to run
rake db:test:prepare
before running my tests :-/

Debugging Unexpected method call Rails 3.1.1

I am using Rails 3.1.1 and added asset-pipelining to my app.
After a call to a method is done, example method1.
def method1
#locations = Location.name_like(params[:term].to_s) # returns a valid hash of values
respond_to do |format|
format.json { render :json => #locations.to_json }
end
end
another method(method2) is being called. As a result I am getting a nil class error.
I checked my logs and it does not show how or why method2 is getting being called. Also I checked if any javascript is calling method2 but there is no javascript for that.
I need help in debugging this situation.
Also I was wondering if asset-pipelining is causing this issue.
Thanks,
Vishal
The assets pipeline is mounted at /assets so I think this is unlikely.
It sounds as though the route for method1 is being captured and sent to method2.
This can happen if a broader route encompassing method1's route is higher up the file.
I would run the rake routes task, and also walk through the route file looking for broad routes the would match method1.

Raising route not found error

I'm writing a book on Rails 3 at the moment and past-me has written in Chapter 3 or so that when a specific feature is run that a routing error is generated. Now, it's unlike me to go writing things that aren't true, so I'm pretty sure this happened once in the past.
I haven't yet been able to duplicate the scenario myself, but I'm pretty confident it's one of the forgotten settings in the environment file.
To duplicate this issue:
Generate a new rails project
important: Remove the public/index.html file
Add cucumber-rails and capybara to the "test" group in your Gemfile
run bundle install
run rails g cucumber:skeleton
Generate a new feature, call it features/creating_projects.feature
Inside this feature put:
This:
Feature: Creating projects
In order to value
As a role
I want feature
Scenario: title
Given I am on the homepage
When you run this feature using bundle exec cucumber features/creating_projects.feature it should fail with a "No route matches /" error, because you didn't define the root route. However, what I and others are seeing is that it doesn't.
Now I've set a setting in test.rb that will get this exception page to show, but I would rather Rails did a hard-raise of the exception so that it showed up in Cucumber as a failing step, like I'm pretty sure it used to, rather than a passing step.
Does anybody know what could have changed since May-ish of last year for Rails to not do this? I'm pretty confident it's some setting in config/environments/test.rb, but for the life of me I cannot figure it out.
After I investigate the Rails source code, it seems like the ActionDispatch::ShowExceptions middleware that responsible of raising exception ActionController::RoutingError is missing in the test environment. Confirmed by running rake middleware and rake middleware RAILS_ENV=test.
You can see that in https://github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L152 it's returning X-Cascade => 'pass' header, and it's ActionDispatch::ShowExceptions's responsibility to pick it up (in https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L52)
So the reason you're seeing that your test case is passing because rack-mount is returning "Not Found" text, with status 404.
I'll git blame people and get it fix for you. It's this conditional here: https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L159. If the setting is true, the error got translated right but we got error page output. If it's false, then this middleware doesn't get loaded at all. Hold on ...
Update: To clearify the previous block, you're hitting the dead end here. If you're setting action_dispatch.show_exceptions to false, you'll not get that middleware loaded, resulted in the 404 error from rack-mount got rendered. Whereas if you're setting action_dispatch.show_exceptions to true, that middleware will got loaded but it will rescue the error and render a nice "exception" page for you.