Why Rails redirects to the /create page? - ruby-on-rails

I have the next create action in feed_controller:
def create
#feed = Feed.new(check_params)
#new_feed = Feed.new(check_params)
#feed.member_id = current_user.member.id
if params[:commit] == "Upload Video"
if #feed.save
now_is = User.select("uploadLimit").where("email" => session[:email]).last.uploadLimit
User.update(session[:id], :uploadLimit => now_is.to_i - 1)
redirect_to "/#{params[:locale]}/feed"
flash[:notice] = "Congratulations! Your media file has been added!"
else
redirect_to "/#{params[:locale]}/feed"
flash[:notice] = "Oops! Something went wrong!"
end
end
end
And my routes.rb is:
post '/:locale/feed/create', to: "feed#create"
get '/:locale/feed/new', to: "feed#new", as: 'feed_new'
So, my question is, why it redirects to the /create but not to the redirect_to "/#{params[:locale]}/feed page? It works yesterday, but today I'm checking now it ceased to work. Who knows, what is the problem?

Related

How to test destroy failure in ruby-on-rails

everyone.
I'm gonna test an active record object destroy failure but I've problems creating a failure situation.
I have a before_filter method called 'require_user_payment_info' which validates the #payment_info object before the delete method is called, so I can't create a 'bad' #payment_info object before the delete method is called.
Here's the require_user_payment_info method:
def require_user_payment_info
#payment_info = credit_card_model.slave.find_by_user_guid(user_guid)
if !#payment_info || #payment_info.user_guid != user_guid
redirect_to(:controller => 'store', :action => 'index') and return false
else
if((#payment_info.card_expires_year.to_i < Date.today.year) ||
((#payment_info.card_expires_month.to_i < Date.today.month) && (#payment_info.card_expires_year.to_i == Date.today.year)))
#payment_info.card_account_public = "" #clear this out so the user is forced to re-enter the credit card number
#payment_info.valid?
flash.now[:error] = t('ui_flash_errors.card_expired_error')
end
end
end
And the actual delete method:
def delete
# required to be called via a delete request
redirect_to :action => 'edit' and return if !request.delete?
if #payment_info.destroy
flash[:notice] = "Delete SUCCESSFUL"
redirect_to :action => 'new'
else
flash[:error] = "Delete failed"
redirect_to :action => 'edit'
end
Any ideas?
This is my solution:
def test_delete
payment_info = Factory.create(:payment_info, :user_guid=>#user.guid, :card_expires_month=>'04',
:card_expires_year=>(Date.today.year+2).to_s, :cardholder_city=>"test city",
:cardholder_state=>'NC', :cardholder_country=>'US', :cardholder_zip=>'27612')
PaymentInfo.any_instance.stubs(:destroy).returns(false)
delete(:delete, {}, #session)
assert_response(:redirect)
assert_equal false, assigns(:payment_info).blank?
assert_redirected_to({:controller=>'account', :action=>'edit'})
assert_equal flash[:error], "There was an error deleting your credit card information. Please try again."
end

Session was not persisted after redirect in rails 5

I try to save my variable in an action to call it from another action in my controller. I use session to save it like below:
def upload_with_dropzone
if params[:document]
#resume = #p.documents.new(document_params)
if verify_recaptcha(model: #resume)
#resume.save
session[:resume_id] = #resume.id
redirect_to prospect_upload_path
else
flash[:error] = "Cannot upload image"
redirect_to prospect_upload_path
end
end
In other action I call session[:resume_id] but it return nil
def upload_resume
if session[:resume_id].present?
#resume = Document.find(session[:resume_id])
elsif params[:interaction] && (1..5).include?(params[:interaction][:interaction_rating].to_i)
#resume = #p.documents.last
#itr.update(interaction_feedback_params)
else
#resume = #p.documents.new
end
end
Below is my routes.rb:
match "/upload" => "upload#upload_resume", via: [:get, :post]
post "/upload_resume" => "upload#upload_via_dropzone"
Something was wrong, please give me an idea!
I put byebug after session[:resume_id] = #resume.id line, and I debug the session[:resume_id]

Set value to belongs_to attributes in rails create action

I'm trying to set the title of a Page in my create action.
I would need to page.translation.title = params[:page][:title]
def create
#page = Page.new(params[:page])
#page.translation.title = params[:page][:title]
if #page.save
redirect_to admin_pages_path
else
render :new
end
end
Also tried
#translation = #page.translation.build(title: params[:page][:title])
from the console when I run:
p = Page.last
p.translation.title
=> nil -----> right now after its created, title is nil.
p.translation.title = "foo"
=> "foo"
This is what I what in my create action. any help would be greatly
appreciated. Thank you.
Update:
I'm using this on a legacy application that's running on refinerycms 2.1.0.dev
Relevant code:
https://github.com/DynamoMTL/g-refinerycms/blob/244d4a89aef4ad31aed9c50a0ca4c8a2ffd3d1ac/pages/app/models/refinery/page_part.rb#L10
https://github.com/DynamoMTL/g-refinerycms/blob/244d4a89aef4ad31aed9c50a0ca4c8a2ffd3d1ac/pages/app/models/refinery/page.rb#L45-L49
https://github.com/DynamoMTL/g-refinerycms/blob/244d4a89aef4ad31aed9c50a0ca4c8a2ffd3d1ac/pages/app/models/refinery/page.rb#L11-L14
Solution
def create
#page = Refinery::Page.new(params[:page])
if #page.save!
#page.translations.create(slug: #page.slug,
title: params[:page][:title],
locale: params[:switch_locale])
flash.notice = t(
'refinery.crudify.created',
what: "'#{#page.title}'"
)
redirect_to admin_pages_path
else
render :new
end
end

Getting AbstractController::DoubleRenderError:

def purchase
...
perform_payment_post
redirect_to :action => 'billing'
...
end
def perform_payment_post
params[:coverages] ||= {}
params[:customer][:coverage_addon] = (params[:coverages].collect { |k,v| k }).join(', ')
params[:customer][:coverage_ends_at] = 1.year.from_now
Rails.logger.info("--- id = #{cookies.signed[:incomplete_gaq_customer_id]}")
id = cookies.signed[:incomplete_gaq_customer_id]
return redirect_to :action => #is_affiliate_user ? 'affiliate':'quote' if id.nil?
#customer = Customer.find(cookies.signed[:incomplete_gaq_customer_id])
return redirect_to :action => 'please_call' if #customer.status_id != 0
#customer.update_attributes(params[:customer])
#customer.notes.create({ :notes_text => #note }) if #note
if params[:property_id].to_i == 0 then #customer.properties.create(params[:property]) end
end
Getting Error on purchase method on line redirect_to :action => "billing".
AbstractController::DoubleRenderError: Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
Please Help Me.
If you want to return early in controller you either need to write it like this
redirect_to(:action => #is_affiliate_user ? 'affiliate':'quote') and return if id.nil?

lost session after redirect_to

Newly added description: (sorry for not mentioning)
The ApplicationController.current_account is defined as:
class ApplicationController < ActionController::Base
class << self
def current_account
#current_account
end
def current_account=(value)
#current_account = value
end
end
=========
I encountered a strange performance in my current project, which is about session. The strange part is it was normal in Safari but failed in other browsers (includes chrome, firefox and opera).
There is a registration form for input of part of the key information (email, password, etc) and is submitted to an action called "create"
This is the basic code of create action:
#account = Account.new(params[:account])
if #account.save
ApplicationController.current_account = #account
session[:current_account] = ApplicationController.current_account
session[:account] = ApplicationController.current_account.id
email = #account.email
Mailer.deliver_account_confirmation(email)
flash[:type] = "success"
flash[:notice] = "Successfully Created Account"
redirect_to :controller => "accounts", :action => "create_step_2"
else
flash[:type] = "error"
flash[:title] = "Oops, something wasn't right."
flash[:notice] = "Mistakes are marked below in red. Please fix them and resubmit the form. Thanks."
render :action => "new"
end
Also I created a before_filter in the application controller, which has the following code:
ApplicationController.current_account = Account.find_by_id(session[:current_account].id) unless session[:current_account].blank?
For Safari, there is no any problem. But for the other browsers, the session[:current_account] does not exist and so produced the following error message:
RuntimeError in AccountsController#create_step_2
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Please could anyone help me?
1] don't write
ApplicationController.current_account
Just
current_account
2] in your application_controller
def current_account
session[:current_account]
end
3]
ApplicationController.current_account = #account
session[:current_account] = ApplicationController.current_account
session[:account] = ApplicationController.current_account.id
should be
session[:current_account] = #account
session[:account] = #account.id

Resources