For my inbox I created a way to delete multiple messages from the inbox. Now I am trying to setup how to delete the message the current user is viewing. There should be a link at the bottom of the message that reads Delete. When user clicks the Delete link the message will be deleted and user would be redirected to their inbox.
When adding the code to the controller, I am now unable to even view the inbox message. I get a undefined local variable or method "message" error when trying to view message, with the page title of NameError in Messages#show.
Anyone know how I can fix this problem?
New code added to controller:
def destroy
#message = Message.find(params[:id])
#message.destroy
flash[:notice] = "Successfully deleted message."
redirect_to user_messages_path(#user, #messages)
end
Messages controller:
Class MessagesController < ApplicationController
before_filter :set_user
def show
#message = Message.find(params[:id])
#message.readingmessage if #message.recipient == current_user
end
def destroy
#message = Message.find(params[:id])
#message.destroy
flash[:notice] = "Successfully deleted message."
redirect_to user_messages_path(#user, #messages)
end
private
def set_user
#user = current_user
end
end
show.html:
<%= link_to "remove", message, :confirm => 'Are you sure?', :method => :delete %>
routes:
Dating::Application.routes.draw do
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'logout' => 'sessions#destroy'
get 'edit' => 'users#edit'
get "/profile/:id" => "users#show"
get "profile/:id/settings" => 'users#edit'
match 'settings/:id' => 'users#settings'
resources :users
resources :sessions
resources :password_resets
resources :galleries
resources :photos
resources :searches
resources :users do
get 'settings', on: :member
end
root to: 'users#new'
root to: 'galleries#index'
resources :users do |user|
resources :messages do
collection do
post 'delete_multiple'
end
end
end
I think you need to use #message in your link_to like this:
<%= link_to "remove", #message, :confirm => 'Are you sure?', :method => :delete %>
Related
I am attempting to trigger a destroy action from an index page for tags attached to multiple records. However, when the action is triggered I get the above error in the create action. The error does not occur when the create action is invoked. My code is as seen below.
Tag Controller
class TagsController < ApplicationController
before_action :require_user, only: [:edit, :update, :destroy]
before_action :set_search
def new
#tag = Tag.new
end
def create
tag = Tag.create(tag_params)
if tag.save
redirect_to tags_path
else
redirect_to sign_up_path
end
end
def destroy
#tag = Tag.find(params[:tag_id])
#tag.destroy
redirect_to tags_path
end
private
def tag_params
params.require(:tag).permit(:name)
end
end
Routes
Rails.application.routes.draw do
# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html
resources :recipes do
resources :ingredients, :steps
put :favorite, on: :member
end
resources :users
get 'recipes' => 'recipes#index'
get 'recipes/:id' => 'recipes#show'
get 'signup' => 'users#new'
get 'tags' => 'tags#index'
get 'new_tags' => 'tags#new'
post 'tags' => 'tags#create'
delete 'tags' => 'tags#destroy'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
root 'recipes#index'
end
Index
<%= link_to 'New Tag', new_tags_path(#tag) %>
<% Tag.find_each do |tag| %>
<%= tag.name %>
<%= link_to 'Delete Tag', #tag,
method: :destroy,
data: { confirm: 'Are you sure?' } %>
<% end %>
This route:
delete 'tags' => 'tags#destroy'
means "/tags" with no id parameter.
You have to tell the router that you want a part of the url used as the :tag_id
delete 'tags/:tag_id' => 'tags#destroy'
Anyway, I'd recommend you to stick with rails conventions and just use
resources :tags
and on the controller
#tag = Tag.find(params[:id])
I'm not sure why are you setting the routes manually, if you are new to Rails read this link: https://guides.rubyonrails.org/routing.html
You even have this lines
get 'recipes' => 'recipes#index'
get 'recipes/:id' => 'recipes#show'
which are unnecessary since resources :recipes already creates them.
The error stemmed from a combination of issues in the Link To statement in the view. The Link as it should be written is below...
<%= link_to 'Delete Tag', tag_path(tag),
method: :delete,
data: { confirm: 'Are you sure?' } %>
index.html:
<% #partners.each do |j| %>
<% if j.link == nil %>
<div class="column_left"><%= j.name %></div>
<% else %>
<div class="column_left"><%= image_tag("#{j.link}") %></div>
<% end %>
<div class="column_right"><%= j.description %></div>
<%if logged_in? %>
<%= button_to "-", j, :method => :destroy , data: { confirm: "Sind Sie sich sicher, dass sie den Partner #{j.name} löschen wollen?" } %>
<% end %>
controller:
def delete
Partner.find(partner_params).destroy_all
redirect_to partner_path, notice: "#Eintrag wurde gelöscht!"
end
def addPartner
#partner = Partner.new(partner_params)
if !Partner.exists?(:name => partner_params[:name])
uploaded_io = params[:partner][:logo]
File.open(Rails.root.join('app', 'assets', 'images', 'partner', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
#partner[:link] = "partner/#{uploaded_io.original_filename}"
params[:partner].delete :logo
#partner.save
redirect_to partner_path, notice: "#{#partner.name} wurde modifiziert!"
end
else
#partner = Partner.where(:name => partner_params[:name])
if #partner.update_all(partner_params)
flash[:notice] = "#{#partner.name} wurde geändert!"
end
end
end
private
def partner_params
params.require(:partner).permit(:name, :link, :description)
end
routes.rb:
Rails.application.routes.draw do
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete '/logout' => 'sessions#destroy'
resources :users
get '/home' => 'main#home'
get '/impressum' => 'main#impressum'
post '/impressum' => 'main#updateImpressumById'
get '/partner' => 'partner#index'
post '/partner' => 'partner#addPartner'
delete '/partner' => 'partner#delete'
get '/jobs' => 'jobs#index'
post '/jobs' => 'jobs#index'
get '/kontakt' => 'contacts#new'
post '/home' => 'main#updateText'
get '/referenzen' => 'reference#index'
post '/referenzen' => 'reference#index'
resources "contacts", only: [:new, :create]
I want to delete an entry from my database by clicking on a button. But there must be a conflict with the "private_params".
I always got this problem:
param is missing or the value is empty: partner
I searched for similiar problems, but nothing seems to fit really.
If you want to delete an entry then you need to send a id of partner
<%if logged_in? %>
<%= button_to "Delete", partner_path(id: j.id), :method => :delete %>
<% end %>
Controller: -
def destroy
Partner.find(params[:id]).destroy_all
redirect_to partner_path, notice: "#Eintrag wurde gelöscht!"
end
Routes: -
Rails.application.routes.draw do
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete '/logout' => 'sessions#destroy'
resources :users
get '/home' => 'main#home'
get '/impressum' => 'main#impressum'
post '/impressum' => 'main#updateImpressumById'
get '/partners' => 'partner#index'
post '/partners' => 'partner#addPartner'
delete '/partner/:id' => 'partner#delete'
get '/jobs' => 'jobs#index'
post '/jobs' => 'jobs#index'
get '/kontakt' => 'contacts#new'
post '/home' => 'main#updateText'
get '/referenzen' => 'reference#index'
post '/referenzen' => 'reference#index'
resources "contacts", only: [:new, :create]
Thanks for the help Gabbar, but now I found the solution on my own. Was a pretty stupid one.
I called
method: destroy
but it must be: method: delete
otherwise the wrong path is chosen.
Then the routes.rb need to be: delete '/partner' => 'partner#delete'
If you only want delete 1 record, use the method destroy like this :
Partner.find(params[:id]).destroy
Now, if you want delete more than 1, use destroy_all passing a condition as an argument, like this :
Partner.destroy_all(status: 'inactive')
So, replace destroy_all with destroyer, like this:
Partner.find(params[:id]).destroy
I hope that helps you
Destroy_all documentation
I am using devise gem for authentication. For the admin user I am trying to create a page where he will see the list of all users and will be able to delete them. Because devise does not provide action for delete I created a controller where I created destroy action.
controller:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to("/devise")
end
The view is simple, it just lists all users and posts a delete link next to them.
<% #users.each do |user| %>
<%= user.email %> <%= link_to 'Delete', :controller => :devise, :action => :destroy, :id => (user[:id]), method: :delete, data: { confirm: "Are you sure you want to delete this user permanently?" } %>
<% end %>
Up until this point it works - the page displays all users and the delete link too. However, when I try to delete random users it just opens them in new window instead of deleting them.
My routes are as follows:
Rails.application.routes.draw do
devise_for :admins
devise_for :users
resources :devise
resources :centres
resources :users
#match '/users/:id', :to => 'devise#show', :as => :user, :via => :get
match '/users/:id', :to => 'devise#destroy', :as => :destroy_user, :via => :delete
root 'welcome#index'
get '/index', to: 'welcome#index'
get '/about', to: 'welcome#about'
get '/help', to: 'welcome#help'
end
I need to make the delete function work. Thank you.
This is the solution for the question I posted:
controlled:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to("/users")
end
view:
<%= link_to "delete", user, method: :delete,
data: { confirm: "Are you sure you want to permanently delete this user?" } %>
route:
match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete
As far as i know, device doesn't handle destroy action. You should create a custom controller and provide a destroy action. In my case, i create a users_controller
users_controller:
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :ok }
end
end
html.erb:
<%= link_to 'Delete', user, method: :delete, data: { confirm: 'Are you sure?' } %>
routes.rb:
match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete
With my rails app I can successfully destroy, sign up and log in users but I can seem to deactivate them. I get this error every time:
ActiveRecord::RecordNotFound in UsersController#deactivate (Couldn't find User with 'id'=)
record = s.execute([id], self, connection).first
unless record
raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
end
record
rescue RangeError
Here's the User controller:
def deactivate
user = User.find(params[:user_id])
if current_user.admin?
user.deactivate_account!
redirect_to users_path
else
redirect_to :back
end
end
The _user.html.erb view:
<li><%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
<%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
<% if current_user.admin? && #user != current_user %>
<%=link_to "deactivate", deactivate_path(user_id: #user), method: :post,
data: { confirm: "Are you sure?"}%>
<% end %>
</li>
and the routes:
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
post '/deactivate', to: "users#deactivate"
The logs are:
Rendered .../.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (9.0ms)
Rendered .../.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (3.9ms)
Rendered .../.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (1.5ms)
Rendered .../.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_web_console.html.erb (1.3ms)
Rendered .../.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (44.8ms)
What could be wrong? I'm assuming error is in the controller. I am following Michael Hartl tutorial, as I'm a beginner, but I'm also trying to implement thing by myself (like this one).
First of all, you should make this a PUT request rather than POST as it is updating the user record.
Here are the changes that I made to make this work:
Modified the route to PUT in your routes.rb file:
put 'deactivate/:id(.:format)', :to => 'users#deactivate', :as => :deactivate_user
You did not have the implementation for deactivate_account method for your User model. I did that. In your User model:
def deactivate_account
update_attributes!(activated: false)
end
And, finally, changed the user partial like this:
<%=link_to "Deactivate", deactivate_user_path(user), method: :put%>
I'm quite new to Ruby on Rails and I'm trying to learn a bit, so I tried making a new function.
The error I'm getting right now is:
ActionController::RoutingError (No route matches [POST] "/users/2"):
I'm trying to make a function to promote other users to admin, which is only possible as an admin.
My users_controller.rb contains:
def promote
if !User.find(params[:id]).admin?
User.find(params[:id]).toggle!(:admin)
flash[:success] = "User is promoted to admin."
redirect_to users_url
else
flash[:danger] = "Admins can't demote other admins."
redirect_to users_url
end
end
My _user.html.erb contains:
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
<% if !user.admin? %>
<br /> <%= link_to "Delete User", user, method: :delete,
data: { confirm: "You sure?" } %>
| <%= link_to "Promote to Admin", user, method: :promote,
data: { confirm: "You sure?" } %>
<% end %>
<% end %>
And then we have my routes.rb, which contains:
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get 'users/new'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
end
I can't exactly find what's going wrong. Some advice would be awesome!
def promote
#user = User.find(params[:id])
if #user.admin?
#user.toggle!(:admin)
flash[:success] = "User is promoted to admin."
redirect_to users_path
else
flash[:notice] = "Admins can't demote other admins."
redirect_to users_path
end
end
<%= link_to #user.name, #user %>
<% if current_user.admin? && !current_user?(#user) %>
<% if !#user.admin? %>
<br />
<%= link_to "Delete User", #user, method: :delete, data: { confirm: "You sure?" } %>
| <%= link_to "Promote to Admin", promote_users_path(user), method: :put, data: { confirm: "You sure?" } %>
<% end %>
<% end %>
in route
resources :users do
member do
get :following, :followers,
put :promote
end
end
or
match '/users', to: 'users#promote', via: :put