link_to delete method takes me to 'show' page on rails - ruby-on-rails

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)

Related

Turbo-frame with Devise, after a successful update user, on page reload the turboframe does not render the appropriate html

I have a strange problem with Devise that I was not able to figure out after several hours so I decided to bring it up to you to have a little help on the mater.
I have a tab panel which uses a turbo_frame_tag to render different tabs that have forms inside them. On form submission if they are errors it works perfectly, the turbo frame is rerendered with the form and its errors associated. However, on a successful form submission for the Devise form (here editing the user), it reloads the page but it does not render the content of the turbo frame whereas my rails server says it is rendering the appropriate html :sweat_smile:
I am giving you some code details below to help you understand:
– Where I display my tab content –
GET /settings (users#settings, users/settings.html.slim)
section { ... }
= render "shared/tab/navigation",
= turbo_frame_tag "tab_content", src: edit_user_registration_path
I put src: edit_user_registration_path because that it my first tab, it does not work with or without it.
I have a user edit form on my first tab, the issue is only for the Devise tab, for the other controllers/actions it works fine. Here for my user registration#edit (I curated the html for better understanding):
– The view I want to display in my turbo frame –
GET /settings/notifications (hairdressers#notifications_settings, hairdressers/notifications_settings.html.slim)
= turbo_frame_tag "tab_content"
section
= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, class: "..." }) do |f|
# some inputs
= button_tag type: "submit",
class: "...",
data: { disable_with: "Submitting..." }
– The controller responsible for handling the request and displaying the view –
My Devise user#registrations controller:
class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
layout "sign_in_up", except: %i[edit update]
# GET /resource/sign_up
# def new
# super
# end
# POST /resource
# def create
# super
# end
# GET /resource/edit
# def edit
# super
# end
# PUT /resource
# def update
# super
# end
# DELETE /resource
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end
# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end
# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
# The path used after sign up for inactive accounts (ie not activated yet accounts)
# That's our 'after_sign_up_path_for' because we ask users to activate their
# account on sign up
def after_inactive_sign_up_path_for(resource)
users_welcome_path(resource_id: resource.id)
end
# CUSTOM: The path used after successful update.
def after_update_path_for(resource)
users_settings_path
end
end
So, when I successfully update my informations as a user with this form form, it redirects me properly to the /settings routes, however it does not display the content of my turbo_frame_tag (here registrations#edit) BUT my rails server says so …
Started PUT "/users" for ::1 at 2022-08-24 19:30:45 +0200
Processing by Users::RegistrationsController#update as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"first_name"=>"Ulysse35", "last_name"=>"Benard1", "email"=>"mathieu#madeinvote.com", "phone_number"=>"06 59 86 98 64", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
TRANSACTION (0.2ms) BEGIN
User Update (7.9ms) UPDATE "users" SET "updated_at" = $1, "first_name" = $2 WHERE "users"."id" = $3 [["updated_at", "2022-08-24 17:30:45.644398"], ["first_name", "Ulysse35"], ["id", 1]]
TRANSACTION (6.7ms) COMMIT
Redirected to http://localhost:3000/settings
Completed 302 Found in 397ms (ActiveRecord: 15.7ms | Allocations: 7481)
Started GET "/settings" for ::1 at 2022-08-24 19:30:45 +0200
Processing by UsersController#settings as TURBO_STREAM
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/users_controller.rb:10:in `settings'
Rendering layout layouts/application.html.slim
Rendering users/settings.html.slim within layouts/application
Rendered shared/tab/_link_item.html.slim (Duration: 23.6ms | Allocations: 786)
Rendered shared/tab/_link_item.html.slim (Duration: 1.4ms | Allocations: 781)
Rendered shared/tab/_link_item.html.slim (Duration: 3.8ms | Allocations: 781)
Rendered shared/tab/_navigation.html.slim (Duration: 62.9ms | Allocations: 2577)
Rendered users/settings.html.slim within layouts/application (Duration: 66.0ms | Allocations: 2870)
Rendered shared/buttons/_flat_large_with_icon.html.slim (Duration: 1.1ms | Allocations: 773)
Rendered shared/buttons/_flat_large_with_icon.html.slim (Duration: 1.1ms | Allocations: 773)
Rendered shared/buttons/_flat_large_with_icon.html.slim (Duration: 1.0ms | Allocations: 773)
Rendered shared/buttons/_primary_compact_with_icon.html.slim (Duration: 2.4ms | Allocations: 772)
Rendered shared/buttons/_primary_large_with_icon.html.slim (Duration: 0.5ms | Allocations: 773)
Rendered shared/dropdown/_link_item.html.slim (Duration: 0.0ms | Allocations: 45)
Rendered shared/dropdown/_link_item.html.slim (Duration: 0.0ms | Allocations: 45)
Rendered shared/dropdown/_link_item.html.slim (Duration: 0.0ms | Allocations: 45)
Rendered shared/dropdown/_menu.html.slim (Duration: 0.3ms | Allocations: 363)
Rendered shared/_navbar.html.slim (Duration: 8.7ms | Allocations: 5647)
Rendered shared/_footer.html.slim (Duration: 1.1ms | Allocations: 21)
Rendered layout layouts/application.html.slim (Duration: 77.9ms | Allocations: 9322)
Completed 200 OK in 85ms (Views: 78.4ms | ActiveRecord: 0.2ms | Allocations: 10645)
Started GET "/users/edit" for ::1 at 2022-08-24 19:30:45 +0200
Processing by Users::RegistrationsController#edit as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering layout layouts/application.html.slim
Rendering devise/registrations/edit.html.slim within layouts/application
Rendered devise/shared/_password_field.html.slim (Duration: 1.0ms | Allocations: 896)
Rendered devise/shared/_password_field.html.slim (Duration: 1.0ms | Allocations: 897)
Rendered devise/shared/_password_field.html.slim (Duration: 0.5ms | Allocations: 895)
Rendered shared/buttons/_submit_primary_large.html.slim (Duration: 1.7ms | Allocations: 779)
Rendered devise/registrations/edit.html.slim within layouts/application (Duration: 7.2ms | Allocations: 5268)
Rendered shared/buttons/_flat_large_with_icon.html.slim (Duration: 0.6ms | Allocations: 773)
Rendered shared/buttons/_flat_large_with_icon.html.slim (Duration: 2.0ms | Allocations: 773)
Rendered shared/buttons/_flat_large_with_icon.html.slim (Duration: 0.5ms | Allocations: 773)
Rendered shared/buttons/_primary_compact_with_icon.html.slim (Duration: 0.4ms | Allocations: 772)
Rendered shared/buttons/_primary_large_with_icon.html.slim (Duration: 2.0ms | Allocations: 773)
Rendered shared/dropdown/_link_item.html.slim (Duration: 0.0ms | Allocations: 45)
Rendered shared/dropdown/_link_item.html.slim (Duration: 0.0ms | Allocations: 45)
Rendered shared/dropdown/_link_item.html.slim (Duration: 0.0ms | Allocations: 45)
Rendered shared/dropdown/_menu.html.slim (Duration: 0.2ms | Allocations: 363)
Rendered shared/_navbar.html.slim (Duration: 6.5ms | Allocations: 5647)
Rendered shared/_footer.html.slim (Duration: 0.0ms | Allocations: 21)
Rendered layout layouts/application.html.slim (Duration: 14.2ms | Allocations: 11710)
Completed 200 OK in 17ms (Views: 14.6ms | ActiveRecord: 0.2ms | Allocations: 13505)
and it seems that I receive well the turbo_frame response, just that it is not inserted properly, but probably I am missing something here
Any suggestion to solve this issue would be appreciated, do not hesitate to ask for further information …
If anyone gets the same problem someday, here is a clean solution I found to make it work ;)
Just edit your registrations#edit and registrations#update actions with something like:
def edit
render turbo_stream: turbo_stream.replace("tab_content", partial: "devise/registrations/edit", locals: { resource: resource } )
end
Do not forget to transform your devise/views/registration/edit view into a _edit partial, anyway it is what it was in my case.
By making your edit action responding with a turbo_stream you will be assured that it gets replaced accordingly.

rails implementing ransack search - routing configuration

Fairly new rails developer, and still trying to get my head around where things go and how to connect them.
I have a database of 'records', and am looking to search them. I found the ransack gem, which does this, however I don't want to put the search on the index page, I want a seperate page for the search and it's results.
I created a new action in the records controller:
def search
#q = Record.ransack(params[:q])
#found_records = #q.result(distinct: true)
end
and then the search.html.erb view, then the route:
resources :records do
match :search, to: 'records#search', on: :collection, via: [:get, :post]
end
and then the view itself
<%= search_form_for(
#q,
url: search_records_path,
html: { method: :post }
) do |f| %>
<%= f.label :brief %>
<%= f.search_field :brief %>
<%= f.submit %>
<% end %>
<div id="records">
<% #found_records.each do |record| %>
<%= render record %>
<% end %>
</div>
and this runs without errors, but when I press the search box the page just refreshes, with no search performed.
I guess this is a routing issue, but now sure how to set the route used by the search button? Any advice here much appreciated!
--edit
The log looks good to me, here is what is logged on the console.
Started POST "/records/search" for 127.0.0.1 at 2022-08-09 05:35:52 +0800
Processing by RecordsController#search as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "q"=>{"brief"=>"rain"}, "commit"=>"Search"}
Rendering layout layouts/application.html.erb
Rendering records/search.html.erb within layouts/application
Record Load (0.1ms) SELECT DISTINCT "records".* FROM "records"
↳ app/views/records/search.html.erb:20
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 49)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 47)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 48)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 47)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 49)
Rendered records/search.html.erb within layouts/application (Duration: 4.5ms | Allocations: 1984)
Rendered layouts/_shim.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layouts/_header.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layout layouts/application.html.erb (Duration: 24.0ms | Allocations: 7469)
Completed 200 OK in 26ms (Views: 24.7ms | ActiveRecord: 0.1ms | Allocations: 8216)
-- update --
Changing the search to a 'get' fixed the issue, and making sure to use #q and not a custom name without changing the ransack config to match.

Rails devise sign up, sign in not working

I am new to rails and just tried to setup user sign up, sign through devise but none of the sign up and sign in are working, when I click on sign up the page just keeps unchanged, it does not even show error messages.
I just added this in my application.html.erb file for errors
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<%- end -%>
Console messages
Started POST "/users" for ::1 at 2022-02-18 21:23:31 +0530
Processing by Devise::RegistrationsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"ry201#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.1ms) SELECT sqlite_version(*)
TRANSACTION (0.2ms) begin transaction
User Exists? (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "ry201#gmail.com"], ["LIMIT", 1]]
TRANSACTION (0.1ms) rollback transaction
Rendering layout layouts/application.html.erb
Rendering devise/registrations/new.html.erb within layouts/application
Rendered devise/shared/_error_messages.html.erb (Duration: 0.9ms | Allocations: 462)
Rendered devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 93)
Rendered devise/registrations/new.html.erb within layouts/application (Duration: 4.2ms | Allocations: 2047)
Rendered home/_header.html.erb (Duration: 0.2ms | Allocations: 128)
Rendered layout layouts/application.html.erb (Duration: 13.5ms | Allocations: 4452)
Completed 200 OK in 216ms (Views: 15.0ms | ActiveRecord: 1.3ms | Allocations: 10722)

Rails Admin render exceptionally slow in production

I am running Rails Admin, with Rails API in production with docker. For some reason, the rendering is extremely slow. A simple page load takes 15 seconds, it seems the rendering of the layouts which is taking an exceptionally long time.
Am I forgetting to compile assets somewhere in the docker build process, that is causing this?
User Load (18.8ms) SELECT "users".* FROM "users" ORDER BY users.id desc LIMIT $1 OFFSET $2 [["LIMIT", 20], ["OFFSET", 0]]
(20.8ms) SELECT COUNT(*) FROM "users"
Rendered /usr/local/bundle/gems/rails_admin-2.0.2views/rails_admin/main/index.html.haml within layouts/rails_admin/application (Duration: 1559.2ms | Allocations: 49878)
Rendered /usr/local/bundle/gems/rails_admin-2.0.2views/layouts/rails_admin/_head.html.haml (Duration: 0.5ms | Allocations: 190)
Rendered /usr/local/bundle/gems/rails_admin-2.0.2views/layouts/rails_admin/_secondary_navigation.html.haml (Duration: 0.5ms | Allocations: 269)
Rendered /usr/local/bundle/gems/rails_admin-2.0.2views/layouts/rails_admin/_navigation.html.haml (Duration: 3.2ms | Allocations: 469)
Rendered /usr/local/bundle/gems/rails_admin-2.0.2views/layouts/rails_admin/_sidebar_navigation.html.haml (Duration: 35.2ms | Allocations: 17042)
Rendering /usr/local/bundle/gems/rails_admin-2.0.2views/layouts/rails_admin/pjax.html.haml
Rendered /usr/local/bundle/gems/rails_admin-2.0.2views/layouts/rails_admin/pjax.html.haml (Duration: 12.1ms | Allocations: 1683)
Completed 200 OK in 2302ms (Views: 1590.1ms | ActiveRecord: 120.6ms | Allocations: 77826)
According to this issue on Github, you could try disabling statistics like so
RailsAdmin.config do |c|
c.actions do
dashboard do
statistics false
end
end
end
Full documentation of this is here.

Back Button Skipping Page in Ruby on Rails application

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.

Resources