I have found many generic posts suggesting this has to do with redirects. I believe this may be due to how I have a form set up.
On the plans.html.erb page I have a form with four submits, each going to the same place with different params:
<%= form_with url: :affiliate_select_plan, class: "mx-auto" do |f| %>
<!-- Paid Plans -->
<% #plans.each_with_index do |plan, i| %>
<%= f.button 'Select Plan', value: plan[:name], type: 'submit' %>
<% end %>
<% end %>
I have the affiliate_select_plan_path setup in my routes.rb:
devise_scope :affiliate do
post 'affiliate/select_plan', :to => 'affiliates/registrations#select_plan'
end
The form successfully hits the select_plan method in the controller, which redirects it to the new_affiliate_registration_path, passing the needed params.
def select_plan
redirect_to new_affiliate_registration_path(plan: plan_params[:button])
end
The new method in the controller is called, directing the user to the sign up page:
# GET /resource/sign_up
def new
#plan = AffiliatePlan.find_by(nickname: params.permit(:plan)[:plan].downcase)
super
end
From this page, if the back button on the browser is selected, it will bring the user back to the page they were at before being at plans.html.erb.
Could this be related to the redirect_to?
EDIT:
Here are the logs:
Started GET "/" for 127.0.0.1 at 2020-02-25 19:06:02 -0500
Processing by Affiliates::RegistrationsController#plans as HTML
Rendering affiliates/registrations/plans.html.erb within layouts/application
Rendered affiliates/registrations/plans.html.erb within layouts/application (5.2ms)
Rendered layouts/_google_analytics.html.erb (0.5ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 195ms (Views: 194.2ms | ActiveRecord: 0.0ms)
Started POST "/partner/select_plan" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#select_plan as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ck8HGRryriXleQrUjCSKjTrIRLIw273EdSu4WZnFn3kAL1mMmk7jqR1tZgnPniHsMzHFMl81vPBRuvA0/W4uSw==", "button"=>"Local"}
Unpermitted parameters: :utf8, :authenticity_token
Redirected to http://localhost:3000/partners/sign_up?plan=Local
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)
Started GET "/partners/sign_up?plan=Local" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#new as HTML
Parameters: {"plan"=>"Local"}
AffiliatePlan Load (1.2ms) SELECT "affiliate_plans".* FROM "affiliate_plans" WHERE "affiliate_plans"."nickname" = $1 LIMIT $2 [["nickname", "local"], ["LIMIT", 1]]
↳ app/controllers/affiliates/registrations_controller.rb:11
Rendering affiliates/registrations/new.html.erb within layouts/application
Rendered affiliates/registrations/new.html.erb within layouts/application (4.6ms)
Rendered layouts/_google_analytics.html.erb (1.1ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 191ms (Views: 187.6ms | ActiveRecord: 1.2ms)
I have a hunch that this might have to do with form resubmission: Forms and the back button tend to be a bit wonky at times.
However, instead of going more in depth with this, let me point you in another direction. I'm doing this because to me, this looks like a classic case of someone trying to find a solution to the wrong problem. I'm saying this because based on the code and log snippets you've provided, you're jumping through hoops to pass a parameter (in your case the name of a plan) via multiple actions – which, if I'm right, is just unnecessary.
Here's what I would do instead:
<% #plans.each do |plan| %>
<%=
link_to 'Select Plan',
new_affiliate_registration_path(plan: plan.downcase),
class: 'some-button-class
%>
<% end %>
This way, you don't have to mess around in your controllers in any way. Also, since there is no POST request, you won't have any issues with form (re)population and such things.
Related
I'm new to Rails and can't seem to figure this out
Every time I click the link it doesn't delete the article, it just takes me to the show view. Not sure why this is happening. Here is my code for the link:
<%= link_to 'delete', article_path(article.id), method: :delete, data: { confirm: "Are you sure?" } %>
my rails server shows a GET request even though I specified the method (I think):
Started GET "/articles/1" for ::1 at 2020-09-07 10:05:13 -0700
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"1"}
Article Load (0.7ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?
[["id", 1], ["LIMIT", 1]]
↳ app/controllers/articles_controller.rb:46:in `set_article'
Rendering articles/show.html.erb within layouts/application
Rendered articles/show.html.erb within layouts/application (Duration: 4.0ms | Allocations: 321)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_flashMessages.erb (Duration: 0.2ms | Allocations: 18)
Rendered layouts/_navBar.html.erb (Duration: 0.1ms | Allocations: 5)
Completed 200 OK in 123ms (Views: 96.9ms | ActiveRecord: 0.7ms | Allocations: 5344)
I can't figure out why. Any help is appreciated. Thanks!
I had the same issue in Rails 6. It seems that a wrong version of jquery can cause this. Somehow #rails/ujs stopps working correctly if you install the wrong jquery version.
If you have no errors in your code you can try to fix it by deleting the jquery gem and add jquery through yarn (my question concering this topic)
First post, so i'm a newbie in StackOverflow. I'm trying for several days to make appear a Return button on a page form but only on a specific one.
So, I was advised to use backlink to make it appears.
Here's my code from the form where I want the return button
<% if #backlink.present? %>
<div class="spacer30"></div>
<% if #backlink == 'infos' %>
path = membre_path(menu: 'infos')
<% end %>
<% end %>
<%= link_to "Retour", path, class: "btn-rounded btn-turquoise btn-small" %>
Here's my code controller
def edit
super do |user|
puts "TEST PARAMS BACKLINK #{params[:backlink]}"
#backlink = params[:backlink]
end
end
and my route's :
get 'change_password', to: 'users/registrations#edit'
put 'update' => 'users/registrations#update', :as => 'user_registration'
get 'edit_password', to: 'users/registrations#edit', :as => 'user_edit'
So i should have in my log my PUTS 'TEST PARAMS BACKLINK' but nothing appear, only :
Started GET "/change_password.1?backlink=infos" for ::1 at 2017-10-04 10:07:41 +0200
Processing by Users::RegistrationsController#edit as
Parameters: {"backlink"=>"infos"}
User Load (9.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering users/registrations/edit.html.erb within layouts/application
Rendered users/registrations/edit.html.erb within layouts/application (14.4ms)
Rendered shared/_navbar.html.erb (4.0ms)
Rendered shared/_flashes.html.erb (1.1ms)
Completed 200 OK in 231ms (Views: 217.0ms | ActiveRecord: 9.1ms)
Any ideas why it doesn't work?
Many thanks.
I just had to delete some lines, here's what i changed from my registration controller :
def edit
#backlink = params[:backlink]
super
end
This way, it appears exactly the way I wanted to.
Many thanks :)
I have some javascript that displays some data from my database on my webpage. I also have a button that when the user clicks it some logic occurs in the controllers and then I want to have the data displayed changed. However, at the momment when they click the button nothing happens (page is not reloaded). Could you guys help me set that up? Here is what I have so far:
controller:
class QtlsController < ApplicationController
require 'json'
require 'uri'
$rollback = nil
def index
logger.debug "\nrollback is: #{$rollback}\n"
render "index"
end
def rollback
$rollback = params[:version].gsub(/"/,'')
redirect_to :action => :index
end
end
view:
<%- if $rollback.nil? %>
generates one type of table
<%- else %>
generates another type of table
<%- end %>
...some other logic and buttons etc...
jQuery(function() {
jQuery( "#rollback").button();
jQuery( "#rollback").click(function() {
var version = getSelectedText('version_selector');
jQuery.ajax({
type: "POST",
headers: {
'X-Transaction': 'POST Example',
'X-CSRF-Token': jQuery('meta[name="csrftoken"]').attr('content')
},
url: "/qtls/rollback",
data: {version: JSON.stringify(version) },
dataType: 'json',
sucess: function() { alert("Success! Rollbacked"); }
});
});
Here is what is in log files when I go to the page and when I click the rollback button:
Started GET "/qtls" for 10.64.229.59 at Tue Jul 17 16:48:52 -0500 2012
Processing by QtlsController#index as HTML
rollback is:
ROLLBACK IS NIL!!!
Qtl Load (2.0ms) SELECT `qtls`.* FROM `qtls`
Rendered qtls/index.html.erb within layouts/application (404.8ms)
Rendered shared/_user_nav.html.erb (1.3ms)
Rendered shared/_nav.html.erb (1.3ms)
Rendered shared/_footer.html.erb (0.6ms)
Completed 200 OK in 544ms (Views: 540.9ms | ActiveRecord: 2.0ms)
Started POST "/qtls/rollback" for 10.64.229.59 at Tue Jul 17 16:48:57 -0500 2012
Processing by QtlsController#rollback as JSON
Parameters: {"version"=>"\"test1\""}
Redirected to http://10.10.136.244:4000/qtls
Completed 302 Found in 3ms (ActiveRecord: 0.0ms)
Started GET "/qtls" for 10.64.229.59 at Tue Jul 17 16:48:58 -0500 2012
Processing by QtlsController#index as JSON
rollback is: test1
Rendered qtls/index.html.erb within layouts/application (37.7ms)
Rendered shared/_user_nav.html.erb (3.3ms)
Rendered shared/_nav.html.erb (4.3ms)
Rendered shared/_footer.html.erb (2.0ms)
Completed 200 OK in 99ms (Views: 97.4ms | ActiveRecord: 0.0ms)
it's a reaaaaaally bad style to use global variables like $rollback!
if you want to store user-data use your session-object.
use the jquery-rails gem to integrate with jQuery in your views and then use the :remote => :true in your form, so that rails can handle the form-submission.
The problem I'm having seems to be that Devise's authenticate_#{role}! method is hijacking my registration attempt.
Started GET "/client/sign_up" for 127.0.0.1 at 2012-01-14 12:02:52 +0000
Processing by Client::RegistrationsController#new as HTML
Rendered /Users/robertwwhite/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.3/app/views/devise/shared/_links.erb (1.4ms)
Rendered client/registrations/new.html.haml within layouts/application (97.6ms)
Rendered client/_navigation.html.haml (1.6ms)
Rendered shared/_flash_messages.html.haml (0.1ms)
Completed 200 OK in 126ms (Views: 116.4ms | ActiveRecord: 7.2ms)
Started POST "/client" for 127.0.0.1 at 2012-01-14 12:02:58 +0000
Processing by WishesController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vq3wgsQeb4eoxhb3sw2Q2kd4edIoOxIfrzJ/WzJUAn0=", "client"=>{"email"=>"bacon#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Completed 401 Unauthorized in 13ms
Started GET "/client/sign_in" for 127.0.0.1 at 2012-01-14 12:02:58 +0000
Processing by Client::SessionsController#new as HTML
Rendered /Users/robertwwhite/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.3/app/views/devise/shared/_links.erb (1.0ms)
Rendered client/sessions/new.html.haml within layouts/application (16.5ms)
Rendered client/_navigation.html.haml (1.5ms)
Rendered shared/_flash_messages.html.haml (0.3ms)
Completed 200 OK in 60ms (Views: 38.6ms | ActiveRecord: 6.4ms)
I've tried overriding the after_signup_path_for(resource_or_scope) but it seems to be getting ignored.
# app/controllers/application_controller.rb
def after_sign_up_path_for(resource_or_scope)
random_path
end
So as it stands users can't register to the site in the first place. Any ideas?
Have you checked to make sure non of your routes are overriding the default devise routes/methods?
Edited by HaaR for clarity of users with similar problem:
I had the following in my config/routes.rb above my devise_for methods.
match "client" => "wishes#index"
Which was overriding Devise's
devise_for :clients, :path => :client
By moving it below, it gives Devise priority, and still passes the get request to the appropriate controller and action without hijacking the POST requests.
I've been following this Railscast on implementing Active Merchant in a new test app and it was going well, but now that I'm trying to add what I made to my main app its breaking.
When clicking 'add to cart' on my test app, it redirects to the current cart and lists the item as expected.
On my main app clicking the add to cart link redirects to:
http://mainapp.dev/line_items?product_id=1
The line_items controller looks like this:
class LineItemsController < ApplicationController
def create
#product = Product.find(params[:product_id])
#line_item = LineItem.create!(:cart => current_cart, :product => #product, :quantity => 1, :unit_price => #product.price)
flash[:notice] = "Added #{#product.name} to cart."
redirect_to current_cart_url
end
end
The add to cart link looks like this:
<%= link_to "Add to Cart", line_items_path(:product_id => product), :method => :post, :class => "product_actions" %>
edit - logs
Test version on adding an item (working):
Started POST "/line_items?product_id=5" for 127.0.0.1 at 2011-09-01 07:33:27 +0100
Processing by LineItemsController#create as HTML
Parameters: {"authenticity_token"=>"li7gkjksc9MENevuGz7emDwnbB6HrvPAE3CY=", "product_id"=>"5"}
[1m[35mProduct Load (0.4ms)[0m SELECT `products`.* FROM `products` WHERE `products`.`id` = 5 LIMIT 1
[1m[36m (33.1ms)[0m [1mBEGIN[0m
[1m[35mSQL (179.4ms)[0m INSERT INTO `carts` (`created_at`, `purchased_at`, `updated_at`) VALUES ('2011-09-01 06:33:28', NULL, '2011-09-01 06:33:28')
[1m[36m (48.3ms)[0m [1mCOMMIT[0m
[1m[35m (0.2ms)[0m BEGIN
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO `line_items` (`cart_id`, `created_at`, `product_id`, `quantity`, `unit_price`, `updated_at`) VALUES (29, '2011-09-01 06:33:29', 5, 1, 250, '2011-09-01 06:33:29')[0m
[1m[35m (0.5ms)[0m COMMIT
Redirected to http://sell.dev/cart
Completed 302 Found in 1265ms
Started GET "/cart" for 127.0.0.1 at 2011-09-01 07:33:29 +0100
Processing by CartsController#show as HTML
[1m[36mCart Load (0.2ms)[0m [1mSELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 29 LIMIT 1[0m
[1m[35mLineItem Load (0.3ms)[0m SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`cart_id` = 29
[1m[36mProduct Load (0.5ms)[0m [1mSELECT `products`.* FROM `products` WHERE `products`.`id` = 5 LIMIT 1[0m
Rendered carts/show.html.erb within layouts/application (202.1ms)
Rendered layouts/_header.html.erb (0.8ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 368ms (Views: 284.2ms | ActiveRecord: 79.5ms)
The main app version:
Started GET "/line_items?product_id=1" for 127.0.0.1 at 2011-09-01 07:34:59 +0100
Processing by LineItemsController#index as HTML
Parameters: {"product_id"=>"1"}
Rendered line_items/index.html.erb within layouts/application (0.3ms)
Rendered layouts/_header.html.erb (1.3ms)
Rendered layouts/_footer.html.erb (178.4ms)
Completed 200 OK in 218ms (Views: 182.3ms | ActiveRecord: 35.5ms)
I can't figure out why it's wanting to redirect to the line items index and not create, the code is the same.
edit - routes
get "cart" => "carts#show", :as => "current_cart"
resources :orders
resources :line_items
resources :carts
resources :products
resources :order_transactions
edit - taken from my application controller
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def current_cart
if session[:cart_id]
#current_cart ||= Cart.find(session[:cart_id])
session[:cart_id] = nil if #current_cart.purchased_at
end
if session[:cart_id].nil?
#current_cart = Cart.create!
session[:cart_id] = #current_cart.id
end
#current_cart
end
Thanks for any help its much appreciated!
If you add an item to your cart by clicking on the link "Add to cart" then you will need to call the create action in the LineItemsController controller as you have it now.
The last line of that method is
redirect_to current_cart_url
So you are indeed redirecting to the current_cart as you want but you are saying you are not redirecting to the current cart which is nonsense really.
Perhaps you have not yet set up the current_cart_url path or views or something?
I'm am not clear on what your actual problem is
Started GET "/line_items?product_id=1" for 127.0.0.1 at 2011-09-01 07:34:59 +0100
Processing by LineItemsController#index as HTML
Parameters: {"product_id"=>"1"}
Rendered line_items/index.html.erb within layouts/application (0.3ms)
Rendered layouts/_header.html.erb (1.3ms)
Rendered layouts/_footer.html.erb (178.4ms)
Completed 200 OK in 218ms (Views: 182.3ms | ActiveRecord: 35.5ms)
There MUST be something happening after this! ~What is it?
edit - solution
Sorry, I totally missed the obvious.
<%= link_to "Add to Cart", line_items_path(:product_id => product), :method => :post, :class => "product_actions" %>
is issuing a get request NOT a post request as indicated by your log file Started GET "/cart" for 127.0.0.1 at 2011-09-01 07:33:29 +0100
You are adding an item to the cart which means you are changing state on the server which means you should be using a POST request NOT a get (link) request and therefore you should use a form (button_to will give you that form). Unless you want loads of bot's spiders;/crawlers etc... adding stuff to your cart you should use
<%= button_to 'Add to Cart', line_items_path(:product_id => product) %>