允许用户使用acts-as-taggable-on在注册表单上选择标签 [英] Allowing users to select tags on sign up form using acts-as-taggable-on

查看:213
本文介绍了允许用户使用acts-as-taggable-on在注册表单上选择标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让用户在使用devise注册时从预定义的标签中进行选择?

Is there a way to allow the users to select from the predefined tags while signing up with devise?

registrationrations / new.html.erb

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

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

这是我到目前为止我的注册页面。我需要做什么,当用户填写电子邮件,密码并检查必需的标签和点击注册时,该标签将被保存给该用户?真的很感谢你们的帮助。谢谢

This is what i got so far for my sign up page. What do i need to do so that when user fills in the email, password and checks necessary tags and clicks signup, the tags gets saved for that user? Would really appreciate your help guys. Thanks

推荐答案

tag_list必须是一个数组。由于 doc 说:

tag_list must be an array. As the doc says:


数组不是强参数的允许标量之一,所以我们
需要配置Devise。

An array is not one of Strong Parameters' permitted scalars, so we need to configure Devise.

在你的情况下,这将是这样的:

In your case it would be something like this:

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_list 。 act-as-taggable只发送tag_list。您的迁移找到问题有用

I don't think you need tag as well as tag_list. act-as-taggable only sends tag_list. You migth find this question useful.

更新:

 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 

这篇关于允许用户使用acts-as-taggable-on在注册表单上选择标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆