Destroyed object still there? - ruby-on-rails

The "destroy" method is not working on my site. I created a "delete" link, but when I click it I just get redirected.
Here is the controller with the destroy method:
class PostsController < ApplicationController
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post
else
render :new, status: :unprocessable_entity
end
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
redirect_to #post
else
render :edit, status: :unprocessable_entity
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to root_path, status: :see_other
end
private
def post_params
params.require(:post).permit(:title, :description, :image, :price)
end
end
Nothing fancy. And here is the link in my posts/show.html.erb view:
<%= link_to 'Delete', root_path,
method: :delete,
data: { confirm: 'Are you sure?' } %>
Doesn't work - I just get redirected to the root path, and the post I tried to delete remains.
I tried removing a post in the rails console, and that did work.
Rails 7.0.4
Ruby 3.1.2
Server output after clicking "delete" link on local site:
Started GET "/posts/3" for ::1 at 2022-10-09 23:59:02 -0400
ActiveRecord::SchemaMigration Pluck (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by PostsController#show as HTML
Parameters: {"id"=>"3"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
↳ app/controllers/posts_controller.rb:7:in `show'
Rendering layout layouts/application.html.erb
Rendering posts/show.html.erb within layouts/application
ActiveStorage::Attachment Load (0.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 3], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]]
↳ app/views/posts/show.html.erb:11
ActiveStorage::Blob Load (0.1ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/views/posts/show.html.erb:12
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 3]]
↳ app/views/posts/show.html.erb:23
Rendered collection of templates [0 times] (Duration: 0.0ms | Allocations: 35)
Rendered comments/_form.html.erb (Duration: 31.1ms | Allocations: 6334)
Rendered posts/show.html.erb within layouts/application (Duration: 142.9ms | Allocations: 31474)
Rendered layout layouts/application.html.erb (Duration: 300.1ms | Allocations: 53293)
Completed 200 OK in 402ms (Views: 312.5ms | ActiveRecord: 2.1ms | Allocations: 66385)
Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhiR28xZVRKamJucHhZM0pxZFhaak9YZGxZelY0ZVdwbGREWnlNZ1k2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpWldsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW1sc1h6RTFPRGg0VGk0ek1qSTNNRGc0TlRZMVh6SmlkWGd1Y0c1bklqc2dabWxzWlc1aGJXVXFQVlZVUmkwNEp5ZHBiRjh4TlRnNGVFNHVNekl5TnpBNE9EVTJOVjh5WW5WNExuQnVad1k3QmxRNkVXTnZiblJsYm5SZmRIbHdaVWtpRG1sdFlXZGxMM0J1WndZN0JsUTZFWE5sY25acFkyVmZibUZ0WlRvS2JHOWpZV3c9IiwiZXhwIjoiMjAyMi0xMC0xMFQwNDowMTo0My45NDVaIiwicHVyIjoiYmxvYl9rZXkifX0=--00b620d927983a0cba2300d10b1e2234b9306a84/il_1588xN.3227088565_2bux.png" for ::1 at 2022-10-09 23:59:03 -0400
Processing by ActiveStorage::DiskController#show as PNG
Parameters: {"encoded_key"=>"[FILTERED]", "filename"=>"il_1588xN.3227088565_2bux"}
Completed 304 Not Modified in 7ms (ActiveRecord: 0.0ms | Allocations: 559)

Looks like you have an issue with Turbo or Rails UJS.
These should really work and send a DELETE request via javascript:
# Turbo
link_to "delete", #post, data: { turbo_method: :delete }
# Rails UJS
link_to "delete", #post, method: :delete
If your javascript is broken than data and method attributes are ignored and you're just clicking a link to show #post, which is what's happening in the log.
On the other hand:
data-turbo-method changes the link request type from the default GET.
Ideally, non-GET requests should be triggered with forms, but
data-turbo-method might be useful where a form is not possible.
https://turbo.hotwired.dev/reference/attributes
To trigger a DELETE request with a form you can use button_to, which creates a little form and rails correctly handles routing to destroy action:
button_to "Delete", #post, method: :delete
https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

Related

The action 'update' could not be found for Api::UsersController:

I am sending an AJAX request from my frontend to rails for a patch request but getting an action cannot be found error.
So far, I have double checked my routes and controller to make sure there were no mistakes. My create action is working fine which is in the users controller. Ive also double checked my AJAX request to make sure it is going to the correct url. My AJAX request is also for an AWS upload if that helps.
Here are my routes
namespace :api, defaults: {format: :json} do
resources :users, only: [:create, :update, :show]
resource :session, only: [:create, :destroy, :show]
end
root "static_pages#root"
end
here is my controller with the update action
class Api::UsersController < ApplicationController
def create
#user = User.new(user_params)
if #user.save
login(#user)
render "api/users/show"
else
render json: #user.errors.full_messages, status: 422
end
def show
#user = User.find(params[:id])
render "api/users/show"
end
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
render "api/users/show"
else
render json: #user.errors.full_messages
end
end
end
def user_params
params.require(:user).permit(:email, :password, :first_name, :last_name, :DOB, :gender, :prof_photo, :cover_photo)
end
end
Update: ajax request for AWS user photo upload.
export const updateUser = (userId, formData) => {
return $.ajax({
method: "PATCH",
url: `api/users/${userId}`,
data: formData,
contentType: false,
processData: false
})
}
Finally here are the server logs
AbstractController::ActionNotFound - The action 'update' could not be found for Api::UsersController:
Started GET "/" for ::1 at 2019-10-06 07:49:03 -0400
Processing by StaticPagesController#root as HTML
Rendering static_pages/root.html.erb within layouts/application
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."session_token" = $1 LIMIT $2 [["session_token", "mRbshUiTIXjVd3LpZSiWvA"], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:10
ActiveStorage::Attachment Load (0.7ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 2], ["record_type", "User"], ["name", "prof_photo"], ["LIMIT", 1]]
↳ app/views/api/users/_user.json.jbuilder:3
ActiveStorage::Blob Load (1.1ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/views/api/users/_user.json.jbuilder:3
Rendered api/users/_user.json.jbuilder (7.2ms)
Rendered static_pages/root.html.erb within layouts/application (13.8ms)
Completed 200 OK in 93ms (Views: 86.5ms | ActiveRecord: 2.8ms)
Started PATCH "/api/users/2" for ::1 at 2019-10-06 07:55:37 -0400
AbstractController::ActionNotFound - The action 'update' could not be found for Api::UsersController:````
Found the rookie mistake! The end statement for create action was all the way at the bottom (hence only my create action was working) Make sure to check your end statements!

Why isn't my form saving but working with the API only?

I have my form to save to my Stripe_account table. I recently nested the resources and now the form won't save to my database tables. I have it still working with the Stripe API and working there though.
What in my code is lacking?
User Model:
has_one :stripe_account
Stripe_account Model:
belongs_to :users
Stripe_account controller:
def new
#stripe_account = StripeAccount.new
#user = User.find(params[:user_id])
end
def create
#stripe_account = StripeAccount.new(stripe_account_params)
#user = User.find(params[:user_id])
acct = Stripe::Account.create({
.....
.....
#stripe_account.id = current_user.id
#stripe_account.acct_id = acct.id
respond_to do |format|
# #user = User.find(params[:id])
if #stripe_account.save
# current_user = #user
#user.stripe_account = acct.id
format.html { redirect_to new_bank_account_path, notice: 'Stripe account was successfully created.' }
format.json { render :show, status: :created, location: #stripe_account }
else
format.html { render :new }
format.json { render json: #stripe_account.errors, status: :unprocessable_entity }
end
end
end
View:
<%= form_for ([#user, #stripe_account]) do | f | %>
Routes:
resources :users do
resources :stripe_accounts
end
#added for testing
get 'stripe_' => "stripe_account#create"
get 'stripe_new' => "stripe_account#new"
Here's my routes maybe can help?: https://pastebin.com/RVWd2Qq9
Now even though I don't have the "bankaccount" controller or models set up correctly yet, shouldn't it be at least attempting to go there and saving the stripe_account? Just making sure that's not the issue. But it appears it's failing because a new form reloads.
The API is successfully going through as well and the accounts are appearing within stripe, just not my own database.
What in my programming is wrong?
Update to add cmd response:
Started POST "/users/2/stripe_accounts" for 127.0.0.1 at 2018-11-10 00:11:26 -0500
Processing by StripeAccountsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"nz1234567890iJuFwsm/Z4ylhE6zoGdWN6QCfWtDZTH1sxZu/WCdWMKBGkc4zoZ2dOgk9c8UDwRzgqdxrT/sA==", "stripe_account"=>{"account_type"=>"individual", "business_name"=>"", "business_tax_id"=>"", "first_name"=>"Dill", "last_name"=>"Pickles", "ssn_last_4"=>"1234", "dob_month"=>"3", "dob_day"=>"4", "dob_year"=>"1917", "address_line1"=>"198 berry avenue", "address_city"=>"san fran", "address_state"=>"CA", "address_postal"=>"90213", "tos"=>"1", "id"=>"2"}, "full_account"=>"{:value=>\"true\"}", "button"=>"", "user_id"=>"2"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ /home/bob/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/stripe_accounts_controller.rb:49
(0.1ms) begin transaction
↳ app/controllers/stripe_accounts_controller.rb:91
(0.1ms) rollback transaction
↳ app/controllers/stripe_accounts_controller.rb:91
Rendering stripe_accounts/new.html.erb within layouts/application
Rendered stripe_accounts/_account_form.html.erb (9.4ms)
Rendered stripe_accounts/new.html.erb within layouts/application (12.5ms)
Rendered layouts/_navbar.html.erb (1.9ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 3202ms (Views: 190.0ms | ActiveRecord: 2.4ms)
Validation failed: User must exist
You can either use optional :true to resolve the error
#stipe_account.rb
belongs_to :user, optional: true
#^ should be singular
OR
Assign the user_id in the create action like so
#stripe_account.user_id = current_user.id #add this line
Update:
undefined method `user_id=' for StripeAccount:0x001234f4c58692ae8 Did
you mean? user="
The error is because you don't have user_id column in stripe_accounts table. Generate a migration that will do the job for you
rails g migration add_user_id_to_stripe_accounts user_id:integer
and do rails db:migrate

Heroku throws rollback errors while creating book record

I am currently building a application where I have a Book Model.
It works locally without any problems at all. I deployed it to Heroku and ran rake db:migrate and tried to create a book
I got following ROLLBACK error.
2018-03-29T07:29:59.748305+00:00 app[web.1]: D, [2018-03-29T07:29:59.748238 #4] DEBUG -- : [feda9269-2f19-4916-a87f-bc179fd52bec] User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 3], ["LIMIT", 1]]
2018-03-29T07:29:59.750826+00:00 app[web.1]: D, [2018-03-29T07:29:59.750763 #4] DEBUG -- : [feda9269-2f19-4916-a87f-bc179fd52bec] (0.8ms) BEGIN
2018-03-29T07:29:59.763631+00:00 app[web.1]: D, [2018-03-29T07:29:59.763526 #4] DEBUG -- : [feda9269-2f19-4916-a87f-bc179fd52bec] (0.9ms) ROLLBACK
2018-03-29T07:29:59.764270+00:00 app[web.1]: I, [2018-03-29T07:29:59.764208 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendering books/new.html.erb within layouts/application
2018-03-29T07:29:59.770061+00:00 app[web.1]: I, [2018-03-29T07:29:59.769996 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered books/_form.html.erb (5.5ms)
2018-03-29T07:29:59.770167+00:00 app[web.1]: I, [2018-03-29T07:29:59.770113 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered books/new.html.erb within layouts/application (5.8ms)
2018-03-29T07:29:59.771544+00:00 app[web.1]: I, [2018-03-29T07:29:59.771486 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered layouts/_navbar.html.erb (0.7ms)
2018-03-29T07:29:59.771904+00:00 app[web.1]: I, [2018-03-29T07:29:59.771828 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Rendered layouts/_alerts.html.erb (0.2ms)
2018-03-29T07:29:59.772181+00:00 app[web.1]: I, [2018-03-29T07:29:59.772125 #4] INFO -- : [feda9269-2f19-4916-a87f-bc179fd52bec] Completed 200 OK in 28ms (Views: 8.2ms | ActiveRecord: 3.4ms)
Books Controller
class BooksController < ApplicationController
before_action :find_book, only: [:show, :edit, :update, :destroy]
before_action :book_owner, only: [:destroy, :edit, :update]
def index
#books = Book.all.order("created_at DESC")
end
def show
end
def new
#book = current_user.books.build
#categories = Category.all.map{ |c| [c.name, c.id] }
end
def create
#book = current_user.books.build(book_params)
#book.category_id = params[:category_id]
if #book.save
redirect_to books_path
else
render 'new'
end
end
def edit
#categories = Category.all.map{ |c| [c.name, c.id] }
end
def update
#book.category_id = params[:category_id]
if #book.update(book_params)
redirect_to book_path(#book)
else
render 'edit'
end
end
def destroy
#book.destroy
redirect_to root_path
end
private
def book_params
params.require(:book).permit(:title, :description, :author, :category_id)
end
def find_book
#book = Book.find(params[:id])
end
def book_owner
unless current_user.id == #book.user_id
flash[:notice] = "You are not allowed to do that!"
redirect_to #book
end
end
end
Book Model
class Book < ApplicationRecord
belongs_to :user
belongs_to :category
end
Book new view
<%= simple_form_for #book, :html => { :multipart => true } do |f| %>
<%= f.input :category_id, collection: #categories, prompt: "Select a category" %>
<%= f.input :title, label: "Book Title" %>
<%= f.input :author %>
<%= f.input :description %>
<%= f.button :submit, :class => "btn-outline-primary" %>
<% end %>
Update:
Started POST "/books" for 127.0.0.1 at 2018-03-29 11:13:22 +0200
Processing by BooksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UYKPhHA99zg5TMMwgKGk+h2OPPaCQkpkjohPRnRNQHlJB1eQF4Ooro4Tvx9VDSr3U6/H4AGoThu8jzkrwyMbUB==", "book"=>{"category_id"=>"", "title"=>"tesas", "author"=>"asdasd", "description"=>"asdasd"}, "commit"=>"Create Book"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
(0.3ms) ROLLBACK
Rendering books/new.html.erb within layouts/application
Rendered books/_form.html.erb (10.0ms)
Rendered books/new.html.erb within layouts/application (11.9ms)
Rendered layouts/_navbar.html.erb (1.2ms)
Rendered layouts/_alerts.html.erb (0.3ms)
Completed 200 OK in 52ms (Views: 46.3ms | ActiveRecord: 0.7ms)
Category_id is nil because i don't have them set yet on my second laptop because i can't get a Category.connection (Category.create(name: "SOMECATEGORY") using this to create a Category but it won't work without Category.connection)
On my heroku application i was able to do Category.connection and Category.create .. - but even after that it didn't work.
What am I doing wrong? Thanks for your help in advance as always!
You are getting the error because the record is invalid. The reason is you are not properly assigning the category_id.
You need to change params[:category_id] to book_params[:category_id]
params[:category_id] will give you nil and the validation check will fail.
def create
#book = current_user.books.build(book_params)
#book.category_id = book_params[:category_id]
if #book.save
redirect_to books_path
else
render 'new'
end
end
You need to make the same changes for update action as well
Update:
I think i found my error : my user_id was nil when creating via web - i created the same record on console and passed the parameter user_id - voila, works. My user_id parameter somehow don't pass on the heroku rails server but it does on the local machine. No code lines were changed.
How is this even possible?

Displaying form validation errors in rails 5

I am having some difficulty getting my nested rails form to display validation errors in the view
Controller:
class RentersController < ApplicationController
before_action :set_renter, only: [:show, :edit, :update, :destroy]
before_action :get_rental
def get_rental
#rental = Rental.find(params[:rental_id])
end
...
# GET /renters/new
def new
#renter = Renter.new
end
...
def create
#renter = #rental.renters.new(renter_params)
respond_to do |format|
if #renter.save
format.html { redirect_to rental_renters_path(#rental), notice: 'Renters were successfully created.' }
format.json { render :show, status: :created, location: #renter }
else
puts #renter.errors.full_messages
format.html { render :new }
format.json { render json: #renter.errors, status: :unprocessable_entity }
end
end
end
...
end
Model
class Renter < ApplicationRecord
belongs_to :rental
validates :name, presence: { message: "..." }
validates :height, presence: { message: "..." }
validates :weight, presence: { message: "..." }
validates :shoeSize, presence: { message: "..." }
end
_form partial being rendered in View
<div class="rental-forms-container sixteen wide column">
<%= form_for([#rental, #renter], remote: true, :html => { class: "renter-form ui form", id: "base-form" }) do |f| %>
<div class="fields">
...
</div>
<% end %>
</div>
<div class="ui warning message">
...
<ul class="list">
<% #renter.errors.messages.values.each do |message| %>
<% message.each do |m| %>
<li><%= m %></li>
<% end %>
<% end %>
</ul>
</div>
...
<%= link_to 'continue with booking', rental_renters_path, remote: true, class: 'ui teal submit button', id: 'submitRenterForms' %>
</div>
Console
Processing by RentersController#create as JS
Processing by RentersController#index as JS
Parameters: {"utf8"=>"✓", "renter"=>{"name"=>"", "height"=>"", "weight"=>"", "wetsuit_style"=>"Adult Womens", "shoeSize"=>"", "rental_id"=>""}, "rental_id"=>"109"}
Parameters: {"rental_id"=>"109"}
Rental Load (0.3ms) SELECT "rentals".* FROM "rentals" WHERE "rentals"."id" = $1 LIMIT $2 [["id", 109], ["LIMIT", 1]]
Rental Load (5.5ms) SELECT "rentals".* FROM "rentals" WHERE "rentals"."id" = $1 LIMIT $2 [["id", 109], ["LIMIT", 1]]
(0.2ms) BEGIN
(0.1ms) ROLLBACK
Name Let us know the name of each renter so we can customize your experience
Height Let us know the height of each renter so we can properly size your wetsuits
Weight Let us know the weight of each renter so we can properly size your wetsuits
Shoesize Let us know the shoe size of each renter so everyone gets the right surf booties
Rendering renters/new.html.erb within layouts/application
Rendered renters/_form.html.erb (2.4ms)
Rendered renters/new.html.erb within layouts/application (3.7ms)
Rendered shared/_following_menu.html.erb (0.1ms)
Rendered shared/_sidebar_menu.html.erb (0.1ms)
Rendered shared/_menu.html.erb (0.8ms)
Rendered shared/_footer.html.erb (0.5ms)
Completed 200 OK in 106ms (Views: 73.8ms | ActiveRecord: 14.0ms)
(0.6ms) SELECT COUNT(*) FROM "renters" WHERE "renters"."rental_id" = $1 [["rental_id", 109]]
Rendering renters/index.html.erb within layouts/application
Charge Load (0.3ms) SELECT "charges".* FROM "charges" WHERE "charges"."rental_id" = $1 LIMIT $2 [["rental_id", 109], ["LIMIT", 1]]
Rendered rentals/_info.html.erb (11.0ms)
Renter Load (0.3ms) SELECT "renters".* FROM "renters" WHERE "renters"."rental_id" = $1 [["rental_id", 109]]
Rendered charges/_form.html.erb (1.9ms)
Rendered renters/index.html.erb within layouts/application (37.5ms)
Rendered shared/_following_menu.html.erb (0.6ms)
Rendered shared/_sidebar_menu.html.erb (0.5ms)
Rendered shared/_menu.html.erb (1.2ms)
Rendered shared/_footer.html.erb (1.4ms)
Completed 200 OK in 265ms (Views: 134.5ms | ActiveRecord: 9.5ms)
The validation errors are outputting to the terminal but they are not appearing in the view.
I have tried using the flash and session hashes to pass them to the view but to no avail. Any guidance would be greatly appreciated.
When using form_for (or if you're now using form_with) pay attention to whether remote or local is set to true. Local uses the browser's normal submission mechanism, while remote uses Ajax. When using remote (as indicated above), the page will not render in the "normal" fashion, as expected.
The Rails documentation has more details: http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html

Rails 4 / Devise Force User to change password on first login

Im having a weird issue trying to force my users to change their passwords on first login.
My server output is telling me it completed the patch successfully, however when I go to log back into the app its still the old password? I'll post output below.
But first here is my code to make this happen:
#application_controller.rb
# Force PW Change On 1st Login
def after_sign_in_path_for(resource)
if current_user.sign_in_count == 1
edit_passwords_path
else
authenticated_root_path
end
end
#passwords_controller.rb
def edit
#user = current_user
end
def update
if current_user.update_without_password(user_params)
flash[:notice] = "Password updated successfully."
redirect_to authenticated_root_path
else
flash[:alert] = "There was a problem, please try again."
render :edit
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
#passwords form_for
<%= form_for current_user, url: passwords_path do |f| %>
password:<br />
<%= f.password_field :password %><br />
password_confirmation:<br />
<%= f.password_field :password_confirmation %><br />
<br />
<%= f.submit %>
<% end %>
#routes.rb
resource :passwords
The force password is doing everything it is supposed to except actually saving the new passwords.
my server output:
Started PATCH "/passwords" for ::1 at 2016-09-07 02:23:43 -0600
Processing by PasswordsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zUOrOdquBht6uwvjvBkPj2yaO0dCgL+3XGhKo0YV1+W/4rEEiiIRHwwOzRCqvSVeVkAO0M7c73ogcmgNQDq/DQ==", "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update User"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
(0.1ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 5ms (ActiveRecord: 0.7ms)
Started GET "/" for ::1 at 2016-09-07 02:23:43 -0600
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Processing by WelcomeController#index as HTML
Rendering welcome/index.html.erb within layouts/application
Rendered welcome/index.html.erb within layouts/application (0.4ms)
Rendered layouts/navigation/_unassigned.html.erb (0.5ms)
Rendered layouts/messages/_flash_msg.html.erb (0.5ms)
Completed 200 OK in 56ms (Views: 54.9ms | ActiveRecord: 0.0ms)
In PasswordsController#Update change update_without_password to update_with_password:
def update
if current_user.update_with_password(user_params)
flash[:notice] = "Password updated successfully."
redirect_to authenticated_root_path
else
flash[:alert] = "There was a problem, please try again."
render :edit
end
end

Resources