2013-11-14 38 views
6

案例:用戶可編輯與蛞蝓友好ID

我站形式包含蛞蝓字段,如果輸入的值應該被用作金屬塊。

編輯:澄清:

我要的是很像蛞蝓WordPress中是如何工作的:

  • 如果沒有提供塞 - >決高下名稱
  • 如果提供塞 - >使用用戶輸入的蛞蝓
  • 如果金屬塊更新 - >推老蛞蝓歷史

我的問題:

無法弄清楚如何讓Friendly 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 %> 
+0

你可以顯示錶單嗎?現在你使用的是friendly_id的名字。您只需要允許用戶更改名稱參數即可。 – ChrisBarthol

+0

用戶可以更改已經更改的名稱參數 - 我想要的是用戶可以自定義slu - - 但應用程序應該生成一個基於名稱的slu if如果沒有提供slu 012 – max

+0

我想我很困惑你在問什麼。用戶可以更改名稱,因此他們可以自定義slug。如果你想從一個不同的參數產生slug,你只需要'friendly_id:parameter,:use => [:slugged,:history]' – ChrisBarthol

回答

4

這是我如何解決它:

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 
+0

如果有衝突怎麼辦?就像用戶提供了一個已經在使用的slu?一樣? – mscriven

+0

@mscriven,默認情況下,友好-id將通過附加一個散列,如'foo-cdeea311-1957-4fab-b649-dd2a2b9ad90d'來使子彈具有唯一性來解決它。我想你可以使用'validates_uniqueness_of:slug',如果你想用Active Record驗證來強制執行。 – max

1

要更改用作塞什麼參數,你只需要改變friendly_id參數:

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 :SLUGNAME, :use => [:slugged, :history] 

    before_save :set_timezone! 

    .... 

    def should_generate_new_friendly_id? 
    name_changed? or SLUGNAME_changed? 
    end 
end 

然後在你看來只是有一種讓用戶修改slugname的方法,您的觀點如下:

<%= 
    f.div_field_with_label(:SLUGNAME) do |key| 
    f.text_field(key) 
    end 
%> 
+0

對不起,如果我在解釋失敗,但這錯過了標記。非常感謝您的努力! – max