Rails 3推荐Rest。例如,我制作博客系统。
### 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])
resucue
@tags = @post.tags.select("posts_tags.tag_lock")
render 'posts/show'
end
end
如果rails可以使用render执行操作,那么resucue代码很简单。也许,一个解决方案是采取新的行动。但是,规范没有新的作用。。。什么更好?你呢??
对不起,我的英语不好。。
我认为你现在这样做很好(修复了一些拼写错误):
### 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