我打算通过使用gem 'acts-as-taggable-on', '~> 5.0'
实现标记系统,根据Hartl的教程扩展toDoList Rails 5应用程序。在添加标签之前,该应用程序当前支持许多用户帐户以及注册和登录功能。我正在遵循此video来帮助我实现它。
我已经更新了Micropost模型,共享的micropost表单和主要的micropost表单,这些表单负责在用户的主页上显示标签。
但是,即使在创建没有错误的标签之后。标签不会显示。有人可以建议错误的来源吗?如果您需要有关我的申请的更多信息,请告诉我。
这是我更改的文件的代码。
Micropost模型
class Micropost < ApplicationRecord
belongs_to :user
attr_accessor :tag_list
acts_as_taggable
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :content, presence: true, length: {maximum: 140 }
end
shared / _micropost_form.html.erb(用于添加任务)
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :tag_list, "Tags (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content, placeholder: "Add new task..." %>
</div>
<%= f.submit "Add Task", class: "btn btn-primary" %>
<% end %>
Micropost的主要表单(_micropost.html.erb,以查看当前任务)
<li id="micropost-<%= micropost.id %>">
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<span class="user"><%= link_to micropost.user.name, micropost.user %></span>
<span class="content"><%= micropost.content %></span>
<p>
Tags: <%= micropost.tag_list %>
</p>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
<% if current_user?(micropost.user) %>
<%= link_to "Done", micropost, method: :delete,
data: { confirm: "Keep up the good work!" } %>
<% end %>
</span>
</li>
Micropost控制器
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
flash[:success] = "You have deleted a task!"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content, :tag_list)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
在一个示例帐户上(某些帖子具有相同的内容,但标签不同,但尚未解决该错误)
更新:我根据注释更改了代码,并收到了新的错误消息。行错误是指save
方法。