control not entering format.html block - ruby-on-rails-4.2

My controller method is like:
def forcefully_logout
process_license_pool_obj = Vendor::ProcessLicensePool.new(#user.vendor)
#user.authentication_token = ''
if #user.save
if process_license_pool_obj.update_license_pool?(false, #user.id.to_s, #user.vendor_id.to_s)
respond_to do |format|
format.html do
redirect_to license_management_vendor_url(#user.vendor_id), notice: 'User was successfully logged-out and license has been released.'
end and return
end
end
end
respond_to do |format|
format.html { redirect_to license_management_vendor_url(#user.vendor_id), warn: 'Unable to forcefully logout user, contact admin.' }
end
end
and the route for this method is as:-
resources :users, except: [:create] do
member { delete 'forcefully_logout', to: 'users#forcefully_logout' }
end
Although the method is working properly, but it gives error for "missing template" and the controll doesn't enters the 'format.html' block means instead of redirecting the method tries to render template. In server log the message for this request is something as "started delete and processing by this method as html" (since the request is also of html type) but redirect_to is not hit. What is the possible reason for this problem is this rails issue or am i missing some concept regarding request object. The request is of "delete" type. Thanks.

Actually the "and return" statement after "format.html" block was causing the flow not to enter 'format.html' block, the working code is as:
def forcefully_logout
process_license_pool_obj = Vendor::ProcessLicensePool.new(#user.vendor)
#user.authentication_token = ''
if #user.save
if process_license_pool_obj.update_license_pool?(false, #user.id.to_s, #user.vendor_id.to_s)
respond_to do |format|
format.html do
redirect_to license_management_vendor_url(#user.vendor_id), notice: 'User was successfully logged-out and license has been released.' and return
end
end
end
end
respond_to do |format|
format.html { redirect_to license_management_vendor_url(#user.vendor_id), warn: 'Unable to forcefully logout user, contact admin.' }
end
end

Related

NetSuite beforeload cancel action

I have a userevent beforeload that conditionally removes print buttons. If users are in the list view they are still able to print from that menu. So I was wondering if anyone knows of a way to either intercept the request and redirect or notify them, or even change which pdf template is used.
Here is my code snippet:
if (scriptContext.type === scriptContext.UserEventType.PRINT) {
if(status == 2){
log.debug({
title: 'Trying to Print Approved PO',
details: status
});
return true;
}
else{
log.debug({
title: 'Trying to Print UnApproved PO',
details: status
});
//code here to redirect users, notify them of issue or change to different pdf template
return false;
}
}
While not ideal, I just threw an error and didn't catch it.
throw 'This error is a result of trying to print a PO that has not been approved.';
You can add checkbox field on the UE beforeLoad event and set the value true/false if the user can/can't generate the pdf. Then control the new field on the freemarker AdvPDF and instead of print the content, you can show a message if the user can't generate the pdf.
var field = objForm.addField({
id: 'custpage_allow_access',
type: serverWidget.FieldType.CHECKBOX,
label: 'Allow access'
});
field.defaultValue = "F";
field.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN
});

Multiple Plans with Stripe on Rails & No Customer Payment Source

I have Stripe setup with Rails and Devise. It works beautifully, but there is three errors.
First of all, I would like to implement two different plans. Once they select it with the Pay With Stripe button, Stripe would do the work of making the customer pay the plan they choose monthly.
Second issue: When I go to the /charges/new path, it works beautifully and shows the plans. However, when I go to the /charges path, it gives me the error of This customer has no attached payment source.
Third issue: The user is suppose to go from /users/new to the /charges/new. This isn't happening.
How would I fix all of these errors?
Here's my charges_controller.rb:
class ChargesController < ApplicationController
before_filter :authenticate_user!
def new
end
def update
end
def create
token = params[:stripeToken]
customer = Stripe::Customer.create(
source: token,
plan: 2020,
email: current_user.email,
)
current_user.subscribed = true
current_user.stripeid = customer.id
current_user.save
redirect_to profile_path
end
end
Here's my routes.rb file:
Rails.application.routes.draw do
devise_for :users, controllers: {registrations: "registrations"}, path_names: { sign_in: "login", sign_out: "logout", registration: "signup", sign_up: "cmon_lemme_in_guys" }
resources :things
resources :charges, only: [:new, :create]
get "charges", to: "charges#create"
authenticated :user do
root "things#profile"
get "profile", to: "things#profile"
end
root "index#welcome"
get "license/terms"
end

what is the proper way for testing create action in rails RESTfull controller?

I have a users_controller.rb file in which the create action is defined as:-
def create
#user = User.new(user_params)
if current_user.g_admin?
#user.role = 'c_admin'
elsif current_user.c_admin?
#user.role = 'c_user'
end
#user.admin_provisioned = true
authorize #user
respond_to do |format|
if #user.save
format.html { redirect_to authenticated_root_url, notice: 'User was successfully created and email has been sent.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
Now, I would like to write test for this method reflecting all the behaviour in this method. I'm completely new to testing, so detailed and standard way for writing test will be accepted as answer. The version to rspec is 3.4. Happy coding.
So the following way you could test this with rspec and capybara. For the fill in part you have to use the input elements that you have on your page.
describe "let an guest create a user account" do
fill_in :user_object, with: "value"
click_button "Save User"
expect(page).to have_content("User was successfully created and email has been sent.")
end
describe "let an guest try to create an account with invalid data" do
fill_in :user_object, with: "invalid value"
click_button "Save User"
expect(page).to_not have_content("User was successfully created and email has been sent.")
expect(page).to have_content("content from the :new page")
end

How do I add a custom form for my collection_action/controller?

I have the following:
collection_action :new, :method => :post do
begin
user = User.find_by_email(params[:email])
if user
UserPermission.create(:user_id => user.id,
:permission => UserPermission::SUPPORT,
:creator => current_user)
end
rescue ActiveRecord::RecordNotFound
flash[:warn] = 'User not found'
end
redirect_to admin_support_users_path, notice: 'Support user added.'
end
form do |f|
f.inputs do
f.input :email
end
end
action_item only: [:index], :method => :post do
link_to 'Add Support User', new_admin_support_user_path
end
The above works in the sense that no error is thrown. The support users page loads and I'm able to click the Add Support User button. However, 'Support user added.' is immediately shown. The Add Support User button does not take me to a form to enter an email. How do I add/create/use a form that passes an email parameter to my collection_action?
I'm new to activeadmin and documentation is sparse, so any help is appreciated. Thanks.
Figured it out. I'll try to explain as I understand it. And my initial question may have been unclear. The reason I was getting the, 'Support user added.' message is because I was updating the wrong method. The method above should have been the :create controller method, not the :new controller method. :new uses HTTP GET, which is why it would go directly to the redirect. :create accepts an HTTP POST. So, instead, I have the following:
def create
begin
user = User.find_by_email(params[:email])
if user
UserPermission.create(:user_id => user.id,
:permission => UserPermission::SUPPORT,
:creator => current_user)
end
rescue ActiveRecord::RecordNotFound
flash[:warn] = 'User not found'
end
redirect_to admin_support_users_path, notice: 'Support user added.'
end
def new
render 'new.html.arb', :layout => 'active_admin'
end
And this correctly creates a nice looking active admin form, accepting an email parameter.
You just need to add another action--just like a normal resource needs separate actions for create and new. Your 'new' action can render a custom form either inline or in a separate partial, as shown here:
http://www.activeadmin.info/docs/5-forms.html
That said, I'm not sure I understand why you need a custom action. Is this in your User resource file in active admin? If so you can just use the default new user action and include the current user in the form as a hidden variable as the creator. If this is not in your User resource active admin file then you probably need one.

How to check if helper method/variable exists in rspec?

Im still trying to figure out rspec and right now I am using authlogic to handle my users and sessions. I did the usual authlogic stuff like adding the def current_user method to applciation controller, and as a helper method, but how do I access that in my rspec controller file?
Here is my user_sessions_controller_spec.rb:
require 'spec_helper'
require 'authlogic'
describe UserSessionsController do
context "user is already logged in" do
before(:each) do
include Authlogic::TestCase
activate_authlogic
UserSession.create Factory.build(:user)
end
it "should redirect the user to the home page" do
get 'new'
response.should redirect_to(home_path)
end
end
describe "#create" do
context "when the user is not logged in" do
before(:each) do
current_user = nil
end
it "correct authorization should create a new session" do
post 'create', {:login => "afactoryuser", :password => "apass", :password_confirmation => "apass"}
current_user.should_not be_nil
end
end
end
end
when i run rspec it just tells me:
correct authorization should create a new session
undefined local variable or method `current_user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x000001017d3410>
so im guessing it is in the rspec context...but how do i know it should be in user-sessions_controller? And am I even testing it correctly?
Insted of current_user in your RSpec, try controller.current_user
If you want to stub current_user method use: controller.stub!(:current_user).and_return(whatever) in before :each
block, but then you will always get whatever if you stub it.

Resources