2014-10-30 11 views
0

업데이트 : 아래 답변이 정확합니다. 문제를 해결하기 위해 내가 한 일을 업데이트하고 싶었습니다. 먼저 레일즈 콘솔에서 모든 이전 라인을 삭제해야했습니다.NoMethodError in Lines # show

그런 다음 create 메서드의 맨 아래에있는 내 회선 컨트롤러에서 bye 버그 보석을 사용하여 다음 버그가 발생한 위치를 확인했습니다. 다시 삭제해야하는 테스트 라인을 만들었습니다. 그래서 나는 달렸다

콘솔의 Line.last.delete. | 라인 |

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 {@ 실행 (어떤 버그를 작업 없음) 내 라인 컨트롤러 이제 방법을 보인다 만드는 방법 line.update.attribute (: story_id : 3)}

이것은 줄과 이야기 사이에 필요한 연관성을 부여했습니다.

원본 게시판.

내 레일 앱에서이 오류가 발생합니다. 나는 새로운 라인을 만들거나 스토리를 시작할 때 스토리 오브젝트에 자동으로 추가하지 않는다고 생각합니다. 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 파일 액티브 :: 자료

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

+0

EDIT : 원래 게시물의 하단에 스토리 모델을 게시했습니다. – user3787971

+0

'@lines = Line.find (params [: id]). collect_lines'에 show_action_line을 넣으시겠습니까? – nikkon226

답변

1

문제는 데이터베이스의 행을 고아

클래스 라인 <입니다. 그것을 그들을 위해보고 이야기를 연관 또는 삭제 :

분리 된 레코드를 찾는 방법 :

#short example review activerecord relations 

@story = Story.find(params[:story_id]) 
story.lines.create(line_params) 
: http://antonzolotov.com/2013/01/26/how-to-find-and-delete-orphaned-records-with-ruby-on-rails.html

이 그런 이야기의 일부가되어야 라인을 보장하기 위해 작성 방법을 검토

그래야합니다.

편집 :

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

+0

사실 이것은 단지 예일뿐입니다. 문제는 lines.last.story에 액세스하려고 시도하고 있으며 스토리가이 줄에 설정되어 있지 않다는 것입니다. 왜 당신은 이야기가 없는지 알아야하지만, 여전히 공동 작업자를 요구하고 있습니다. – tebayoso

+0

좋아, 고맙다, 나는 코딩에 익숙하지 않으므로 잠시 시간이 걸릴 것이다. 그러나 나는 그것을 알아낼 것이라고 생각한다. – user3787971