开发者

Rails 3 : How to execute action when render :action?

开发者 https://www.devze.com 2023-03-11 18:14 出处:网络
Rails 3 recommend Rest. For example, I make Blog system. ### Pos开发者_如何学编程tsController # show detail

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
0

精彩评论

暂无评论...
验证码 换一张
取 消