AlwaysCharging
2009-Dec-19 20:20 UTC
What''s the "update" equivalent to the "build" method?
Currently I''m using .build in my Create action so that I have a little
more control before it gets saved. For instance, I have:
@comment = current_user.comments.build(params[:comment])
@comment.blog_comment = @comment.blog_comment[0...140]
to chop the blog_comment off at 140 characters. Well, that works
great, but if a user goes to edit that comment, they can then make the
comment longer than 140 characters since I''m using the following in
the update action:
@comment = Comment.find(params[:id])
if @comment.update_attributes(params[:comment])
So, how do I interject and chop off the comment at 140 characters when
a user decides to update their comment?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Rick DeNatale
2009-Dec-19 20:34 UTC
Re: What''s the "update" equivalent to the "build" method?
On Sat, Dec 19, 2009 at 3:20 PM, AlwaysCharging <goodgets-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Currently I''m using .build in my Create action so that I have a little > more control before it gets saved. For instance, I have: > @comment = current_user.comments.build(params[:comment]) > -I4CJOCdi0N4RIhToyOJVgQ@public.gmane.org_comment = @comment.blog_comment[0...140] > > to chop the blog_comment off at 140 characters. Well, that works > great, but if a user goes to edit that comment, they can then make the > comment longer than 140 characters since I''m using the following in > the update action: > > @comment = Comment.find(params[:id]) > if @comment.update_attributes(params[:comment]) > > So, how do I interject and chop off the comment at 140 characters when > a user decides to update their comment?One way might be to alter the value first with something like: @comment = Comment.find(params[:id] comment_attributes = params[:comment] comment_attributes[''blog_comment''] &&comment_attributes[''blog_comment''][0...140] if @comment.update_attributes(comment_attributes) Something similar could also be done in the create case, in which case I''d factor out the two lines calculating comment_attributes into a controller method. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Emery Finkelstein
2009-Dec-19 20:58 UTC
Re: What''s the "update" equivalent to the "build" method?
Seems like the perfect place for a call back if you''re doing this
without discriminating.
class Comment < ActiveRecord::Base
...
before_validation :trim_blog_comment
protected
def block_comment
self.blog_comment = blog_comment[0..140] if blog_comment.length >
140
end
end
Now it''s being trimmed at the model level on all creates/saves/
updates, no matter where the call to save comes from.
On Dec 19, 3:20 pm, AlwaysCharging
<goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Currently I''m using .build in my Create action so that I have a
little
> more control before it gets saved. For instance, I have:
> @comment = current_user.comments.build(params[:comment])
> @comment.blog_comment = @comment.blog_comment[0...140]
>
> to chop the blog_comment off at 140 characters. Well, that works
> great, but if a user goes to edit that comment, they can then make the
> comment longer than 140 characters since I''m using the following
in
> the update action:
>
> @comment = Comment.find(params[:id])
> if @comment.update_attributes(params[:comment])
>
> So, how do I interject and chop off the comment at 140 characters when
> a user decides to update their comment?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
AlwaysCharging
2009-Dec-20 02:23 UTC
Re: What''s the "update" equivalent to the "build" method?
This is perfect! Thank you Emery. I was thinking the same thing, but was trying before_save filters and couldn''t get it to work for the Update action. Honestly, I didn''t even know about the before_validation helper, guess I got some more learnin'' to do. Thanks again. And @Rick, thank you for the suggestion as well. On Dec 19, 3:58 pm, Emery Finkelstein <emery.finkelst...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Seems like the perfect place for a call back if you''re doing this > without discriminating. > > class Comment < ActiveRecord::Base > ... > before_validation :trim_blog_comment > > protected > def block_comment > self.blog_comment = blog_comment[0..140] if blog_comment.length > > 140 > end > end > > Now it''s being trimmed at the model level on all creates/saves/ > updates, no matter where the call to save comes from. > On Dec 19, 3:20 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Currently I''m using .build in my Create action so that I have a little > > more control before it gets saved. For instance, I have: > > @comment = current_user.comments.build(params[:comment]) > > @comment.blog_comment = @comment.blog_comment[0...140] > > > to chop the blog_comment off at 140 characters. Well, that works > > great, but if a user goes to edit that comment, they can then make the > > comment longer than 140 characters since I''m using the following in > > the update action: > > > @comment = Comment.find(params[:id]) > > if @comment.update_attributes(params[:comment]) > > > So, how do I interject and chop off the comment at 140 characters when > > a user decides to update their comment?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.