form_for and confirm option and passing Rspec tests - ruby-on-rails-3

I have my form
= semantic_form_for #order_refund, url: create_partial_refund_order_path do |f|
= f.inputs do
= f.input :order_id, as: :hidden
= f.input :amount, label: 'Amount to refund'
= f.actions :confirm => 'Are you sure you want to create a partial refund?
And my spec
it 'performs a partial refund' do
visit order_path(#order)
click_link('Partial Refund')
fill_in('Amount to refund', with: 5)
save_and_open_page
click_button('Create Refund')
current_path.should == order_path(#order)
page.should have_content 'Partial refund successful.'
end
Why is the test passing when it is not confirming the action? Thank you!'

The confirmation is made with JavaScript. While your test doesn't use JS than it just doesn't care about it.
If you want to test it than:
add to the Gemfile
gem 'selenium-webdriver'
enable JS for the test:
it 'performs a partial refund', js: true do

Related

Show validation errors in view for custom Devise login form

At the moment, I'm building a Rails platform using Spree Commerce where I need two Devise login forms.
The default Devise login forms are already implemented. For the second (custom) Devise form, I created the following view:
global_login.html.erb
<div class="inner">
<h2 class="large-margin">
Login
</h2>
<%= form_for(Spree::User.new, :url => spree_login_path) do |f| %>
<%= f.error_messages %>
<%= f.email_field :email, {:value => params[:email], :placeholder => 'Email address', :class => 'input'} %>
<%= f.password_field :password, {:placeholder => 'Password', :class => 'input'} %>
<%= f.submit 'Login', :class => 'submit' %>
<% end %>
</div>
This view does display a Devise login form. However, every time I submit the form, I'm being redirected to the default Devise view where the validation errors are shown.
Does somebody know a solution to this problem?
Cheers!
I found a workaround for this issue. I added a param to one of the forms named "inside_cart". When the request is send to the #create method, I check if the param is present.
I overwrote the Spree::UserSessionsController#create method as follows:
Spree::UserSessionsController.class_eval do
layout false
def create
authenticate_spree_user!
if spree_user_signed_in?
respond_to do |format|
format.html {
flash[:success] = Spree.t(:logged_in_succesfully)
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
}
format.js {
user = resource.record
render :json => {:ship_address => user.ship_address, :bill_address => user.bill_address}.to_json
}
end
else
if params[:spree_user] && params[:spree_user][:inside_cart]
flash.now[:error] = t('devise.failure.invalid')
render :new, layout: false
else
flash.now[:error] = t('devise.failure.invalid')
render 'spree/users/global_login', layout: 'spree/layouts/spree_application'
end
end
end
end

Recaptcha only showing up after page refresh - Rails 4 - Devise

I have installed recaptcha according to the gem instructions, however when I view the sign_up page (using Devise), the catcha doesn't appear until I refresh the page.
Looking through other comments, the recommendation is to disable turbolinks (which I am using) by changing the sign_in link to:
<%= link_to "Sign up", new_registration_path, "data-no-turbolink" => true %><br />
I have tried this, but I still don't get the captcha until I do a page refresh.
Relevant code:
Views/devise/registrations/new.html.erb
................
<%= recaptcha_tags %>
................
/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def create
if !verify_recaptcha
flash.delete :recaptcha_error
build_resource
resource.valid?
resource.errors.add(:base, "There was an error with the recaptcha code below. Please re-enter the code.")
clean_up_passwords(resource)
respond_with_navigational(resource) { render_with_scope :new }
else
flash.delete :recaptcha_error
super
end
end
def clean_up_passwords(*args)
# Delete or comment out this method to prevent the password fields from
# repopulating after a failed registration
end
end
Issue was that I needed to still include "class:" in the link:
<%= link_to "Sign up", new_user_registration_path, "data-no-turbolink" => true, class: "btn btn-primary btn-med" %>
Just try in controller this code
def create
if resource.valid? && !verify_recaptcha
clean_up_passwords(resource)
flash.delete :recaptcha_error
elsif !resource.valid? && verify_recaptcha
clean_up_passwords resource
respond_with resource
elsif !resource.valid? && !verify_recaptcha
flash.now[:alert] = "Recaptcha error"
flash.delete :recaptcha_error
clean_up_passwords resource
respond_with resource
end
end
And in your Views/devise/registrations/new.html.erb
<%= recaptcha_tags display: {theme: 'red', tabindex: 5}, ssl: false, noscript: false %>

form_for tag with conditions + rails3

I have a form_for tag and I want to enable its :remote => true option upon the user status
if the current user is not an admin I want the form to be :remote => true
if the current user is an admin I want the form to be a default form
This is what I came up with and its nowt working ;(
<%= form_for #school, ((:remote => true) unless current_user.admin?) , :html => { :class => 'form-horizontal' } do |f| %>
can some one help me
I'm on
ruby 1.9
Rails 3.2.x
thanks in advance
How about using a Helper method with the following
def form_remote_or_not(user, object, &block)
if user.admin?
form_for object, :remote => true, &block
else
form_for object, &block
end
end
As for your view file; <%= form_remote_or_not(current_user, #school) do |f| %>

devise rails - error messages in different view?

I'm trying to show my errors (both flash and devise) in one of my layout files (_header.html.erb):
<div id="alerts">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<% end %>
<%= devise_error_messages! %>
</div>
but I'm getting an error: undefined local variable or method `resource', because the error message is no longer in the devise form. I tried the method suggested here: http://pupeno.com/2010/08/29/show-a-devise-log-in-form-in-another-page/ by pasting that code into the application_controller.rb file. No luck. Ideas?
oh. I forgot to mention... The pages work without error, but my tests are failing... Here's a failing test:
it 'succeeds with a valid email and password' do
user = Factory.create(:user)
visit sign_in_path
fill_in 'user_email', :with => user.email
fill_in 'user_password', :with => user.password
click_button 'Sign in'
page.should have_content("hi #{user.username}")
end
This is Rails3, fyi.
I was getting errors like this and solved using :partial and :locals when using the render method:
<%= render :partial => 'header', :locals => { :flash => flash } %>
See Rails 3: "undefined local variable or method" if I put content in a partial
turns out the code needed to be in the application_helper instead of the controller, but the code at pupeno.com works!

User signed out halfway through cucumber test

I'm in the process of splitting change-password functionality off into its own page on my site. The whole process works fine when I run through it manually as a real user, but I can't get my Cucumber tests to pass, the reason being that on the final call to the controller, the current_user is mysteriously returning nil.
I've having a serious WTF moment because the user DEFINITELY is being logged in at the beginning of the tests, and I'm following all the same steps as other passing tests, and as I said the whole thing works fine when stepping through it by hand.
Here's the test in question:
Scenario: The user should be able to change their password
Given I have a signed in user with email = "test#mycompany.com" and password = "password"
When I am on the change-password page # this hits registrations_controller#change_password
And I fill in "Current password" with "password"
And I fill in "New password" with "new_password"
And I fill in "Re-enter new password" with "new_password"
And I press "Update"
Then I should see "You updated your account successfully"
And the login step:
Given /^I have a signed in user with email\s*=\s*"([^"]*)" and password\s*=\s*"([^"]*)"$/ do |email, password|
#user = User.new
#user.email = email
#user.password = password
#user.confirm!
#user.save!
visit '/sign_in'
fill_in("Email", :with => #user.email)
fill_in("Password", :with => #user.password)
click_button("Sign in")
end
Controller actions, from "registrations_controller.rb":
def change_password
# calling "current_user" here retuns the logged-in user as expected
#user = current_user
end
def update_password
# calling "current_user" here returns nil
# error occurs at call to nil.update_with_password
#user = current_user
if #user.update_with_password( params["user"] )
redirect_to after_update_path_for( #user )
else
clean_up_passwords( #user )
render_with_scope :change_password
end
end
Now, we are using devise (1.1.8), and my best guess is that I've done something wrong with the devise routes, which look like this in "routes.rb"
devise_for :users, :controllers => {:confirmations => "confirmations", :sessions => "sessions", :registrations => "registrations"} do
# ...other routes here...
get "/change_password", :to => "registrations#change_password"
put "/update_password", :to => "registrations#update_password"
end
Finally, for completeness, here is "change_password.html.haml"
= render :partial => "shared/stats"
#main
= form_for(resource, :as => resource_name, :url => "update_password", :html => { :method => :put, :autocomplete => "off" }) do |f|
= devise_error_messages!
.edit-account-hold
%span Your password
= f.label :current_password, "Current password"
= f.password_field :current_password
= f.label :password, "New password"
= f.password_field :password
= f.label :password_confirmation, "Re-enter new password"
= f.password_field :password_confirmation
= f.submit "Update", :class => "orange-button border-radius"
= link_to "Cancel", account_path, :id => "cancel-link"
:javascript
$(document).ready( function() {
$("#user_current_password").focus();
});
Try running it through Selenium. Watching the test run can often give clues about what's going wrong.