更新:下面的答案是正确的。只是想更新一下我为解决问题所做的一切。首先,我必须删除rails控制台中前面的所有行。
然后,我在创建方法底部的行控制器中使用了bug来发现下一个bug发生的位置。我创建了一个需要再次删除的测试行。所以我跑了
控制台中的Line.last.delete。
这就是我的行控制器create方法现在看起来的样子(没有bug)。
def create
if user_signed_in?
@line = Line.create(line_params)
if @line
if params[:line][:previous_line_id].empty?
@line.story = Story.create
@line.save
else
@line.story = @line.previous_line.story
@line.save
end
redirect_to line_path(@line)
else
flash[:error] = @line.errors
redirect_to line_path(Line.find(params[:line][:previous_line_id]))
end
else
最后,我运行了@Lines.each {AC.26 linesline.update.attribute(:story_id: 3)} }
这给了故事和台词之间必要的联系。
原始波纹管。
我的rails应用程序中出现了这个错误。我认为,当我创建一个新的行或开始一个故事,它不会自动添加到一个故事对象。我列出了我的show.html.erb文件和行controller.rb文件。
我遗漏了什么?如何让控制器正确地将数据添加到故事对象中?
谢谢!
我在行控制器中添加了几行代码:
class LinesController < ApplicationController
def new
params[:previous_line_id].nil? ? @line = Line.new : @line = Line.find(params[:previous_line_id]).next_lines.create
@lines = @line.collect_lines
@ajax = true if params[:ajax]
render :layout => false if params[:ajax]
if @line.previous_line
@line.update_attribute(:story_id, @line.previous_line.story.id)
else
story = Story.create
@line.story = story
@line.save
end
end
def create
if user_signed_in?
@line = Line.create(line_params)
if @line
redirect_to line_path(@line)
else
flash[:error] = @line.errors
redirect_to line_path(Line.find(params[:line][:previous_line_id]))
end
else
flash[:error] = "Please sign in or register before creating a line!"
unless params[:line][:previous_line_id].empty?
redirect_to line_path(Line.find(params[:line][:previous_line_id]))
else
redirect_to root_path
end
end
end
# params[:id] should correspond to the first line of the story.
# if params[:deeper_line_id] is not nil, that means that they want to render up to the nested line id
def show
@lines = Line.find(params[:id]).collect_lines
@next_lines = @lines.last.next_lines.ranked
@lines.last.update_attribute(:score, @lines.last.score + 1)
end
def select_next
@line = Line.find(params[:id])
@line.update_attribute(:score, @line.score + 1)
@lines = [@line]
@next_lines = @line.next_lines.ranked
render :layout => false
end
def send_invite
if user_signed_in?
UserInvite.send_invite_email(current_user,Line.find(params[:id]), params[:email]).deliver
flash[:notice] = "Your invite was sent!"
else
flash[:error] = "Please sign in"
end
redirect_to Line.find(params[:id])
end
private
def line_params
params.require(:line).permit(:text, :previous_line_id, :user_id)
end
end
我把这些线加到上面的控制器上。
if @line.previous_line
@line.update_attribute(:story_id, @line.previous_line.story.id)
else
story = Story.create
@line.story = story
@line.save
end
这是我的show.html.erb文件
<div class="row">
<div class="col-lg-2">
</div>
<div class="box-container col-lg-7 ">
<div id="story" class="box">
<% @lines.each do |line| %>
<span class="story-line" data-id="<%=line.id%>"><%= link_to line.text, '#', :class=>"story-line" %></span>
<% end %>
</div>
<div id="next-steps">
<%= render 'next_steps' %>
</div>
<span style="font-size:.9em; margin-bottom:15px; display:block;">*If the links don't work, try refreshing.</span>
</div>
<div class="col-lg-2" style="padding-right:25px;">
<%= render 'invite' %>
Your Fellow Collaborators: <br />
<div class="collaborators">
<% @lines.last.story.collaborators.uniq.each do |collaborator| %>
<%= link_to profile_path(:id => collaborator.id) do %>
<%= image_tag collaborator.profile_image_uri, :class => "prof-icon" %>
<% end %>
<% end %>
</div>
故事模型
class Story < ActiveRecord::Base
has_many :lines
has_and_belongs_to_many :collaborators, :class_name => "User", :join_table => "collaborators_stories", :association_foreign_key => :collaborator_id
def first_line
self.lines.first_lines.first_lines.first
end
end
这是我的lines.rb文件
类行< ActiveRecord::Base
scope :first_lines, -> { where previous_line_id: nil}
scope :ranked, -> { order("score + depth DESC")}
belongs_to :user
belongs_to :story
belongs_to :previous_line, :class_name => "Line", :foreign_key => "previous_line_id"
has_many :next_lines, :class_name => "Line", :foreign_key => "previous_line_id"
validates_presence_of :text
after_create :update_depths
def update_depths
line = self.previous_line
while !line.nil?
line.update_attribute(:depth, line.depth + 1)
line = line.previous_line
end
end
def first_line
line = self
while !line.previous_line.nil?
line = line.previous_line
end
line
end
def collect_lines
line = self
lines = [self]
while !line.previous_line.nil?
lines.unshift(line.previous_line)
line = line.previous_line
end
lines
end
end
发布于 2014-10-30 19:05:04
问题是数据库中的孤立行。寻找它们并将其与一个故事关联起来,或者删除它:
如何查找孤立的记录:http://antonzolotov.com/2013/01/26/how-to-find-and-delete-orphaned-records-with-ruby-on-rails.html
然后检查创建方法,以确保一行应该是故事的一部分:
#short example review activerecord relations
@story = Story.find(params[:story_id])
story.lines.create(line_params)
那应该管用。
编辑:
def self.find_orphan_ids
Lines.where([ "user_id NOT IN (?) OR story_id NOT IN (?)", User.pluck("id"), Story.pluck("id") ]).destroy_all
end
https://stackoverflow.com/questions/26660873
复制相似问题