An Rspec test like this (actually taken from RefineryCMS' own test suite)
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
module Refinery
describe FastController do
it "should render the wymiframe template" do
get :wymiframe
response.should be_success
end
end
end
Results in the following error:
Failure/Error: get :wymiframe
ActionController::RoutingError:
No route matches {:controller=>"refinery/fast", :action=>"wymiframe"}
# ./spec/controllers/fast_controller_spec.rb:6:in `block (2 levels) in <module:Refinery>'
In this case, I'm using refinery 2.0.8 with Rspec 2.11, and the relevant section after running rake routes looks like:
wymiframe GET /wymiframe(/:id)(.:format) refinery/fast#wymiframe
I have tried a few other controller Rspecs which also fail with routing errors. I am of course trying to write tests for my own extra methods I am adding to vanilla Refinery controllers, but just thought I'd see if I could get a controller test working for a totally fresh refinery install.
This must be a simple mistake! Any suggestions?
I stumbled upon this by accident. Instead of:
get :wymiframe
I needed to use:
get :wymiframe, { use_route: :any_old_thing }
I don't know why this worked - especially since for "any_old_thing", I really used nothing meaninful or connected tomy project at all. But, it seems to have worked, and now I can test my controllers.
Related
I'm using rails 3.0 and PDFKit. SASS and HAML but I don't have the asset pipeline implemented yet.
If I make a call from a controller I can generate perfect styled pdf with images, calling PDFKit.new passing render_to_string :show.
But if I do the same through a rake task, my PDF is generated without styles, and image_tag helper throws an error like this:
can't convert nil into String
Surely, I'm doing something wrong in the rake task... but everything works in the controller... What I'm missing?
Should I include something in the rake task? or maybe use another view with inline styles and absolute paths?
the calls are these:
CONTROLLER VERSION
def generate_html_invoice
render_to_string :show, layout: 'mypdflayout'
end
mypdf = PDFKit.new html_generator
RAKE TASK VERSION
def generate_html_invoice
invoice_view = ActionView::Base.new(MyWeb::Application.config.paths["app/views"].first)
invoice_view.assign({ ....... various params here})
html_invoice = invoice_view.render(template: "invoices/show", layout: 'mypdflayout')
return html_invoice
mypdf = PDFKit.new html_generator
The same error is thrown by image_tag helper and stylesheet_link_tag helper
An alternative way could be instantiate the controller in the rake task but.. is it possible? and, is it a good practice?
I couldn't find any decent solution to this, but I tried the following techniques:
QUICK AND DIRTY
change the view using %link and %img HAML tags instead the helpers, using the absolute path to the files.
SLOW BUT ELEGANT
In the rake task, call the controller to receive the view url and give it to PDFKit in this way
url = "#{(Rails.env.production? ? 'http://www.example.com' : 'http://localhost')}/invoce/#{invoice.id}"
path_to_pdf = "root/......./mypdf.pdf"
invoce_page = PDFKit.new url
invoce_page.to_file(path_to_pdf)
This one is the solution I choose. I know, it's a little dumb: the controller calls a rake task which calls the controller again... And it makes a lot of http request to the server.
But in this way I can have a underground process to generate PDF invoices without waiting for the response.
I think I don't have to worry about overcharging the server because the request will be queued normally.
I am working on cucumber BDD on rails-3 application.
When I use "assert ! controller.signed_in?" in step_definition
And When I run "cucumber"
Then I got this error "undefined method 'signed_in?' for nil:NilClass (NoMethodError)"
Why is it not going into controller-helper (signed_in? is defined in sessions_helper)?
When I tried by "assert ! SessionsController.signed_in?"
Error : undefined method `signed_in?' for SessionsController:Class (NoMethodError)
And When "assert ! SessionsHelpers.signed_in?"
Error : undefined method `signed_in?' for SessionsHelper:Module (NoMethodError)
In my Sessions_helper:
def signed_in?
!current_user.nil?
end
I also experienced this issue, and solved it by adding World(SessionsHelper) to the bottom of the Cucumber steps file (there's probably a better place to put it).
On a side note, you'll probably encounter other issues relating to cookies and sessions as it appears Cucumber does not support these between requests. So for example, you won't be able to test signed_in? after performing a redirect/submitting to the login form.
I found a weird bug in Rails 3.0.? that I want to report to the Rails team, but I have no idea how to do it.
Does anyone here know how to do this? Where is the open source project hosted? Do they have have ticket system?
Actually, I am going to share the issue I found, maybe it really isn't a bug. I am using Rails 3.0.7 and Ruby 1.8.7.
I created some static pages and I have two pages that are named very similar one named "holiday" and the other named "holidays". First, I created the singular "holiday" page and everything worked as it should. Then I created the plural version of it and when I tried to test it didn't work, I kept getting redirected to the not found or 404. Just to be clear, yes I restarted the server but that didn't fix the problem. The only way this weird issue went away in when I cleared the browser cache.
Here are the code snippets.
In the routes I added this:
match '/holiday' => 'pages#holiday' , :as => 'holiday'
match '/holidays' => 'pages#holidays' , :as => 'holidays'
in the controller I just added empty actions
def holiday(); end
def holidays(); end
somewhere in the views folder I have the corresponding pages "holiday.html.erb" and "holidays.html.erb".
When I visit the first page (/holiday) it works. The page gets served.
When I visit the second page (/hoidays) it doesn't work. I get redirected to the not found / 404 page.
Has anyone encountered this weird issue in Rails 3?
Here's the bugs/issues system:
https://github.com/rails/rails/issues
You can search for the bug before submitting it. Maybe someone already reported it.
I'm finding it difficult to understand how ActiveAdmin(http://activeadmin.info/) works with existing controllers
I have the following controllers
app/controllers/projects_controller.rb
and I was successfully able to implement ActiveAdmin UI over my views in the above controller. But my question is I have added the following before_filter in my controller
class StaticContentsController < ApplicationController
before_filter :list_content_types
def index
#static_contents = StaticContent.all
end
end
But this filter seems to be not executing, in fact I changed the code inside the index method to
#static_contents = abc StaticContent.all
As it should give and error because of 'abc' section, but surprisingly my app works with out an error. My guess is 'ActiveAdmin' reads controllers my its own, not the existing ones
this is my index action path
http://localhost:3000/admin/static_contents
and this is in development mode
Can someone help me on understanding how controllers works with ActiveAdmin or am I missing something here
Following are my configs
rails (3.0.0)
ruby 1.8.7
activeadmin (0.3.2)
thanks in advance
sameera
Activeadmin controllers are not the same as your app's controllers, they are separate. The reason your code is not causing an exception from the activeadmin interface is because that code is never hit. The activeadmin controller documentation specifies how to modify the default activeadmin actions.
It gives me the creeps,i'm done, i need some help here, i reverted multiple times back but i can't find the error.
Simple controller (customers),a simple form for adding a customer via :remote => true and the controller does respond_to do |format| { format.js } . Works fine, renders my create.js.rjs template.
I work for a few hours without making any javascript changes or changes to my controllers or authorization etc.. and when i try it again it's not working anymore.
What i mean with not working: Controller gets called, record saved, all partials rendered. But no javascript evaluated, not even a simple alert(1) at the beginning of the file.
I tried with different prototype.js versions and different rails.js versions, but nothing helped. I hope someone has a clue about this or already experienced this.
It's not that i don't want to post code. But it won't help. Its basic code that works and, after some changes where i don't know what i really changed (some locales here, some css there, html templates from a completely different controller a bit..)..
Currently developing with: ruby 1.9.2, rails 3.0.3, prototype 1.7 RC3, rails.js from github.
SOLVED
How stupid, I missed the part where the template naming changed. My application templace was named "application.rhtml". It worked until now. As it stopped to work, I changed it to "application.html.erb" and now it's working.