簡體   English   中英

允許用戶使用“行為可標記”選擇注​​冊表單中的標記

[英]Allowing users to select tags on sign up form using acts-as-taggable-on

有沒有辦法允許用戶在注冊devise時從預定義標簽中進行選擇?

registrations / new.html.erb

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %>
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <%= f.password_field :password, autocomplete: "off" %></br >
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

</div>
 <% tag_cloud User.tag_counts_on(:tags).order("name ASC"), %w[s m l] do |tag, css_class| %>
    <label class="category-select">
      <%= check_box_tag 'tag[]', (tag.name), false, class: 'tag-color' %>
      <span class="tag-name"><%= tag.name %></span>
    </label>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

application_controller.rb

def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :tag_list, :tag, :context_list, :context) }
  end

user.rb

acts_as_taggable_on :tags, :context

到目前為止,這是我注冊頁面的內容。 我需要做些什么,以便當用戶填寫電子郵件,密碼並檢查必要的標簽並單擊注冊時,將為該用戶保存標簽? 非常感謝您的幫助。 謝謝

tag_list必須是一個數組。 正如文檔所說:

數組不是Strong Parameters允許的標量之一,因此我們需要配置Devise。

在您的情況下,將是這樣的:

def configure_devise_params
   devise_parameter_sanitizer.for(:sign_up) do |u|
     u.permit(:email, :password, :password_confirmation, {:context_list => []}, {:tag_list => []})
   end
end

我認為您不需要tag以及tag_list 作為標簽行為只能發送tag_list。 您發現這個問題很有用。

更新:

 def configure_devise_params
   result = devise_parameter_sanitizer.for(:sign_up) do |u|
     u.permit(:email, :password, :password_confirmation, {:tag_list => []})         
   end
   result[:tag_list] = result[:tag_list].join(', ')
   result
 end 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM