Ruby on rails 如何显示用户';s由同一标记关联的任务?
我正在通过RubyonRails创建一个todo应用程序。我创建了自己的标签模型,没有使用act_as_taggable gem。我使用Desive进行用户身份验证。我的一个特性是有一个页面,它可以显示用户的所有任务,这些任务都与标记相关。然而,我试图在tags controller中更改我的show和index方法,以合并当前用户,但它总是抛出这个错误 (未定义的方法<代码>标记代码>用于#您的意思是?标记点击): 我无法确定如何编辑代码以在当前用户中创建标记,并正确处理当前用户的标记列表 以下是相关代码: 任务模型Ruby on rails 如何显示用户';s由同一标记关联的任务?,ruby-on-rails,Ruby On Rails,我正在通过RubyonRails创建一个todo应用程序。我创建了自己的标签模型,没有使用act_as_taggable gem。我使用Desive进行用户身份验证。我的一个特性是有一个页面,它可以显示用户的所有任务,这些任务都与标记相关。然而,我试图在tags controller中更改我的show和index方法,以合并当前用户,但它总是抛出这个错误 (未定义的方法标记用于#您的意思是?标记点击): 我无法确定如何编辑代码以在当前用户中创建标记,并正确处理当前用户的标记列表 以下是相关代码
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
validates :item, presence: true,
length: { minimum: 5 }
belongs_to :user
validate :due_date_cannot_be_in_the_past
def self.tagged_with(name)
Tag.find_by!(name: name).tasks
end
def tag_list
tags.map(&:name).join(" ")
end
def tag_list=(names)
self.tags = names.split(" ").map do |name|
Tag.where(name: name).first_or_create!
end
end
def due_date
due.to_s
end
def due_date=(str)
self.due = Chronic.parse(str).to_date.to_s
rescue
@invalid_date = true
end
def validate
errors.add :due, 'is not a valid date' if @invalid_date
end
def due_date_cannot_be_in_the_past
if due.past?
errors.add(:due_date, "is not a valid date")
end
end
end
任务控制器:
def index
@incomplete_tasks = Task.where(complete: false)
@complete_tasks = Task.where(complete: true)
end
def show
@task = current_user.tasks.find(params[:id])
end
def new
@task = current_user.tasks.build
end
def edit
@task = Task.find(params[:id])
end
def create
@task = current_user.tasks.build(task_params)
if @task.save
redirect_to @task
else
render 'new'
end
end
def update
@task = Task.find(params[:id])
if @task.update(task_params)
redirect_to @task
else
render 'edit'
end
end
def destroy
@task = Task.find(params[:id])
@task.destroy
redirect_to tasks_path
end
def complete
@task = current_user.tasks.find(params[:id])
@task.update_attribute(:complete, true)
flash[:notice] = "Task Marked as Complete"
redirect_to tasks_path
end
private
def task_params
params.require(:task).permit(:item, :description, :tag_list, :due)
end
end
def show
@tag = current_user.tag.find(params[:id])
end
def index
@tag = current_user.tag.all
end
标签控制器:
def index
@incomplete_tasks = Task.where(complete: false)
@complete_tasks = Task.where(complete: true)
end
def show
@task = current_user.tasks.find(params[:id])
end
def new
@task = current_user.tasks.build
end
def edit
@task = Task.find(params[:id])
end
def create
@task = current_user.tasks.build(task_params)
if @task.save
redirect_to @task
else
render 'new'
end
end
def update
@task = Task.find(params[:id])
if @task.update(task_params)
redirect_to @task
else
render 'edit'
end
end
def destroy
@task = Task.find(params[:id])
@task.destroy
redirect_to tasks_path
end
def complete
@task = current_user.tasks.find(params[:id])
@task.update_attribute(:complete, true)
flash[:notice] = "Task Marked as Complete"
redirect_to tasks_path
end
private
def task_params
params.require(:task).permit(:item, :description, :tag_list, :due)
end
end
def show
@tag = current_user.tag.find(params[:id])
end
def index
@tag = current_user.tag.all
end
如果需要任何其他信息来澄清此问题,请务必告诉我。您好。请阅读这篇文章,了解嵌套属性是如何工作的。那么tag_list是任务的嵌套属性吗?是的,应该是。如果您能进一步解释为什么tag_list是任务的嵌套属性,为什么它不是任务的嵌套属性的标记,是否可能?我现在得到了这个错误:验证失败:用户必须存在,用户不能为空