Rails:删除按钮的Param问题

时间:2017-11-13 08:56:52

标签: ruby-on-rails button parameters 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]

我想通过单击按钮从数据库中删除条目。但是必须与&#34; private_params&#34;发生冲突。 我总是遇到这个问题:

param缺失或值为空:partner

我搜索了类似的问题,但似乎没有什么适合。

3 个答案:

答案 0 :(得分:0)

如果您要删除条目,则需要发送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]

答案 1 :(得分:0)

感谢Gabbar的帮助,但现在我自己找到了解决方案。是一个非常愚蠢的人。 我打电话了

method: destroy

但必须是:method: delete 否则选择错误的路径。

然后routes.rb必须是:delete '/partner' => 'partner#delete'

答案 2 :(得分:0)

如果您只想删除1条记录,请使用方法ssr,如下所示:

ReactDOM.render

现在,如果要删除多于1个,请使用destroy将条件作为参数传递,如下所示:

Partner.find(params[:id]).destroy

所以,用驱逐舰替换destroy_all,就像这样:

destroy_all

我希望能帮到你

Destroy_all documentation