开发者

Nested resource on validation error will allow edit on all children model

开发者 https://www.devze.com 2023-03-24 10:01 出处:网络
I have the following relationship: :discussion has_many :posts, and I have the Post model nested inside the Discussion.

I have the following relationship: :discussion has_many :posts, and I have the Post model nested inside the Discussion.

The discussions#show view has a form allowing users to create post (reply) on that discussion.

In my update action I have:

if @discussion.update_attributes(params[:discussion])
  format.html { redirect_to(@discussion, :notice => 'success') }
else
  format.html { render :action => "edit" }
end
开发者_如何学JAVA

I used field_for :posts, @post to A) allow nested model update B)restrict the form to only allow single post create.

However if the validation fails, the page will show all posts belonging to the form in editable form. I only want to allow the user to re-edit the single post. Is there a good way to achieve this?

If validation fails, the post is not saved and I don't know how to pin-point the particular new post.

I was thinking maybe I shouldn't create a post through discussion form, since it is like a layer of overhead. But now I have nested it, I can no longer route to the posts/new view.


This is the way I came up with to only allow single post edit:

else
  format.html { 
    @post = @discussion.posts.last
    render :action => "edit"
  }

It's a bit hackish, hackers can still send hacked POST messages and alter other posts. So better and clean solutions are welcomed.

0

精彩评论

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