【问题标题】:Rails active record::not found using friendly id and slug recognitionRails active record::not found using friendly id and slug recognition
【发布时间】:2017-02-02 21:21:50
【问题描述】:

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

这是我的代码 sn-ps:

型号:

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

【问题讨论】:

  • 不确定friendly_id,但您可以使用activerecord find_by Design.find_by(slug: params[:id]) 使其正常工作
  • 干杯 - 抱歉,我应该将它添加到哪个文件中?
  • 只需在控制器上的 find_design 操作中将其替换为 @design = Design.find_by(slug: params[:id])
  • 我认为更多的是我创建帖子后检查数据库时,slug列中没有数据,它只是说NULL。因此,由于某种原因,当我创建一个新设计 - ...designs/new 并提交时,它会将其添加到数据库中,但 slug 字段为 NULL。
  • 所以我尝试在 Rails 控制台中运行 Design.find_each(&amp;:save),但这也没有解决它。

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


【解决方案1】:

使用 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]上引用 将以下内容添加到您的模型设计中。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])

【讨论】:

  • 我认为更多的是我创建帖子后检查数据库时,slug列中没有数据,它只是说NULL。因此,由于某种原因,当我创建一个新设计 - ...designs/new 并提交时,它会将其添加到数据库中,但 slug 字段为 NULL。
  • 所以我尝试在 Rails 控制台中运行 Design.find_each(&amp;:save),但这也没有解决问题。
  • 尝试在您的模型上添加img_name 方法。
  • 对不起,这不起作用。我确定这与向数据库的 slug 迁移以及无法正常工作的事实有关。我可以创建设计帖子,当我转到 ...designs#index 时,它就在那里并且在数据库中,它只是 slug 没有获取 img_title,因此friendly_id 没有显示它。
  • 好吧,或者您可以尝试编写自己的方法来生成 slug 并在您的模型上调用它before_validation
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多