Rails 3 : How to execute action when render :action?
Rails 3 recommend Rest. For example, I make Blog system.
### Pos开发者_如何学编程tsController
# show detail
# tag have lock or unlock status
def show
@post = Post.find(params[:id])
@tags = @post.tags.select("posts_tags.tag_lock")
end
### CommentsController
# Posts#show has comment form.
# when you post comment, rails execute this action
def create
@post = @post.find(params[:id])
begin
@post.comments.create!(params[:comment])
resucue
@tags = @post.tags.select("posts_tags.tag_lock")
render 'posts/show'
end
end
if rails can execute action with render, resucue code is simple. Maybe, a solution is to make new action. but, specifications dont have new action... What is better? what about you??
Sorry, my english is bad..
I think you're fine doing it the way you are (fixed some typos):
### PostsController
# show detail
# tag have lock or unlock status
def show
@post = Post.find(params[:id])
@tags = @post.tags.select("posts_tags.tag_lock")
end
### CommentsController
# Posts#show has comment form.
# when you post comment, rails execute this action
def create
@post = Post.find(params[:id])
begin
@post.comments.create!(params[:comment])
rescue
@tags = @post.tags.select("posts_tags.tag_lock")
render 'posts/show'
end
end
精彩评论