0
我正在使用devise和我的銷燬鏈接路徑不能在我的rails應用程序上工作,我不知道爲什麼:/當我點擊它時,它調用'show'方法。我花了一些時間來研究這個問題。銷燬鏈接路徑不工作
我相信問題在我的index.html.erb文件中。看看:)我明白'pin'這個詞不應該跟着'destroy'的鏈接,因爲這是調用show方法,但我不知道還有什麼要放在那裏。
Index.html.erb:
<div id="pins">
<% @pins.each do |pin| %>
<div class="box">
<%= image_tag pin.image.url %>
<%= pin.description %>
<%= pin.user.email if pin.user %>
<%= link_to 'Show', pin %>
<% if pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(pin) %>
<%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure more than anything!?' } %></td>
<% end %>
</div>
<% end %>
</div>
Pins_controller.rb:
class PinsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :set_pin, only: [:show, :edit, :update, :destroy]
def index
@pins = Pin.all
end
def show
@pin = Pin.find params[:id]
end
def new
@pin = Pin.new
end
def edit
end
def create
@pin = Pin.new(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find(params[:id])
redirect_to pins_path, notice: "Not allowed!" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image)
end
end
而且我的routes.rb文件
Rails.application.routes.draw do
resources :pins
devise_for :users
root "pages#home"
get "about" => "pages#about"
end
請點擊鏈接時使用查看確認嗎?如果沒有,這意味着您的頁面上未啓用rails-ujs庫。 – MikDiet
啊,不,我不知道。我會看看,現在感謝:) – Benjamints