Rails活动记录::使用友好ID和slug识别未找到

时间:2016-09-25 13:42:22

标签: ruby-on-rails paperclip rails-activerecord slug friendly-id

我设置了我的rails应用程序以使用friendly_id和paperclip,并且我已经使用迁移将slug列添加到'designs'数据库表中。当我创建一个新设计帖子并上传我的图像(使用回形针)时,当我检查数据库时,slug列没有更新,然后我得到一个活动记录错误,说明如下:

enter image description here

以下是我的代码段:

型号:

class Design < ApplicationRecord
  attr_accessor :slug
  extend FriendlyId
  friendly_id :img_name, use: [:slugged, :finders]
  has_attached_file :image, styles: {
  :thumb    => ['100x100#',  :jpg, :quality => 70],
  :preview  => ['480>',      :jpg, :quality => 70],
  :large    => ['800>',      :jpg, :quality => 30],
  :retina   => ['1200>',     :jpg, :quality => 30]
},
:convert_options => {
  :thumb    => '-set colorspace sRGB -strip',
  :preview  => '-set colorspace sRGB -strip',
  :large    => '-set colorspace sRGB -strip',
  :retina   => '-set colorspace sRGB -strip -sharpen 0x0.5'
}
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

控制器:

class DesignsController < ApplicationController
  before_action :find_design, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @designs = Design.all.order("created_at desc")
  end

  def new
    @design = Design.new
  end

  def create
    @design = Design.new(design_params)

    if @design.save
      redirect_to @design, notice: "Hellz yeah, Steve! Your artwork was successfully saved!"
    else
      render 'new', notice: "Oh no, Steve! I was unable to save your artwork!"
    end
  end

  def show
  end

  def edit
  end

  def update
    if @design.update design_params
      redirect_to @design, notice: "Huzzah! Your artwork was successfully saved!"
    else
      render 'edit'
    end
  end

  def destroy
    @design.destroy
    redirect_to designs_path
  end

  private

  def design_params
    params.require(:design).permit(:img_name, :slug, :image, :caption)
  end

  def find_design
    @design = Design.friendly.find(params[:id])
  end
end

查看(#show)

<div id="post_show_content" class="skinny_wrapper wrapper_padding">
    <header>
        <p class="date"><%= @design.created_at.strftime("%A, %b %d") %></p>
        <h1><%= @design.img_name %></h1>
        <hr>
    </header>
          <%= image_tag @design.image.url(:retina), class: "image" %>
        <div class="caption">
          <p><%= @design.caption %></p>
        </div>

    <% if user_signed_in? %>
      <div id="admin_links">
        <%= link_to "Edit Artwork", edit_design_path(@design) %>
        <%= link_to "Delete Artwork", design_path(@design), method: :delete, data: {confirm: "Are you sure?" } %>
      </div>
    <% end %>

    

</div>

迁移:

class AddSlugToDesigns < ActiveRecord::Migration[5.0]
  def change
    add_column :designs, :slug, :string
    add_index :designs, :slug, unique: true
  end
end

2 个答案:

答案 0 :(得分:0)

使用activerecord find_by方法用slug找到你的模型。你的find_design方法看起来像这样

class DesignsController < ApplicationController
  def find_design  
    @design = Design.find_by(slug: params[:id])
  end
end

在你的设计模型中,你没有定义img_name(这是一个方法调用)你在friendly_id :img_name, use: [:slugged, :finders]上引用 将以下内容添加到您的模型design.rb

def img_name
  self.img_name
end

另一种方法是跳过使用friendly_id gem并编写自己的方法来生成随机slug并将其称为before_validation。

之类的东西
before_validation :set_slug, :on => :create

def set_slug
  random_token = SecureRandom.hex(3)
  self.slug = "#{self.img_name.downcase}_#{random_token}"
end

然后在您需要使用slug

查询db的控制器上使用Design.find_by(slug: params[:id])

答案 1 :(得分:0)

在运行迁移之前运行rails generate friendly_id。尽管我之前已经为数据库中的其他模型和表执行了此操作,但我需要在为新表中的新slug运行迁移之前再次执行此操作。