tag:blogger.com,1999:blog-50700365998889446782024-03-13T03:26:16.524-07:00Ruby On RailsRyan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-5070036599888944678.post-9857824444604081612009-05-13T03:11:00.000-07:002009-05-13T03:19:30.069-07:00Rich-Text Textarea Editor in Rails Using WidgOften your users might need to enter rich-text in your program: whether it be for updating their profile or leaving comments on your blog, it's common functionality. Using WidgEditor will help you make this easy. There are plugins for various other editors, but so far I've found this simple method the most stable.<br /><br />1. Download <a href="http://www.themaninblue.com/experiment/widgEditor/">widgEditor</a> (latest version)<br />2. Edit the widgEditor.js file as shown below if using AJAX<br />3. Copy the widgEditor.js file into your public/javascripts folder<br />4. Copy the widgEditor.css file into your applications public/stylesheets folder.<br />5. Copy all the images from the widgEditor folder to your public/images folder.<br />6. Include the javascript and css files in your layout file.<br />7. Give any text field you want to be an editor field a class of “widgEditor”.<br />Example:<br />%= stylesheet_link_tag 'widgEditor' %><br />%= javascript_include_tag "widgEditor" %><br />%= text_area 'profile', 'about', :cols => "50", :rows=>"4", :class=>"widgEditor" %><br /><br /><h3>Using AJAX and WidgEditor</h3><br />You'll need to modify widgEditor.js for AJAX. WidgEditor modifies your form submit function to ensure that a clean HTML copy of your input gets submitted. You need to change this so that it handles our AJAX/Javascript forms. In the widgEditor.prototype.modifyFormSubmit function, change the line that reads:<br />from: oldOnsubmit = theForm.onsubmit;<br />to: theForm.oldOnsubmit = theForm.onsubmit;<br /><br />Then just a few lines below in the new onSubmit function, change the oldOnsubmit() function call to<br />from: return oldOnsubmit();<br />to: return theForm.oldOnsubmit()Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comtag:blogger.com,1999:blog-5070036599888944678.post-16167794568166700372008-11-07T13:31:00.000-08:002008-11-07T13:37:27.450-08:00Rails GuidesThe Ruby on Rails team and many Rails enthuisiasts have been working hard on a new site for <a href="http://guides.rubyonrails.org/">guides</a> for Rails. They range from guides for getting started, to guides covering more of the specifics. The <a href="http://guides.rubyonrails.org/getting_started_with_rails.html">getting started</a> guide covers a lot, but it can look a little length to the newcomer - perhaps it should be broken into sections. It does, though, cover each of the pieces and paradigms used in Rails, making learning the language fully rather easy. For example, it covers the Model View Controller paradigm used in Rails. Models are representations of the data we used, Views are displays of that data, and the Controller interacts with the user. Then it goes on to talk about all the pieces of rails, ranging from Active Record (for Models) through Active Support (for all the little things).Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comtag:blogger.com,1999:blog-5070036599888944678.post-16119392517392257282008-11-05T14:26:00.000-08:002008-11-05T14:32:02.736-08:00Rails Dynamic LinksSince I've been working on a program for Mindbridge for a while, (and <a href="http://blog.ryankopf.com">you can see how busy</a> I am), I try occasionally to post useful things I remember here. One is about Rails dynamic linking abilities. I recently forgot, then remembered again, about how easy it is to link to an object.<br /><br />(%= link_to "View", @object %) instead of (%= link_to "View", object_path(@object) %)<br /><br />You can use a handy <a href="http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf">REST cheat sheet</a> to keep it all handy. It helps me remember that I have to explicitly declare the delete method, for example, in my links.Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comtag:blogger.com,1999:blog-5070036599888944678.post-31961562312237413302008-10-21T14:21:00.000-07:002008-10-21T14:29:05.147-07:00Rails CaptchaI just finished a lot of improvement on the custom Captcha implementation I use on all my websites. Right now the captcha protects comment forms and user profiles to prevent abuse by spammers, and it's very effective. Spammers obviously can't post spam without actually reading and interpreting my images as letters, or without having access to my MySQL database.<br /><br /><strong>How It Works</strong><br />The design for my custom captcha for rails is simple enough. Whenever the user request a page displaying a captcha-protected form, a captcha object is created in the database, which has a randomly generated private key, and I use it's id number as the public key. If the inputter does not submit the correct private key along with the public key (which is stored in a hidden value on the form), then they are given an error message. The user can see the private key, if they are a human, through a gif image which includes the letters A-L as part of the 5-digit key. The letters are blurred a little using a few RMagick functions and a few calls to random, and then the resulting image is given through rails in response to a .gif format request.<br /><br />My improvements recently were rather simple. I first realized I had way too much code in the controller, and I moved most of it to methods in the Captcha.rb model method. Then I used a collect function and some loops to simplify things that were repeated, trying to follow along with DRY principals. Now I'm pretty sure that the program runs a little smoother and bugs will be easier to iron out.Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comtag:blogger.com,1999:blog-5070036599888944678.post-64361670424765180702008-08-07T13:55:00.000-07:002008-08-07T14:00:25.285-07:00Rails Development and HostingHere are some great posts about rails development, hosting, hiring, etc:<br /><br /><br /><br /><strong>Ruby on Rails Hosting</strong><br>An overview of some Ruby on Rails development and hosting companies.<br><a href='http://www.associatedcontent.com/article/937819/ruby_on_rails_hosting.html'>View more »</a><br /><br /><br /><br /><strong>11 Tips on Hiring a Rails Developer</strong><br />Tips include looking for people who have contributed to the community, and more.<a href="http://www.rubyinside.com/11-tips-on-hiring-a-rails-developer-662.html">View more »</a><br /><br /><br /><br /><strong>Hosting Rails</strong><br />A very cheap hosting company that offers mongrel for as little as $10 per month.<a href="http://www.hostingrails.com/">View more »</a><br />Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comtag:blogger.com,1999:blog-5070036599888944678.post-75096416113191180462008-08-01T09:34:00.000-07:002008-08-01T09:42:10.107-07:00Custom 404 Pages - Rails 404 StatusSometimes your application hits it where it doesn't know what to render. Perhaps your user tried to access so URL from memory and spelled it wrong. Maybe they tried /user/john instead of /users/john. Whatever happens, they'll be getting some sort of error. Fortunately for you, you can control this error without much work. Add this to the bottom of your routes.rb file.<br /><br /><span style="font-style: italic;">map.connect '*path', :controller => 'application', :action => 'rescue_404' unless ::ActionController::Base.consider_all_requests_local</span><br /><br />Then add to your application.rb<br /><br /><pre><span style="font-style: italic;">def rescue_404</span><br /><span style="font-style: italic;"> rescue_action_in_public CustomNotFoundError.new</span><br /><span style="font-style: italic;"> end</span><br /><br /><span style="font-style: italic;"> def rescue_action_in_public(exception)</span><br /><span style="font-style: italic;"> case exception</span><br /><span style="font-style: italic;"> when CustomNotFoundError, ::ActionController::UnknownAction then</span><br /><span style="font-style: italic;"> #render_with_layout "shared/error404", 404, "standard"</span><br /><span style="font-style: italic;"> render :template => "shared/error404", :layout => "standard", :status => "404"</span><br /><span style="font-style: italic;"> else</span><br /><span style="font-style: italic;"> @message = exception</span><br /><span style="font-style: italic;"> render :template => "shared/error", :layout => "standard", :status => "500"</span><br /><span style="font-style: italic;"> end</span><br /><span style="font-style: italic;"> end</span><br /><br /><span style="font-style: italic;"> def local_request?</span><br /><span style="font-style: italic;"> return false</span><br /><span style="font-style: italic;"> end</span><br /></pre><br />Sometimes you may want to throw a 404 yourself. For example if they go to /wiki/doesnotexist you can render your own page. Use something as simple as:<br /><br /><span style="font-style: italic;">render :action => "something", :status => 404</span><br /><br />or as simple as<br /><br /><span style="font-style: italic;">render :file => "#{RAILS_ROOT}/public/404.html", :status => 404 and return</span>Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comtag:blogger.com,1999:blog-5070036599888944678.post-6934991482366566082008-06-02T11:09:00.000-07:002008-06-02T11:14:28.078-07:00Scaffold versus Scaffold ResourceScaffolding has always been the standard way to start a Ruby on Rails piece/component/controller, generating the standard files and paths based on the specified model name. More recently, though, scaffold now creates a RESTful version of these rails conventions. At one time it simply generated regular CRUD, and then scaffold_resource came along to create a REST-based controller, model, and views. Now scaffold does that, by creating the correct views, actions, and allowing Rails to respond to different types of requests (XML, HTML, etc), which make it more restful.Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.comtag:blogger.com,1999:blog-5070036599888944678.post-69120599171858673192008-05-15T22:13:00.000-07:002008-05-15T22:18:07.066-07:00Give it a RESTSo recently I learned what REST really meant. Basically it's CRUD+. This means you have Create, Read, Update, Delete, and then add in Index, Edit, & New. For some reason this is a big deal... I just thought that was a regular ol rails feature. But rest also involves using a resources map to do things like this:<br /><br />map.resources :newsstory<br /><br />Which then gives you access to things like<br />new_newsstory_path<br />edit_newsstory_path(@newsstory)<br /><br /><span style="font-weight: bold;">REST Examples</span><br />You get all this after map.resources :object<br />Path for the form to create a new object -> new_object_path<br />Path to create new object -> object_path (:method => :post)<br />Path to edit object -> edit_object_path(@object)<br />Path to send the edit form (update object) -> object_path(@object) (:method => :put)<br />Path to view object -> object_path(@object)Ryan Kopfhttp://www.blogger.com/profile/16318410460192759175noreply@blogger.com