1. You don't test to see if Review.find found anything...
2. You could use begin... rescue ... end to discover the specific error on
update_attribute, I think it should be plural
3. Are you using model.errors.any? , model.errors.empty?
4. Not familiar with your use of sanitize. I think I would evaluate params
with and without sanitize to see how the call is affecting your params
5. How are you managing 200 and 500? I think that you should return to
reviewname in event of a data error to inform the user.
On Wednesday, June 3, 2015 at 3:09:06 PM UTC-4, Ruby-Forum.com User wrote:
>
> Hey guys, I am a student and I got the memo (without explanation) that
> my following code can be much better. How would you improve my
> controller/reviewname method? Thank you for your time!
>
> class ReviewController < ApplicationController
>
> def index
> @reviews = Review.all.order(created_at: :desc)
> end
>
> def reviewname
> @review = Review.find(params[:id])
> if @review.update_attribute(:title, sanitize(params[:title]))
> format.json { render json: { status: 200 } }
> else
> format.json { render json: { status: 500 } }
> end
> end
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/b0bb3218-fde8-4a66-9f58-dd34acbe06d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.