gpt4 book ai didi

ruby-on-rails - 使用 acts_as_taggable 在 Ruby on Rails 4 中保存标签时遇到问题

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:31 27 4
gpt4 key购买 nike

我正在尝试将 act_as_taggable 实现到我的 Rails 4 应用程序中(基于微博模型),但该程序未保存标签,因此这些标签不可见。标签和标签表已被收集并存在于数据库中,但我实现的代码似乎都没有在提交表单时保存标签或创建标签。我按照此 tutorial 中的步骤操作几乎完全一样,但有些东西似乎不起作用。

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

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

相关代码片段

gem 文件

...

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

...

微博.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>

架构.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
...

路线.rb

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

最佳答案

如果我正确地理解了这个问题,那么您基本上需要做的就是告诉推荐的参数保护模型(strong_parameters)关于可作为标记的 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]

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

关于ruby-on-rails - 使用 acts_as_taggable 在 Ruby on Rails 4 中保存标签时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19556843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com