使用acts_as_taggable在Ruby on Rails 4中保存标签 [英] Trouble saving tags in Ruby on Rails 4 with acts_as_taggable

查看:119
本文介绍了使用acts_as_taggable在Ruby on Rails 4中保存标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Rails 4应用程序中实现act_as_taggable(在microposts模型上),但是该程序没有保存标签,因此无法查看.用于标记和标记的表已经倾斜并且存在于数据库中,但是我实现的代码似乎都没有在提交表单时保存标记或创建标记的.我完全遵循了教程中的步骤,但有些东西似乎不起作用.

I'm trying to implement act_as_taggable into my Rails 4 app (upon the microposts model), but the program isn't saving the tags, which therefore aren't viewable. The tables for tags and taggings have been raked and exist in the database, but none of the code I'm implemented seems to save a tag or create a tag when the form is submitted. I followed the steps in this tutorial near exactly, but something doesn't seem to work.

我不确定这是否是Rails 4的中心问题,并且/或者是否与Rails中缺少"attr_accessible"代码有关.由于该代码示例未指定向microposts表添加任何内容,因此我只能假定该连接是在其他地方建立的,但是我应该在哪里以及如何修复我不知道的连接(也许在microposts_helper.rb中?).

I'm not sure if it's a rails 4 centric problem and/or is connected to the lack of 'attr_accessible' code used in rails. Since the code example doesn't specify adding anything to the microposts table, I can only assume the connection is made elsewhere, but where and how I should fix that I don't know (in microposts_helper.rb perhaps?).

先谢谢了.任何帮助将不胜感激.

Thanks in advance. Any help is greatly appreciated.

相关代码段

宝石文件

...

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

...

microposts.rb

microposts.rb

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

microposts_controller.rb

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

microposts_helper.rb

module MicropostsHelper
    include ActsAsTaggableOn::TagsHelper
end

_microposts_form.html.rb

_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

_micropost.erb.html

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

schema.rb

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

routes.rb

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

推荐答案

如果我正确地理解了这个问题,那么从本质上讲,您所需要做的就是告诉所推荐的params(strong_parameters)保护模型关于行为的信息-可标记的宝石.您已经在其中途了:

If I understand the question correctly, all you should need to do is, essentially, tell the recommended protection model for params(strong_parameters) about the acts-as-taggable gem. Your already Halfway there with this:

private

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

    [...]
end

只需添加:

private

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

    [...]
end

此外,您可能想在自己的route.rb文件中添加一件事.替换行:

Also, you may want to add one thing to your routes.rb file.. Replace the line:

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

具有:

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

让我知道这些建议是否奏效.祝你好运!

Let me know if these suggestions do the trick. Good luck!

这篇关于使用acts_as_taggable在Ruby on Rails 4中保存标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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