Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么可以';在使用友好id时,我不能再编辑和销毁我的PIN吗?_Ruby On Rails_Friendly Id - Fatal编程技术网

Ruby on rails 为什么可以';在使用友好id时,我不能再编辑和销毁我的PIN吗?

Ruby on rails 为什么可以';在使用友好id时,我不能再编辑和销毁我的PIN吗?,ruby-on-rails,friendly-id,Ruby On Rails,Friendly Id,我相信我正确地遵循了friendly_id github页面中的所有步骤。我知道它是有效的,因为它将我的url从/1更改为/sample url。然而,问题是我不能再编辑和销毁我更改的url所在的PIN 我希望有人能帮我修这个。谢谢大家! /pins_controller.rb class PinsController < ApplicationController before_action :set_pin, only: [:show, :edit, :update, :destr

我相信我正确地遵循了friendly_id github页面中的所有步骤。我知道它是有效的,因为它将我的url从/1更改为/sample url。然而,问题是我不能再编辑和销毁我更改的url所在的PIN

我希望有人能帮我修这个。谢谢大家!

/pins_controller.rb

class PinsController < ApplicationController
  before_action :set_pin, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  respond_to :html

  def index
    @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 8)
    respond_with(@pins)
  end

  def show
    respond_with(@pin)
  end

  def new
    @pin = current_user.pins.build
    respond_with(@pin)
  end

  def edit
  end

  def create
    @pin = current_user.pins.build(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
    respond_with(@pin)
  end

  def upvote
    @pin = Pin.find(params[:id])
    @pin.upvote_by current_user
    redirect_to :back
  end

  def downvote
    @pin = Pin.find(params[:id])
    @pin.downvote_from current_user
    redirect_to :back
  end

  private
    def set_pin
      @pin = Pin.friendly.find(params[:id])
    end

    def correct_user
      @pin = current_user.pins.find_by(id: params[:id])
      redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
    end

    def pin_params
      params.require(:pin).permit(:description, :image)
    end
end
class PinsControllerparams[:page],:per\u page=>8)
用(@pins)响应_
结束
def秀
用(@pin)响应_
结束
def新
@pin=当前_user.pins.build
用(@pin)响应_
结束
定义编辑
结束
def创建
@pin=当前用户.pins.build(pin参数)
如果@pin.save
将_重定向到@pin,注意:“pin已成功创建。”
其他的
渲染操作:“新建”
结束
结束
def更新
如果@pin.update(pin_参数)
将_重定向到@pin,注意:“pin已成功更新。”
其他的
渲染操作:“编辑”
结束
结束
def销毁
@销
用(@pin)响应_
结束
投票表决
@pin=pin.find(参数[:id])
@pin.upvote_由当前_用户提供
重定向到:返回
结束
def否决票
@pin=pin.find(参数[:id])
@来自当前用户的pin.downvote_
重定向到:返回
结束
私有的
def设置针
@pin=pin.friendly.find(参数[:id])
结束
def正确的用户
@pin=当前用户.pins.find_by(id:params[:id])
重定向到pin路径,注意:如果@pin.nil,则“无权编辑此pin”?
结束
def引脚参数
参数require(:pin).permit(:description,:image)
结束
结束
/pin.rb

class Pin < ActiveRecord::Base

    acts_as_votable

    belongs_to :user

    has_attached_file :image, :styles => { :medium => '300x300>', :thumb => '100x100>' }
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]

    validates :image, presence: true
    validates :description, presence: true

    extend FriendlyId
  friendly_id :description, use: :slugged
end
class Pin{:medium=>'300x300>,:thumb=>'100x100>'
验证附件内容类型:图像、内容类型=>[“图像/jpg”、“图像/jpeg”、“图像/png”]
验证:映像,状态:true
验证:描述,状态:true
扩展友好ID
友好的\u id:description,use::sluged
结束

罪魁祸首是
@pin=current\u user.pins.find\u by(id:params[:id])
中更正\u user

请注意,对于编辑、更新和销毁操作,您将获取两次pin。一次进入
设置密码
和一次进入
纠正用户
。在
correct_user
中,您只需检查
@pin.user_id==当前_user.id

另外,您现在的方式是,在
authenticate\u user!中进行用户身份验证最后运行,如果未经身份验证的用户向编辑操作提交请求,将导致错误

class PinsController < ApplicationController
  #authenticate_user! must go first
  before_action :authenticate_user!, except: [:index, :show]
  before_action :set_pin, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]


  respond_to :html

  .... your actions here

  private
    def set_pin
      @pin = Pin.friendly.find(params[:id])
    end

    def correct_user
      unless @pin.user_id == current_user.id
        redirect_to pins_path, notice: "Not authorized to edit this pin"

        #you must return false to halt
        false
      end
    end

    def pin_params
      params.require(:pin).permit(:description, :image)
    end
end
class PinsController
谢谢AbM!这很有帮助。我从来没有想过行动的顺序很重要。因此,对于我未来的项目,只有用户!应该先来,其他人都可以不按顺序吗?顺序很重要。将按照您定义的顺序运行
before\u操作
s。如果页面需要身份验证,您可能应该运行
authenticate\u用户第一个。在我提供的解决方案中,
set\u pin
必须运行第二次才能设置
@pin
。如果情况并非如此(即,
correct\u user
是第二个),则会引发错误(告诉您nil类没有
user\u id
),因为
@pin
尚未定义。试试看我是什么意思是的,我完全明白你的意思。谢谢你,伙计!