How can you pass a block to have_selector? - ruby-on-rails-3

I have the following view which I can't spec out properly in a view spec:
file: "products/index.html.haml"
#products
= render #products
And this is my view spec:
require 'spec_helper'
describe "products/index.html.haml" do
let(:products) {[mock_model(Product)]}
before do
stub_template "products/product.html.haml" => ""
render
end
it "should render the products" do
rendered.should have_selector(#products) do
rendered.should render_template products
end
end
The problem here is that have_selector does not accept a block so there is no way to assert that the partial is rendered inside the #products div. Since Capybara matchers don't work within View specs you cannot use within either.
See also this issue: https://github.com/rspec/rspec-rails/issues/387

The correct answer is with webrat does take a block, and capybara does not.
Here's the issue in which Jonas Nicklas, the creator of capybara explains why it doesn't and that he's not planning on adding it:
https://github.com/jnicklas/capybara/issues/384
There were some examples using Object#tap to pass blocks to capybara's #find that perhaps once worked, but don't seem to be working any longer.
UPDATE: Confirmation from David that there is no way to do this presently with rspec/capybara:
https://groups.google.com/forum/?fromgroups=#!topic/rspec/HBfznq4Yd0k

What I do is test for a class or id that the partial renders. And have_selector does accept blocks.
it "should render the products" do
rendered.should have_selector('#products') do |products|
products.should have_select('.product')
end
rendered.should render_template 'products'
end

Related

Rails 3 Remote Form Ajax Success

I'm having some issue figuring out how to handle success on my entries form using rails 3 ujs ajax.
If there are errors, I have a create.js.erb that will alert(j(#entry.errors.full_messages), and this works. But if there are no errors, the form doesn't redirect (because I'm rendering in a dialog) and I'd like the js to alert("success") and close the dialog. (using fancybox 2).
Can you give me some pointers working with rails 3 ujs and ajax?
An approach is to identify and handle the error at controller level instead of view.
def create
#entry = something
if #entry.save
#notice = "Success message!"
respond_to :js # render default create.js.erb
else
respond_to :js { render 'create_error.js.erb' }
end
end
// create.js.erb
$("#dialog").close();
alert("<%= #notice %>">;
// create_error.js.erb
alert(j(#entry.errors.full_messages);

I want to use `layout false` but I still want to use the CSS that would typically load when I'm rendering the layout - how do I do this?

I have a controller where I set layout to false:
class SplashController < ApplicationController
layout false
def index
end
end
But when I load this page there is no css whatsoever - I assume this has to do with how rails handles layout false - but my current knowledge of rails leaves me lost.
How do I not render a layout, but still load all the other assets (css, js, etc. . .) that would typically load if I were to load a layout? (*Note that the layout file has no specific reference to any of these assets)
By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the :layout => true option.
As you need only the information to be displayed, I suggest to use :text to render.
You can send plain text – with no markup at all – back to the browser by using the :text option to render:
render :text => "OK"
NOTE: Rendering pure text is most useful when you’re responding to AJAX or web service requests that are expecting something other than proper HTML.
UPDATE:
Also if you want that assets should be shown but still layout should be false then you have to render layout to false after making the assets available. This means you make some view, then define your required css and js files there and then call that view from controller and then set layout to false.
Setting the layout to false after view will show the css and js stuff but still keep the layout to false.
But setting the layout to false before showing the view that contains css and js will not include assets at all.
The other alternative of the above will work also:
css : <%= eval("render :partial => 'myurl/blah', :formats=> [:css], :layout => false").dump.html_safe %>
You see that how partial view that contains your assets like :css is getting called while layout is false.

rendering a partial Rails3.x + coffeescript

I have the following requirement. I have a 'school' drop down and as the last options I have add new school, so if the user selects that option I want to load the new_school form as a partial via ajax.
I'm on
gem 'rails', '3.2.9'
gem 'coffee-rails', '~> 3.2.1'
Jquery via gem 'jquery-rails'
Earlier with rails < 3 and prototype I used to do it with
Ajax.Updater (aka Rails link_to_remote :update => 'some_div')
and with rails > 3 + JQuery I'm familiar with *.js.erb, and having something like
$("#school_form").html("<%= escape_javascript(render(:partial => "form"))%>");
But I'm new to coffeescript and I have no idea on how to do this with coffeescript, can someone help me :), (because I believe you shouldn't have to do a server request for this)
So far I have done following to catch the select_tag change event
$ ->
$('#school_name_select').change ->
unless $(this).val()
$('school_name').html([I want to have the _new_school_form partial here])
Use a hidden div.
In general, you don't want to bother trying to mix JS and HTML. The escaping can be complicated, error-prone, and flat out dangerous due to the possibility of cross-site scripting attacks.
Simply render your form partial in a div that's not displayed by default. In ERB:
<div id="school_name_form" style="display: none;">
<%= render 'form' %>
</div>
In your CoffeeScript:
$ ->
$('#school_name_select').change ->
if $(this).val()
$('#school_name_form').slideUp()
else
$('#school_name_form').slideDown()
I recommend using a small, tasteful transition like slide or fade. It gives your app a more polished feel.
No AJAX is required. This pattern is so common that I have an application-wide style defined as follows.
.not-displayed {
display: none;
}
Then using HAML (if you're into that), the HTML template becomes simply:
#school_name_form.not-displayed
= render 'form'
You can try to render the form partial inside hidden div (not too correct from semantic point of view), or put the form html as data attribute of any relevant element, something like
f.select school_name, ... , data: {form: escape_javascript(render(:partial => "form"))}
And the Coffeescript
$ ->
$('#school_name_select').change ->
unless $(this).val()
$('school_name').html($('#school_name_select').data('form'))

Override html in active_admin gem

I wanna override html code when working with active_admin gem in Rails; because the nav-bar and many elements in these gem's views are different with my views (other pages). I hope that has a way to change html code without changing css manually! Thanks
It is not very easy , activeadmin use DSL for building html (called "Arbre")
You have to monkey patch every page class, also , it may prevent customizing of css too.
For example to move sidebar to left, create initializer with next patch.
class ActiveAdmin::Views::Pages::Base < Arbre::HTML::Document
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_sidebar unless skip_sidebar?
build_main_content_wrapper
end
end
end
default method was
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
The full list of classes used for rendering can be found here , so some of them you need to patch.
https://github.com/gregbell/active_admin/tree/master/lib/active_admin/views
Be ready for a big piece of work.
UPD. Gem for changing activeadmin sidebar position
https://github.com/Fivell/active_admin_sidebar

Rails prevent layout during AJAX request

I have searched around and have not been able to find a solution for this type of mechanic. I want to load all pages normally in Rails, but whenever I do an ajax request I just want to return the page without the layout. So anytime I make an ajax requst I can append a ?page=true or something along those lines and have Rails just return the page without the layout.
Is this possible? Is there a better way to do it that I am missing?
Thanks for any help!
Final Solution Working Code:
In the controller all you need to do is append a little logic to the format.html in the respond_to block.
In the show method for example
def show
# code beforehand
respond_to do |format|
format.html { render :layout => !request.xhr? }
# other formats
end
end
And that's it! Prevent layouts during AJAX requests!
Note: Thanks to the smathy's comment on his answer this was simplified further. I originally had format.html { render :layout => nil if request.xhr? } This solution works just as well, but smathy's modification keeps it even simpler.
You don't need to add that parameter, request.xhr? will return true in your controller when it's an Ajax request. Just use that to decide whether to render the layout or not.