
Originally Posted by
pjleonhardt
Hey there,
Easy questions for you guys (and gals). I've only been studying ruby/rails for about 2 days, so bare with me
In one of my controllers I'm doing something like:
def view_listing
id = params[:listing]
@listing = Listing.find(id)
end
basically, trying to show the details on a specific listing. If it is called like
website.com/controller/view_listing/219, i get params[:id] equal to 219, right?
Also, how do I verify that it is actually an int (sql safe.)
Thanks!
SQL safe is taken for you in most cases in rails. The only time you have to be careful is when using the conditions option, in which case you do :conditions => ["my_column = ?", params[:my_value]] and, of course, running any SQL queries yourself. If you want to catch errors, I usually do this:
Code:
def view_listing
@listing = Listing.find(params[:listing])
rescue ActiveRecord::RecordNotFound
render :action => 'page_not_found'
end
If you want to make sure that the user is accessing the method by get, you can do in your method (well, you should do this really because there is nothing that happens if it isn't get):
Code:
def view_listing
@listing = Listing.find(params[:listing]) if request.get?
end
Or, use the verify class method
Code:
verify :method => :get, :only => :view_listing, :redirect_to => :index
def view_listing
@listing = Listing.find(params[:listing])
end
Bookmarks