저는 레일을 처음 사용하기 때문에 여기에서 도움을 얻을 수 있습니다. 나는 몇몇 튜토리얼을 따라 코멘트와 심지어는 AJAX 종소리와 호루라기가있는 블로그를 만들었고 나는 희망하는 것을 붙잡고있다. 블로그와 댓글의 기본 표시는 가장 오래된 것을 먼저 나열하는 것입니다. 가장 최근의 항목과 가장 최근의 주석을 맨 위에 표시하려면 어떻게해야합니까? 이것이 컨트롤러 나 모델의 기능인지 정말로 모릅니다. 약간의 커스터마이징을 했으므로 여기서는 컨트롤러 .rb 파일의 코드가 도움이된다.Ruby on Rails 블로그 항목 및 댓글의 역순 정렬
코멘트 CONTROLLER
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create!(params[:comment])
respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
end
게시물 컨트롤러는 find
를 사용하여 주문을 취소 할 수 있습니다처럼 보이는
class PostsController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
# GET /posts
# GET /posts.xml
def index
@posts = Post.all(:include => :comments)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.json { render :json => @posts }
format.atom
end
end
# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new
respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(@post) }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to(@post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
private
def authenticate
authenticate_or_request_with_http_basic do |name, password|
name == "admin" && password == "secret"
end
end
end
이것은 정말 도움이되는 대답이었고 효과가있었습니다. 한 번 더 questin (미안). show.html.erb에서 작동하도록 AJAX 설정을했습니다. 하단에 대신 새로운 댓글을 추가 할 수는 없습니다. 나는 몇 가지를 시도했지만 빠른 단어는 여기에? – bgadoci
AJAX를 어떻게 지내십니까? RJS 또는 ??? 블로그 자습서를 계속 따르는 경우 create.js.rjs의 첫 번째 줄 맨 아래 : 맨 위로 변경하고 싶습니다. JS를 다른 것으로 바꾸면 새로운 질문을 게시 할 가치가 있습니다. –
이것들은'Comment' 모델 자체 내에서'default_scope' 메소드를 사용하는 세 번째 옵션입니다. 'default_scope : order => 'created_at desc'' –