2014-10-30 28 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

相關問題