行#中的NoMethodError显示

时间:2014-10-30 18:38:38

标签: ruby-on-rails ruby

更新:答案是正确的。只是想更新我为解决问题所做的工作。 首先,我必须删除rails控制台中以前的所有行。

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

控制台中的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 | line.update.attribute(:story_id:3)}

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

原始邮政信箱。

我在我的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文件

class Line&lt;的ActiveRecord ::基

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 个答案:

答案 0 :(得分:1)

问题是数据库中的孤立行。寻找他们并将其与故事相关联,或将其删除:

如何查找孤立记录: http://antonzolotov.com/2013/01/26/how-to-find-and-delete-orphaned-records-with-ruby-on-rails.html

然后查看create方法以确保一行应成为故事的一部分:

#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