gpt4 book ai didi

ruby-on-rails - 用户可编辑的带有友好 ID 的 slug

转载 作者:行者123 更新时间:2023-12-04 14:59:24 24 4
gpt4 key购买 nike

案例:

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

编辑:一些澄清:

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

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

  • 我的问题:

    无法弄清楚如何获取友好 ID 以使用用户提供的 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

    编辑:
    <%= 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 %>

    最佳答案

    我是这样解决的:

    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

    和测试:
    /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

    关于ruby-on-rails - 用户可编辑的带有友好 ID 的 slug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19989827/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com