Rails: Param problems with delete button - ruby-on-rails

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

Related

ActionController::ParameterMissing in TagsController#create

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?' } %>

I am working with carrierwave and there is an error unknown operator: $oid (2)

I am trying to upload a file and show the file data like a post.I am using carrierwave to upload a file.Here are the details
index.html.erb
<div style="margin-bottom:0px;margin-left:430px;margin-top:50px;width:30%;">
<%= form_for Post.new, :url => {:action => 'create'}, :html => {:multipart => true} do |f| %>
<%= f.file_field :attachment , class: "filestyle"%>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.submit "Upload" , class: "btn btn-primary btn-block" ,style:"margin-top:8px;" %>
<% end %>
</div>
Postscontroller.rb
def index
#posts = Post.all
#user = User.find_by(id: params[:user_id])
end
def create
#post = Post.new(post_params)
#user = User.find_by(id: params[:user_id])
if #post.save
redirect_to :action => 'index'
flash[:notice] = "The post has been uploaded."
else
render "index"
end
end
private
def post_params
params.require(:post).permit(:attachment,:user_id)
end
routes.rb
Rails.application.routes.draw do
root "users#index"
get '/' => "users#index"
get "/new1" => "users#new1"
get "/new2" => "users#new2"
get "/new3" => "users#new3"
resources :users ,to: 'posts'
post "/users" => "users#create"
get "users/:id" => "users#show"
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
delete '/logout', to: 'sessions#destroy'
resources :sessions
resources :posts
get "users/:id/post" => "posts#index"
end
When i am trying to upload a file ,then it is showing an error that
unknown operator: $oid (2).I don't know why it is happening.I am new to the rails please help me out to overcome this error.

Rails Routing Help - Edit/Show have :id/:action, but delete has :action/:id

my routes.rb
root 'pages#home'
get 'about', to: 'pages#about'
resources :articles
match ':controller(/:action(/:id))', :via => [:get, :post]
my articles_controller.rb
def delete
#article = Article.find(params[:id])
end
def destroy
article = Article.find(params[:id])
article.destroy
flash[:notice] = "Article destroyed successfully."
redirect_to(:action => 'index')
end
my delete.html.rb
<h2>Delete Article</h2>
<div class="subjects delete">
<h2>Delete Subject</h2>
<%= form_for(:article, :url => {:action => 'destroy', :id => #article.id}) do |f| %>
<p>Are you sure you want to permanently delete this subject?</p>
<p><%= #article.title %></p>
<div class="form-buttons">
<%= submit_tag("Delete Article") %>
</div>
<% end %>
</div>
Now the problem is when i click edit article its redirecting to
http://localhost:3000/articles/7/edit
Show is redirecting to localhost:3000/articles/7
Where as delete is redirecting to localhost:3000/articles/delete/8
and when clicking delete button it says The action '8' could not be found for ArticlesController
Why i dont get redirected to localhost:3000/articles/8/delete
i changed routes.rb to
match ':controller(/:id(/:action))', :via => [:get, :post]
in this delete works but edit doesnt.
You'll be best using the resources directive for the routes:
#config/routes.rb
resources :articles #-> url.com/articles/:id/destroy
resources :pages, only: [], path: "" do
get :about, on: :collection #-> url.com/about
end
root 'pages#home'
The above will provide the correct routes.
Your problem is you're submitting a form to try and change the value. This will not work; you'll need:
#app/views/articles/show.html.erb
<%= button_to "Delete", article, method: :delete, data: { confirm: "Are you sure?" } %>
This will call your posts#destroy controller & fire your code (which seems okay).

Resources delete method not working with Devise

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

Rails Route that Updates or Redirects

Im not sure if I'm doing this right. I have an action that I would like to either copy, create, and save a new object if a user is logged in, or redirect if they are not logged in. Im not using a form here because I am using a stylized button with an image that looks like this:
<a href="/lists/add/<%= #list.id %>" class="button">
<span class="add_list">Learn these words</span>
</a>
and the action looks like this:
def add
if is_logged_in?
list = logged_in_user.copy_list(params[:id])
if list.save
flash[:notice] = "This list is now in your stash."
redirect_to stash_zoom_nav_quiz_path(list, "zoomout", "new", "quizoff")
else
flash[:notice] = "There was a problem adding this list."
redirect_to :back
end
else
redirect_to :controller => "users", :action => "signup_and_login", :list_id => params[:id]
end
end
map.resources :lists, :collection => {:share => :get, :share_callback => :get, :add => :put}
I have added this action as a :put in my routes and I'm not sure if this is right or if the other stuff is the right way to even do it for that matter. Any help is appreciated.
The specific answer to your question is
map.resources :lists, :collection => { :share => :get, :share_callback => :get }, :member => { :add => :put }
add action works on a member, not on a collection.
But there are other problems in your code. First, you should always use Rails helpers to generate the URLs. In fact, the path /lists/add/<%= #list.id %> is wrong. It should be /lists/<%= #list.id %>/add
Change
<a href="/lists/add/<%= #list.id %>" class="button">
<span class="add_list">Learn these words</span>
</a>
to
<% link_to add_list_path(#list), :class => "button" do %>
<span class="add_list">Learn these words</span>
<% end %>
The controller can be simplified. Move the is_logged_in? check in a before filter.
class MyController < ActionController::Base
before_filter :require_logged_user, :only => %w( add )
def add
list = logged_in_user.copy_list(params[:id])
if list.save
flash[:notice] = "This list is now in your stash."
redirect_to stash_zoom_nav_quiz_path(list, "zoomout", "new", "quizoff")
else
flash[:notice] = "There was a problem adding this list."
redirect_to :back
end
end
protected
def require_logged_user
if !is_logged_in?
redirect_to :controller => "users", :action => "signup_and_login", :list_id => params[:id]
end
end
end
Try this in your routes.rb:
map.resources :lists, :member => {:add => :put}, :collection => {:share => :get, :share_callback => :get}
:member - Same as :collection, but for actions that operate on a specific member.

Resources