Hi Emiel,
On 12.2.2005, at 22:43, Emiel van de Laar wrote:>
> And with that I can pick up the post id in the comment controller like
> so:
>
> def new
> Comment.new
> Comment.post_id = @params[''post_id'']
> end
Here''s an alternative, maybe a bit more OO way for that:
@comment = Comment.new(@params["comment"])
@post = Post.find(@params["post_id"])
@post.comments << @comment
>
> It works, but it''s not very pretty with urls like
> /comment/new?post_id=2
Well, do you think this would be prettier:
> <%= link_to "Leave a comment.", :controller =>
"comment",
> :action => "create", :id => @post.id %>
Then the url would be /comment/create/2
Anyway, I wouldn''t sweat too much about that comment creation url.
It''s
not like anyone would link to it or that google would need to index it.
>
> I know there must be a better (rails) way to do this. One perhaps
> being:
>
> flash["post_id"] = @post.id
Nah, that''s abusing the flash method. I would strongly suggest relaying
the id in the query vars (or in the url as above), that''s always the
safest bet.
//jarkko
>
> What I had in mind was perhaps the following based on the scaffolding
> code. Using the follwing link:
>
> <%= link_to "Leave a comment.", :controller =>
"comment",
> :action => "create", ...
>
> def create
> # create the comment with the correct post id
> # and redirect directly to editing it.
> end
Why would you want to create it before the user has written anything?
You''d probably end up with a lot of bogus items in your comments. Just
make a comment form page (or put the form in your post view page) and
point the form to your create method. You can sneak the post_id in in a
hidden form variable.
//jarkko
--
Jarkko Laine
http://jlaine.net
http://odesign.fi
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails