用户可编辑的带有友好 ID 的 slug [英] User editable slugs with Friendly ID

查看:41
本文介绍了用户可编辑的带有友好 ID 的 slug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

案例:

我的站表单包含一个 slug 字段,如果输入了一个值,它应该被用作 slug.

一些说明:

我想要的很像 slugs 在 wordpress 中的工作方式:

  • 如果没有提供 slug -> slug 名称
  • 如果提供了 slug -> 使用用户输入的 slug
  • 如果 slug 已更新 -> 将旧的 slug 推入历史

我的问题:

无法弄清楚如何获取友好 ID 以使用用户提供的 slug.

class Station [:slugged, :history]before_save :set_timezone!....def should_generate_new_friendly_id?名称_改变了吗?或 slug_changed?结尾结尾

<小时>

<%= form_for(@station) do |f|%><%=f.div_field_with_label(:name) do |key|f.text_field(key)结尾%><%=f.div_field_with_label(:slug) 做 |key|f.text_field(key)结尾%><%=f.div_field_with_label(:hw_id, 'Hardware ID') do |key|f.text_field(key)结尾%><%=f.div_field_with_label(:latitude) 做 |key|f.text_field(key)结尾%><%=f.div_field_with_label(:longitude) 做 |key|f.text_field(key)结尾%><%= f.div_field_with_label(:user_id, "Owner") do |key|f.select(:user_id, options_from_collection_for_select(User.all, :id, :email), { include_blank: true })结尾%><div class="actions"><%= f.submit %>

<% end %><%= form_for(@station) do |f|%><%=f.div_field_with_label(:name) do |key|f.text_field(key)结尾%><%=f.div_field_with_label(:slug) 做 |key|f.text_field(key)结尾%><%=f.div_field_with_label(:hw_id, 'Hardware ID') do |key|f.text_field(key)结尾%><%=f.div_field_with_label(:latitude) 做 |key|f.text_field(key)结尾%><%=f.div_field_with_label(:longitude) 做 |key|f.text_field(key)结尾%><%= f.div_field_with_label(:user_id, "Owner") do |key|f.select(:user_id, options_from_collection_for_select(User.all, :id, :email), { include_blank: true })结尾%><div class="actions"><%= f.submit %>

<%结束%>

解决方案

我是这样解决的:

class Station [:slugged, :history]before_save :evaluate_slugbefore_save :set_timezone!def should_generate_new_friendly_id?如果!蛞蝓?名称_改变了吗?别的错误的结尾结尾结尾

以及测试:

/spec/models/station_spec.rb

describe Station do...让(:站){创建(:站)}描述猛击"做它应该在没有 slug 的情况下使用 slug 名称"吗站=创建(:站,名称:'foo')期望(station.slug).to eq 'foo'结尾它应该使用 slug 如果提供"做station = create(:station, name: 'foo', slug: 'bar')期望(station.slug).to eq 'bar'结尾结尾...结尾

/spec/controllers/stations_controller.rb

describe StationsController do...描述POST 创建"做它创建一个带有自定义 slug 的站"做valid_attributes[:slug] = 'custom_slug'后:创建,{:站=>valid_attributes}获取 :show, id: 'custom_slug'期望(响应).成为_成功结尾...结尾描述PUT更新"做它更新slug"做把:更新,{:id =>station.to_param, :station =>{ slug: 'custom_slug' }}获取 :show, id: 'custom_slug'期望(响应).成为_成功结尾...结尾...结尾

Case:

My station forms contain a slug field, if a value is entered it should be used as the slug.

EDIT: some clarification:

What I want is much like how slugs work in wordpress:

  • If no slug is provided -> slug the name
  • If slug is provided -> use the user entered slug
  • If slug is updated -> push old slug to history

My problem:

Can´t figure out how to get Friendly Id to use the user provided slug.

class Station < ActiveRecord::Base
  extend FriendlyId
  belongs_to :user
  has_many  :measures
  validates_uniqueness_of :hw_id
  validates_presence_of :hw_id
  class_attribute :zone_class
  self.zone_class ||= Timezone::Zone
  friendly_id :name, :use => [:slugged, :history]

  before_save :set_timezone!

  ....

  def should_generate_new_friendly_id?
    name_changed? or slug_changed?
  end
end


edit:

<%= form_for(@station) do |f| %>

<%=
    f.div_field_with_label(:name) do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:slug) do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:hw_id, 'Hardware ID') do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:latitude) do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:longitude) do |key|
      f.text_field(key)
    end
%>
<%= f.div_field_with_label(:user_id, "Owner") do |key|
        f.select(:user_id, options_from_collection_for_select(User.all, :id, :email), { include_blank: true  })
    end
%>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %><%= form_for(@station) do |f| %>

<%=
    f.div_field_with_label(:name) do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:slug) do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:hw_id, 'Hardware ID') do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:latitude) do |key|
      f.text_field(key)
    end
%>
<%=
    f.div_field_with_label(:longitude) do |key|
      f.text_field(key)
    end
%>
<%= f.div_field_with_label(:user_id, "Owner") do |key|
        f.select(:user_id, options_from_collection_for_select(User.all, :id, :email), { include_blank: true  })
    end
%>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

解决方案

This is how i solved it:

class Station < ActiveRecord::Base
  extend FriendlyId
  belongs_to :user
  has_many  :measures
  validates_uniqueness_of :hw_id
  validates_presence_of :hw_id
  class_attribute :zone_class
  self.zone_class ||= Timezone::Zone
  friendly_id :name, :use => [:slugged, :history]
  before_save :evaluate_slug
  before_save :set_timezone!


  def should_generate_new_friendly_id?
    if !slug?
      name_changed?
    else
      false
    end
  end

end

And the tests:

/spec/models/station_spec.rb

describe Station do
  ...   
  let(:station) { create(:station) }

  describe "slugging" do
    it "should slug name in absence of a slug" do
      station = create(:station, name: 'foo')
      expect(station.slug).to eq 'foo'
    end

    it "should use slug if provided" do
      station = create(:station, name: 'foo', slug: 'bar')
      expect(station.slug).to eq 'bar'
    end
  end
  ...
end

/spec/controllers/stations_controller.rb

describe StationsController do

    ...

    describe "POST create" do
        it "creates a station with a custom slug" do
          valid_attributes[:slug] = 'custom_slug'
          post :create, {:station => valid_attributes}
          get :show, id: 'custom_slug'
          expect(response).to be_success
        end

        ...
    end

    describe "PUT update" do
        it "updates the slug" do
          put :update, {:id => station.to_param, :station => { slug: 'custom_slug' }}
          get :show, id: 'custom_slug'
          expect(response).to be_success
        end

        ...
    end 

    ...
end

这篇关于用户可编辑的带有友好 ID 的 slug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆