使用acts_as_taggable在Ruby on Rails 4中保存标记时遇到问题

时间:2013-10-24 04:18:22

标签: ruby-on-rails ruby ruby-on-rails-4 acts-as-taggable

我正在尝试将act_as_taggable实现到我的Rails 4应用程序中(在微博模型上),但程序没有保存标签,因此无法查看。标签和标签的表已经过调整并存在于数据库中,但是我提供的代码似乎都没有提交标签或在提交表单时创建标签。我完全按照tutorial附近的步骤进行了操作,但似乎没有用。

我不确定它是否是以rails 4为中心的问题和/或是否与rails中使用的'attr_accessible'代码相关。由于代码示例没有指定向微博表添加任何内容,我只能假设连接是在其他地方进行的,但我应该在哪里以及如何修复我不知道(在microposts_helper.rb中可能?)。

提前致谢。非常感谢任何帮助。

相关代码片段

的Gemfile

...

gem 'acts-as-taggable-on', '~> 2.4.1'

...

microposts.rb

   class Micropost < ActiveRecord::Base     
   belongs_to :user  
   acts_as_taggable   
   acts_as_taggable_on :tags
   ... 
   end

microposts_controller.rb

  before_action :signed_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy

  def index
    if params[:tag]
      @microposts = Micropost.tagged_with(params[:tag])
    else
      @microposts = Micropost.all
    end
  end

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to current_user
    else
      @feed_items = []
      render 'users/show'
    end
  end

  def destroy
    @micropost.destroy
    redirect_to user_url
  end

  def tagged
    if params[:tag].present? 
      @microposts = Micropost.tagged_with(params[:tag])
    else 
      @microposts = Micropost.postall
    end  
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content)
    end

    def correct_user
      @micropost = current_user.microposts.find_by(id: params[:id])
      redirect_to user_url if @micropost.nil?
    end
end

microposts_helper.rb

module MicropostsHelper
    include ActsAsTaggableOn::TagsHelper
end

_microposts_form.html.rb

<%= form_for(@micropost) do |f| %>
  ...
  <div class="field">
    ...
    <%= f.label :tags %>
    <%= f.text_field :tag_list %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

_micropost.erb.html

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="tags">
    <%= micropost.tag_list %>
  </span>
...
</li>

schema.rb

...
create_table "microposts", force: true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "microposts", ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
...
  create_table "taggings", force: true do |t|
    t.integer  "tag_id"
    t.integer  "taggable_id"
    t.string   "taggable_type"
    t.integer  "tagger_id"
    t.string   "tagger_type"
    t.string   "context",       limit: 128
    t.datetime "created_at"
  end

  add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id"
  add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"

  create_table "tags", force: true do |t|
    t.string "name"
  end
...

的routes.rb

Dev::Application.routes.draw do
    ...
    resources :microposts, only: [:create, :destroy]
    ...
    match 'tagged', to: 'microposts#tagged', :as => 'tagged', via: 'get' 
end

2 个答案:

答案 0 :(得分:4)

如果我正确理解了这个问题,那么你应该做的就是告诉建议的params(strong_parameters)关于act-as-taggable gem的保护模型。你已经半途而废了:

private

    def micropost_params
      params.require(:micropost).permit(:content)
    end

    [...]
end

只需添加:

private

    def micropost_params
     params.require(:micropost).permit(:content, :tag_list => [])
    end

    [...]
end

此外,您可能希望在routes.rb文件中添加一个内容。替换行:

resources :microposts, only: [:create, :destroy]

使用:

resources :microposts, only: [:create, :destroy, :tag]

请告诉我这些建议是否有效。祝你好运!

答案 1 :(得分:0)

我认为表单中标签的输入变量应为 tags_list 。即

<%= form_for(@micropost) do |f| %>
  ...
  <div class="field">
    ...
    <%= f.label :tags %>
    <%= f.text_field :tags_list %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

并更新_micropost.erb.html以使用 tags_list

或者您可以更新您的模型以使用标记属性,即'acts_as_taggable_on :tag'