2014-10-30 25 views
0

更新:答案正确。只是想更新我所做的解决问题。 首先,我必须删除轨道控制台中的所有以前的行。行中的NoMethodError#show

然后,我在创建方法底部的行控制器中使用了再见错误宝石来发现下一个错误发生的位置。我创建了一个测试线,我需要再次删除。所以我跑了

Line.last.delete in console。

这是我的台词控制器创建方法现在看起来

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 {方式(工作没有bug)|线| line.update.attribute(:story_id:3)}

这给了行和故事之间必要的联系。

ORIGINAL POST BELLOW。

我在我的rails应用程序中收到这个错误。我认为,当我创建一个新行或开始一个故事时,它不会自动将它添加到故事对象。我列出了我的show.html.erb文件以及我的行controller.rb文件。

我错过了什么?我如何让控制器正确地向故事对象添加数据?

谢谢!

enter image description here

我添加的几行代码到我的线控制器:

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 
+1

这是很难没有看到你的模型说。检查你的'Line'模型是否有'belongs_to:story'和'Story'有'has_many:collaborators'。 – blelump 2014-10-30 18:45:02

+0

编辑:我将故事模型发布到原始文章的底部 – user3787971 2014-10-30 18:55:20

+0

您会在show动作中显示'@lines = Line.find(params [:id])。collect_lines'中的'collect_lines'方法吗? – nikkon226 2014-10-30 19:00:23

回答

1

问题是孤立的数据库中的行。看看他们和其关联到一个故事,或者将其删除:

如何找到孤立的记录: 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 
+0

我是否需要将其添加到故事模型或线条模型中? – user3787971 2014-10-30 19:10:23

+0

事实是,这只是一个例子。问题是你正在尝试访问lines.last.story,并且故事没有为这个lines.last设置。你必须找出为什么你没有故事,但你仍然在寻求合作者。 – tebayoso 2014-10-30 19:12:50

+0

好的,谢谢,我是一般的编程新手,所以这需要一段时间,但我想我会弄清楚。 – user3787971 2014-10-30 19:14:01

相关问题