Pregunta

Aquí está mi prueba en mi controlador:

describe "GET index" do
  it "assigns all comments as @comments" do
    comment = Comment.create! valid_attributes
    get :index, { :url => "http://localhost:3000", :use_route => :good_comments}
    assigns(:comments).should eq([comment])
  end
end

Cuando ejecuto la prueba, tiene diferentes valores para :comments y comment. El primero es lo que uno esperaría:

#<MyComments::Comment _id: BSON::ObjectId('4f0f7c41516d9f5a4a000001'), comment: "Some comment", created_at: Fri, 13 Jan 2012 00:35:13 UTC +00:00, images: [], resource_hash: "aHR0cDovL2xvY2FsaG9zdDozMDAwLw==", updated_at: Fri, 13 Jan 2012 00:35:13 UTC +00:00, urls: [], user_id: nil>

El segundo es simplemente extraño:

#<Plucky::Query sort: [["created_at", -1]], transformer: #<Proc:0x007fe86e3184c8@/Users/shamoon/.rvm/gems/ruby-1.9.2-p290@global/gems/mongo_mapper-0.10.1/lib/mongo_mapper/plugins/querying.rb:79 (lambda)>

No estoy seguro de lo que está pasando, por lo que cualquier ayuda sería muy apreciada.

Gracias =)

Editar: Agregar código del controlador

# POST /comments
# POST /comments.json
def create
  @comment = Comment.new params[:comment]
  respond_to do |format|
    if @comment.save
      format.html { redirect_to :back, notice: 'Comment was successfully created.' }
      format.json { render json: @comment, status: :created, location: @comment }
    else
      format.html { render action: "new" }
      format.json { render json: @comment.errors, status: :unprocessable_entity }
    end
  end
end

# GET /comments
# GET /comments.json
def index
  url = ""
  if params[:url]
    url = params[:url]
  end
  resource_hash = Comment.encode_url url

  @newComment = Comment.new
  @newComment.resource_hash = resource_hash

  @comments = Comment.find_all_by_resource_hash resource_hash
  @comments = Comment.sort(:created_at.desc)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @comments }
  end
end

Editar: acción de índice actualizado

def index
  url = ""
  if params[:url]
    url = params[:url]
  end
  resource_hash = Comment.encode_url url

  @newComment = Comment.new
  @newComment.resource_hash = resource_hash

  @comments = Comment.where(:resource_hash => resource_hash).sort(:created_at.desc)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @comments }
  end
end
¿Fue útil?

Solución

@comments = Comment.find_all_by_resource_hash resource_hash
@comments = Comment.sort(:created_at.desc)

Ese es su problema, está sobrescribiendo la variable @comments en la segunda línea, intente esto para comenzar, no estoy seguro de si le permitirá llamar a eso, pero bien, ver:

@comments = Comment.find_all_by_resource_hash resource_hash
@comments = @comments.sort(:created_at.desc)

ACTUALIZAR

Intenta hacer esto:

@comments = Comment.where(:resource_hash => resource_hash).sort(:created_at.desc).all
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top