Rails:删除按钮的参数问题
Rails: Param problems with delete button
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 %>
控制器:
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]
我想通过单击按钮从我的数据库中删除一个条目。但是肯定和"private_params"有冲突。
我总是遇到这个问题:
缺少参数或值为空:合作伙伴
我搜索了类似的问题,但似乎没有真正适合的问题。
如果您想删除条目,则需要发送 id
合作伙伴
<%if logged_in? %>
<%= button_to "Delete", partner_path(id: j.id), :method => :delete %>
<% end %>
控制器:-
def destroy
Partner.find(params[:id]).destroy_all
redirect_to partner_path, notice: "#Eintrag wurde gelöscht!"
end
路线:-
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]
感谢 Gabbar 的帮助,但现在我自己找到了解决方案。是一个非常愚蠢的人。
我打电话给
method: destroy
但必须是:method: delete
否则选择了错误的路径。
那么routes.rb需要是:delete '/partner' => 'partner#delete'
如果您只想删除 1 条记录,请使用方法 destroy
,如下所示:
Partner.find(params[:id]).destroy
现在,如果要删除超过 1 个,请使用 destroy_all
将条件作为参数传递,如下所示:
Partner.destroy_all(status: 'inactive')
因此,将 destroy_all 替换为驱逐舰,如下所示:
Partner.find(params[:id]).destroy
希望对你有帮助
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 %>
控制器:
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]
我想通过单击按钮从我的数据库中删除一个条目。但是肯定和"private_params"有冲突。 我总是遇到这个问题:
缺少参数或值为空:合作伙伴
我搜索了类似的问题,但似乎没有真正适合的问题。
如果您想删除条目,则需要发送 id
合作伙伴
<%if logged_in? %>
<%= button_to "Delete", partner_path(id: j.id), :method => :delete %>
<% end %>
控制器:-
def destroy
Partner.find(params[:id]).destroy_all
redirect_to partner_path, notice: "#Eintrag wurde gelöscht!"
end
路线:-
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]
感谢 Gabbar 的帮助,但现在我自己找到了解决方案。是一个非常愚蠢的人。 我打电话给
method: destroy
但必须是:method: delete
否则选择了错误的路径。
那么routes.rb需要是:delete '/partner' => 'partner#delete'
如果您只想删除 1 条记录,请使用方法 destroy
,如下所示:
Partner.find(params[:id]).destroy
现在,如果要删除超过 1 个,请使用 destroy_all
将条件作为参数传递,如下所示:
Partner.destroy_all(status: 'inactive')
因此,将 destroy_all 替换为驱逐舰,如下所示:
Partner.find(params[:id]).destroy
希望对你有帮助