Rails 4 CRUD Update Successful but not saved to Database - ruby-on-rails

I am experiencing a really weird issue in my rails 4 app. I've created a CRUD operation called FOO. All sections work as one would expect it to, except for the Update operation.
The FOO model has 4 Attributes:
title
body
author
status
When I load the Update form, the model is loaded into the form as it should be. The update action returns a "SUCCESS" status if a valid form is submitted, but here is the issue: Only the "Title" attribute is ever updated. All the other attributes remain unchanged. If I Update them one-by-one, they also do not change.
Here is my FooController code:
def update
if #foo.validate( params[ :foo ] )
#foo.save
respond_with( #foo, location: root_path )
else
render :edit
end
end
I am using the Reform GEM instead of Strong Params:
class FooForm < ApplicationForm
property :title
property :body
property :author
property :status
end
And I'm loading the #foo instance in my controller like this:
before_action :load_form_instance, only: [ :edit, :update ]
def load_form_instance
#foo = FooForm.new( Foo.find( params[ :id ]) )
end
I have tested overwriting these values from my Rails console and that seems to work just fine.
Does anybody have any idea where things might be going wrong?
UPDATE
Here is my development LOG as per a request below:
Started PATCH "/foo/1" for 127.0.0.1 at 2015-06-04 20:19:10 +0200
[1m[36mUser Load (1.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 1]]
Processing by FooController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yB+BwFSKt62uSQZtY6auHZ4+DbxVdcZZE2VIWzgRAhVX3Hqn7xmdKewcJ39LnSQm0hfkK+7HazGdypyI7noobA==", "foo"=>{"title"=>"This is a test", "body"=>"This is Body 2", "author"=>"Test1", "status"=>"Active"}, "commit"=>"Update Foo", "id"=>"1"}
[1m[35mFoo Load (0.7ms)[0m SELECT "foo".* FROM "foo" WHERE "foo"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36m (0.3ms)[0m [1mSELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'super_admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))[0m [["user_id", 1]]
[1m[35m (0.2ms)[0m BEGIN
[1m[36m (0.2ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/foo
Completed 302 Found in 32ms (ActiveRecord: 1.4ms)

Related

rails app sending too much irrelevant data to json

Desired output: only the data from events, like:
[{"id":600,"title":600,"start":"2018-10-17T15:24:06.000Z","end":"2018-10-17T17:11:06.000Z","color":"green"}
[{"id":601,"title":601,"start":"2019-10-17T15:24:06.000Z","end":"2018-10-17T17:11:06.000Z","color":"green"}
Current output for some reason also gives info from application.html.erb and from application.js and other files:
events/_event.json.jbuilder:
json.id event.id
json.title event.id
json.start event.starts_at
json.end event.ends_at
json.color event.status_color unless event.status_color.blank?
json.url event_url(event, format: :json)
index.json.jbuilder:
json.array! #events, partial: 'events/event', as: :event
Contoller:
class EventsController < ApplicationController
def index
#events = Event.all
##q = Event.ransack(params[:q])
##events = #q.result.includes(:location, :client, :jobs).paginate(:page => params[:page], :per_page => 15).order("created_at DESC")
end end
Console:
Started GET "/events.json" for 143.27.126.62 at 2019-01-31 10:49:24 +0000
Cannot render console from 143.27.126.62! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by EventsController#index as JSON
User Load (0.8ms) SELECT "users".* FROM "users" WHERE (users.tenant_id IS NULL) AND "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/control.rb:102
Tenant Load (0.7ms) SELECT "tenants".* FROM "tenants" INNER JOIN "tenants_users" ON "tenants"."id" = "tenants_users"."tenant_id" WHERE (tenants.tenant_id IS NULL) AND "tenants_users"."user_id" = $1 [["user_id", 1]]
↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/control.rb:69
MILIA >>>>> [change tenant] new: 1 old: 1
MILIA >>>>> [auth_tenant callback]
Tenant Load (0.6ms) SELECT "tenants".* FROM "tenants" WHERE (tenants.tenant_id IS NULL) AND "tenants"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/base.rb:126
CACHE Tenant Load (0.0ms) SELECT "tenants".* FROM "tenants" WHERE (tenants.tenant_id IS NULL) AND "tenants"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/base.rb:126
CACHE Tenant Load (0.0ms) SELECT "tenants".* FROM "tenants" WHERE (tenants.tenant_id IS NULL) AND "tenants"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /usr/local/rvm/gems/ruby-2.5.3/bundler/gems/milia-4c36b46f3f42/lib/milia/base.rb:126
Rendering events/index.json.jbuilder within layouts/application
Event Load (80.7ms) SELECT "events".* FROM "events" WHERE (events.tenant_id = 1)
↳ app/views/events/index.json.jbuilder:1
Rendered events/_event.json.jbuilder (1.1ms)
Rendered events/_event.json.jbuilder (0.3ms)
Rendered events/_event.json.jbuilder (0.2ms)
Rendered events/_event.json.jbuilder (0.2ms)
Rendered events/_event.json.jbuilder (0.3ms)
Maybe it has to do with Rendering events/index.json.jbuilder within layouts/application?
Because I need to write some code, I'm posting this now as an answer:
Are you sure it's doing a /events.json? or it is doing a /events instead? (looking at your screenshot, I think you just typed in /events.json there in the Chrome address bar, because it should include the "hostname" there by default (from AFAIK).
How rails determine what the response format to render is through this doc:
Example:
GET /posts/5.xml | request.format => Mime::XML
GET /posts/5.xhtml | request.format => Mime::HTML
GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first`
Therefore,
GET /events.xml will by "default" render app/views/events.xml.TEMPLATE_ENGINE
GET /events.json will by "default" render app/views/events.json.TEMPLATE_ENGINE
GET /events will by "default" infer from the first value in the "Accept" header. i.e. if I type in this URL in Chrome address bar, looking at Chrome Network tab, Chrome seems to generate the "Accept" header as follows:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
...of which the first value is text/html, and is why Rails by "default" will render app/views/posts.html.TEMPLATE_ENGINE.
^ this is currently my guess why you are seeing an HTML instead, because probably the request you are doing is GET /events instead of the correct one which is GET /events.json. You can still however do GET /events, but you need to modify the "Accept" header to equal to "application/json"
Debugging:
To test my theory above, can you open Chrome "Network Tab" (click HERE if you don't know how to open it). And then do / send a request as normal to your Rails endpoint (dunno maybe you were clicking a button to send the request?). Afterwards, the "Network" tab will be populated. You would see something like below:
As you could see, the url is posts.json which is what it should be. If you are instead seeing events and not events.json, then this is your problem; you need to make sure it's doing an events.json instead, or you can still do events, but update the "Accept" header in the request (as you would see in the screenshot, the value there should be "Accept: application/json"
P.S. all of what I said above is under the assumption that you are not rendering "manually" in the controller-action, and that you are not calling render manually (which will then do Rails defaults and Rails will render implicitly as what I have described in my answer above)
Try to add respont_to :json at the last line of the action method.
class EventsController < ApplicationController
def index
#events = Event.all
resond_to :json # add this line
end
end

Nested resource failing to update

I'm trying to do what I think should be simple: do a simple edit on a single text string field with the default update action. But it just doesn't seem to work, despite many attempts and alterations.
There are no errors and the flash message responds successfully, but information isn't saved to the database at all:
routes.rb
resources :interviews do
resources :invitations do
put :accept
end
end
views/invitations/edit.html.haml
= simple_form_for [#interview, #invitation] do |f|
= f.error_notification
= f.input :testing
= f.submit 'Edit Invitstion', :class => 'button small'
controllers/invitations_controller.rb
def update
#invitation = Invitation.find(params[:id])
#interview = Interview.find(params[:interview_id])
#invitation.update_attributes(invitation_params)
if #invitation.update_attributes(invitation_params)
redirect_to edit_interview_invitation_path(#interview, #invitation), notice: "Your profile has been successfully updated."
else
render action: "edit"
end
end
private
def invitation_params
params.permit(:user_id, :interview_id, :invitation_id, :session_time, :workflow_state, :testing)
end
And here's the log:
Started PATCH "/interviews/3/invitations/7" for ::1 at 2016-05-15 19:01:52 +0800
Processing by InvitationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"o0U5t0yPN0aE2er+DWK0uxqRGyp4ywfdSrEfvwiSQ3UUaOnr3Fd0raFs1IUqVzizKoqxRU0DDpmvysntB9fdhQ==", "invitation"=>{"interview_id"=>"3", "workflow_state"=>"invited", "session_time"=>"", "testing"=>"testtesttest"}, "commit"=>"Edit Invitstion", "interview_id"=>"3", "id"=>"7"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 7]]
Invitation Load (0.2ms) SELECT "invitations".* FROM "invitations" WHERE "invitations"."id" = $1 LIMIT 1 [["id", 7]]
Role Load (0.2ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT 1 [["id", 3]]
Interview Load (0.2ms) SELECT "interviews".* FROM "interviews" WHERE "interviews"."id" = $1 ORDER BY created_at DESC LIMIT 1 [["id", 3]]
CACHE (0.0ms) SELECT "invitations".* FROM "invitations" WHERE "invitations"."id" = $1 LIMIT 1 [["id", "7"]]
CACHE (0.0ms) SELECT "interviews".* FROM "interviews" WHERE "interviews"."id" = $1 ORDER BY created_at DESC LIMIT 1 [["id", "3"]]
Unpermitted parameters: utf8, _method, authenticity_token, invitation, commit, id
(0.1ms) BEGIN
Invitation Exists (0.4ms) SELECT 1 AS one FROM "invitations" WHERE ("invitations"."user_id" = 3 AND "invitations"."id" != 7 AND "invitations"."interview_id" = 3) LIMIT 1
(0.1ms) COMMIT
Redirected to http://localhost:3000/interviews/3/invitations/7/edit
Completed 302 Found in 12ms (ActiveRecord: 1.6ms)
Started GET "/interviews/3/invitations/7/edit" for ::1 at 2016-05-15 19:01:52 +0800
Processing by InvitationsController#edit as HTML
Parameters: {"interview_id"=>"3", "id"=>"7"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 7]]
Invitation Load (0.3ms) SELECT "invitations".* FROM "invitations" WHERE "invitations"."id" = $1 LIMIT 1 [["id", 7]]
Role Load (0.2ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT 1 [["id", 3]]
Interview Load (0.2ms) SELECT "interviews".* FROM "interviews" WHERE "interviews"."id" = $1 ORDER BY created_at DESC LIMIT 1 [["id", 3]]
Rendered invitations/edit.html.haml within layouts/application (6.1ms)
Completed 200 OK in 48ms (Views: 39.1ms | ActiveRecord: 1.6ms)
Check the format of your params object in your logs. Your invitation params are being passed within the params["invitation"] key, yet you're whitelisting and updating your object based on the params in the root params hash.
Also note that your logs are reporting that you're trying to update your invitation with unpermitted params:
Unpermitted parameters: utf8, _method, authenticity_token, invitation, commit, id
You can fix this by simply updating your invitation_params to use params[:invitation] rather than params like so:
def invitation_params
params.require(:invitation).permit(:user_id, :interview_id, :invitation_id, :session_time, :workflow_state, :testing)
end
Also, you might want to consider raising an error if you're trying to update a parameter that's not whitelisted to prevent these sorts of issues in the future.
In your rails config:
config.action_controller.action_on_unpermitted_parameters = :raise

How is Ajax Destroy method for a nested resource is causing its parent resource to be destroyed?

I have resources set up like this:
resources :scoreboards do
resources :teams
end
On my scoreboard#show view page, I have a collection for the team model which generates a div for each team. Beside each team-div is a Delete button which routes to a method in the teams_controller to delete that team.
Here is a list of all the code associated with it:
The Delete Button next to a team
<div>team example</div> <%= link_to "Del", scoreboard_team_path(#scoreboard, team), remote: true, method: :delete, class: "btn btn-primary" %>
The Teams_controller method for the button
def destroy
#scoreboard = Scoreboard.find(params[:scoreboard_id])
#team = #scoreboard.teams.find(params[:id])
#team.destroy
respond_to do |format|
format.html {redirect_to scoreboard_url(#scoreboard)}
format.js
end
end
The destroy.js.erb file
$( "#team_<%=#team.id%>" ).hide();
Now the issue on hand is that whenever I click on a delete button really fast in quick succession(twice or more on one button), all the Ajax delete buttons stop working. This is most likely because the Scoreboard resource the teams were associated to has been deleted because I get the following error in the Scoreboards_Controller:
NoMethodError in ScoreboardsController#show
undefined method `teams' for nil:NilClass
def show
#scoreboard = Scoreboard.find_by_id(params[:id])
#team = #scoreboard.teams.build # new team form on the page
#comment = #scoreboard.comments.new
#schedule = #scoreboard.schedules.build
end
Then when I check my list of Scoreboards, the #Scoreboard resource which the #team was associated to does not exist anymore. Why is that happening?
Edit: Checking the development logs has clarified what is happening.
So I click delete really fast and it destroys the Team associated with that delete button. Since I manage to click the delete button twice before the destroy.js.erb files processes(hides the deleted div), the Teams_Controller#destroy method is run again on the deleted team, however there is nothing to delete, therefore the Teams_Controller#destroy proceeds to redirect to #scoreboard. Now for some reason, the Scoreboards_Controller#destroy executes and deletes the #scoreboard and after that it attempts to redirect again and encounters a routing error because the scoreboard no longer exists.
Here is some of the log for clarification:
Started DELETE "/scoreboards/45/teams/478" for 99.000.000.000 at 2015-12-12 03:54:09 +0000
Processing by TeamsController#destroy as JS
Parameters: {"scoreboard_id"=>"45", "id"=>"478"}
[1m[36mScoreboard Load (0.3ms)[0m [1mSELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1[0m [["id", 45]]
[1m[35mTeam Load (0.2ms)[0m SELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = ? AND "teams"."id" = ? LIMIT 1 [["scoreboard_id", 45], ["id", 478]]
[1m[36m (0.3ms)[0m [1mbegin transaction[0m
[1m[35mSQL (0.4ms)[0m DELETE FROM "teams" WHERE "teams"."id" = ? [["id", 478]]
[1m[36m (10.6ms)[0m [1mcommit transaction[0m
Rendered teams/destroy.js.erb (0.2ms)
Completed 200 OK in 48ms (Views: 28.1ms | ActiveRecord: 11.8ms)
Started DELETE "/scoreboards/45/teams/478" for 99.000.000.000 at 2015-12-12 03:54:09 +0000
Processing by TeamsController#destroy as JS
Parameters: {"scoreboard_id"=>"45", "id"=>"478"}
[1m[35mScoreboard Load (0.3ms)[0m SELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1 [["id", 45]]
[1m[36mTeam Load (0.2ms)[0m [1mSELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = ? AND "teams"."id" = ? LIMIT 1[0m [["scoreboard_id", 45], ["id", 478]]
Redirected to https://score-app-kpauls.c9.io/scoreboards/45
Completed 302 Found in 19ms (ActiveRecord: 0.5ms)
Started DELETE "/scoreboards/45" for 99.000.000.000 at 2015-12-12 03:54:09 +0000
Processing by ScoreboardsController#destroy as JS
Parameters: {"id"=>"45"}
[1m[35mScoreboard Load (0.2ms)[0m SELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1 [["id", 45]]
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 105]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 105]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1[0m [["id", "45"]]
[1m[35m (0.2ms)[0m begin transaction
[1m[36mTeam Load (0.1ms)[0m [1mSELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = ?[0m [["scoreboard_id", 45]]
[1m[35mSQL (0.3ms)[0m DELETE FROM "teams" WHERE "teams"."id" = ? [["id", 479]]
[1m[36mSQL (0.0ms)[0m [1mDELETE FROM "teams" WHERE "teams"."id" = ?[0m [["id", 480]]
[1m[35mSQL (0.1ms)[0m DELETE FROM "teams" WHERE "teams"."id" = ? [["id", 481]]
[1m[36mSQL (0.1ms)[0m [1mDELETE FROM "teams" WHERE "teams"."id" = ?[0m [["id", 482]]
[1m[35mComment Load (0.1ms)[0m SELECT "comments".* FROM "comments" WHERE "comments"."scoreboard_id" = ? [["scoreboard_id", 45]]
[1m[36mSQL (0.1ms)[0m [1mDELETE FROM "scoreboards" WHERE "scoreboards"."id" = ?[0m [["id", 45]]
[1m[35m (14.1ms)[0m commit transaction
Redirected to https://score-app-kpauls.c9.io/scoreboards
Completed 302 Found in 39ms (ActiveRecord: 15.6ms)
After this the program encounters a routing error. I will continue to look into it but if anyone could help find the reason why scoreboards_controller#destroy is being called, it'd be really appreciated.
Update on the Problem:
So I have figured out the problem. I had these two methods in my application_controller file.
rescue_from ActiveRecord::RecordNotFound do
flash[:warning] = 'Resource not found.'
redirect_back_or root_path
end
def redirect_back_or(path)
redirect_to request.referer || path
end
Whenever, I would click on the delete button twice in quick succession, the destroy action would be rerouted to the scoreboard#show page and proceed to call the destroy method for that resource on the second click. This is because the #team which the destroy method was being called for had already been destroyed in the first click therefore requesting a redirect. I did get the flash messages after I refreshed the page to go the home page but dismissed them as relevant at first but they were key to the conclusion.
The code infrastructure looks good, I would recommend looking at the associations and making sure you don't have dependent: :destroy on team belongs_to :scoreboard
--
In regards the problem of multiple "delete" button clicking, the problem looks like you're getting a redirect to the parent resource. I don't have any reason why this would be the case, except that perhaps Rails has an inbuilt set of functionality to load "parent" routes if the child fails.
The way I would tackle the fix is to use conditions:
def destroy
#scoreboard = Scoreboard.find(params[:scoreboard_id])
#team = #scoreboard.teams.find params[:id]
if #team.destroy
respond_to do |format|
format.html {redirect_to scoreboard_url(#scoreboard)} #-> could this be the reason for the redirect?????
format.js
end
else
redirect_to scoreboard_teams_path(#scoreboard), notice: "Team Already Deleted"
end
end
I'd also look at conditioning the #team -- if #team && #team.destroy - I can refactor if you wanted more information.
Doing this will give you a definite flow which can handle exceptions. I think the problem is that when you click the delete button (and the record no longer exists), Rails is unable to handle the exception.
The inbuilt way that Rails comes back with errors is to redirect to the object_path(#object), and show the errors (like you've got in your format.html).
Therefore, I would guess that Rails is trying to take you back to #scoreboard (scoreboard_path(#scoreboard)), and since you have method: :delete, it's running the destroy method for that controller.
To fix it, you'd need to use the conditions above to let Rails know what to do in case of problems.
In your teams_controller in the destroy action, I suggest you change this line:
#team = #scoreboard.teams.find(params[:id])
for
#team = Team.find(params[:id])

User can't delete his own account , Rails 4 + Devise

I have Ruby on Rails 4 application also I am using Devise gem to provide login/register.
Everything works just fine, but users that are logged in can't delete their own account. Any other delete functions work, so Javascript shouldn't be a problem.
In view:
<%= link_to "Delete account", registration_path(resource_name), data: { confirm: "Your profile will be permanently deleted. Are you sure?" }, method: :delete%>
I am overriding default devise Registration controller with this:
class RegistrationsController < Devise::RegistrationsController
clear_respond_to
respond_to :json
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
end
My log file:
Started DELETE "/ru/users" for xx.xxx.xxxx.xx at 2015-05-26 22:30:45 +0300
Processing by RegistrationsController#destroy as HTML
Parameters: {"authenticity_token"=>"dXKjMMmKibIy2LNCrR66JuThjPzqsdtjFMXTCubcFe8=", "locale"=>"ru"}
[1m[36mUser Load (0.3ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 66 ORDER BY `users`.`id` ASC LIMIT 1[0m
[1m[35mCountry Load (0.3ms)[0m SELECT `countries`.* FROM `countries` WHERE `countries`.`id` = 1 LIMIT 1
[1m[36m (0.6ms)[0m [1mSELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (vip_recomend >= '2015-05-26 22:30:45')[0m
[1m[35m (0.4ms)[0m SELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (recomend >= '2015-05-26 22:30:45')
[1m[36m (0.3ms)[0m [1mSELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (highlight >= '2015-05-26 22:30:45')[0m
[1m[35m (0.3ms)[0m SELECT COUNT(*) FROM `girls` WHERE (country_id=1) AND (vip_highlight >= '2015-05-26 22:30:45')
[1m[36mRegion Load (0.2ms)[0m [1mSELECT `regions`.* FROM `regions` WHERE `regions`.`country_id` = 1[0m
[1m[35mList Load (0.3ms)[0m SELECT `lists`.* FROM `lists` WHERE (billing_id =5) ORDER BY `lists`.`id` ASC LIMIT 1
[1m[36m (0.3ms)[0m [1mBEGIN[0m
[1m[35m (0.2ms)[0m COMMIT
[1m[36m (0.2ms)[0m [1mBEGIN[0m
[1m[35m (0.2ms)[0m COMMIT
Redirected to http://xxxxxxxxxxx.xxx.xxx/ru
Completed 302 Found in 24ms (ActiveRecord: 4.4ms)
Description:
When user pushes delete button, user gets message :
translation missing: ru.devise.registrations.user.destroyed
also he is logged out from his account, but the actual destroying never happens. What could be possible problem?
Thanks for your time.
Best wishes.
Here Destroy deletes user account by using destroy method of devise registrations_controller. For translation missing: ru.devise.registrations.user.destroyed you need to make changes in devise.ru.yml file -
ru:
devise:
registrations:
user:
destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
This is background process. So, you can't see in your logs(delete sql query).

redirect_to method propagates DELETE http action throughout nested routes

The above is the result of deleted comment. Notice that as you delete a comment, the comment's parent post is also deleted through redirect_to
Started DELETE "/posts/19/comments/30" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by CommentsController#destroy as JS
Parameters: {"post_id"=>"19", "id"=>"30"}
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "30"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "30"]]
(0.0ms) begin transaction
SQL (0.2ms) DELETE FROM "comments" WHERE "comments"."id" = ? [["id", 30]]
(7.7ms) commit transaction
Redirected to http://localhost:3000/posts/19
Completed 302 Found in 13ms (ActiveRecord: 8.4ms)
Started DELETE "/posts/19" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by PostsController#destroy as JS
Parameters: {"id"=>"19"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
(0.0ms) begin transaction
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 19
SQL (0.2ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 19]]
(1.1ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 6ms (ActiveRecord: 1.7ms)
Started DELETE "/" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by PagesController#home as JS
Rendered pages/home.html.haml within layouts/application (0.1ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 200 OK in 40ms (Views: 39.3ms | ActiveRecord: 0.2ms)
routes.rb
resources :posts do
member do
put "soft_destroy"
end
resources :comments do
member do
get "reply"
post "create_reply"
put "soft_destroy"
end
end
end
comments controller
def destroy
#post = Post.find(params[:post_id])
#comment = Comment.find(params[:id])
#comment.destroy
redirect_to #post
end
delete link on view file
= link_to "delete", [#post, comment], method: :DELETE, remote: true
Post model
has_many :comments, dependent: :destroy
accepts_nested_attributes_for :comments
Comment model
belongs_to :post
Is there a reason why DELETE html verb propagates on posts controller as well? Rather than just calling show action?
The problem was caused by delete link on view file
= link_to "delete", [#post, comment], method: :DELETE, remote: true
For some reason, ajax request with method DELETE seems to propagate beyond the first DELETE request.
I removed remote: true and it now makes a GET request rather than DELETE request to the post.
= link_to "delete", [#post, comment], method: :DELETE
I still don't understand why this is happening though.

Resources