Rails Style Params in Python and Flask
Ruby on Rails does a great job of parsing arrays in form data and populating a params hash, for quick and easy access in your controllers.
For instance
Gives you
params[:item] # => ["item 1", "item 2"]
Flask doesn't do this work f...
Written by Sean Behan on 11/04/2018
How to Reset and Clear a Form Field or Textarea with Stateless Components in React
Stateless components are nice. They are simple to use and understand but can become tricky when you start thinking about data and how to manage state. In general, the consensus seems to be that if your UI is going to need state, then at least the componen...
Written by Sean Behan on 10/21/2018
Manual ManyToMany Through with Django's ORM
Here is a code snippet that demonstrates how to set up a __ManyToMany through__ relationship in Django. In Rails, the equivalent would be called a __has_many through__ association.
If you set the __through__ argument on the ManyToManyField, Django wil...
Written by Sean Behan on 07/21/2017
How to Fix Raw query must include the primary key with Django ORM
When running raw SQL queries in Django you must include a primary key otherwise an invalid query exception is raised. Normally this is fine but when running more complex queries a primary key may not be available or even make sense.
There is a simple ...
Written by Sean Behan on 07/15/2017
Git Untrack Already Tracked Files
To remove files that are currently being tracked by git, you have to remove them from the "cache". Note, doing this will NOT delete the file on your local machine. It will still be there but not be tracked.
git rm -r --cached supersecretpasswords.txt
Yo...
Written by Sean Behan on 06/17/2012
Using Formtastic to Cleanly Create Nice Looking Forms in Rails
Forms can easily get cluttered when you're dealing with a lot of form fields... er, ERB tags. I've written about extending Rails form builders, which certainly goes along way to shrinking your views where forms are used. The plugin Formtastic is even bett...
Written by Sean Behan on 06/17/2012
How To Flush Your Permalink Structure in Wordpress When Using Taxonomies ...or Wordpress Taxonomies Not Working Instead I See A 404 Page
I didn't know this, but because of a linking problem using custom taxonomies in Wordpress, I was forced to find out. If you create a new taxonomy, it will not work immediately on the front facing end. Your users will be greeted by a 404, page not found in...
Written by Sean Behan on 06/17/2012
Launch Photoshop (Or Any App) From The Command Line on Mac OS X
I often find myself coding with the terminal open. Cding around a web app project I usually end up at some point launching Photoshop. Either to touch up or work on a psd, png, jpg ...etc. I fire up Photoshop and then navigate to the app's public directory...
Written by Sean Behan on 06/17/2012
Onchange Event Fired from Select Field in Rails Form
In the view there is a regular Rails form and a javascript function that will be triggered when the country select field is changed. The javascript function will make an ajax request to the country_select url with the country code passed as the id variabl...
Written by Sean Behan on 06/17/2012
Extending Rails Form Builders
Extending forms in Rails is simple and will greatly reduce the amount of code in your views. This example is taken right from the Agile Web Development book on Rails(2.1.*) with one minor tweak. I want to pass a label argument along with the field name so...
Written by Sean Behan on 06/17/2012
Override to_param method in model to get pseudo permalinks without any work
There are a number of permalink plugins for Rails, http://www.seoonrails.com/even-better-looking-urls-with-permalink_fu, is a good one that I've used before. However, this involves informing the model class (has_permalink :title), adding a route, using t...
Written by Sean Behan on 06/17/2012
Trouble Using Attr_Accessor in Rails Models and Forms
You might use the attr_accessible method to create getters and setters for a class that has attributes which don't map directly to corresponding fields in a database. For example let's take the scenario where you are processing a credit card transaction. ...
Written by Sean Behan on 06/17/2012
Nested Has_one Relationship with Fields_for and Attr_accessible in Model Class
To make child attributes accessible to your model through a nested forms (Rails 2.3) you'll need to add the "#{child_class}_attributes" to the attr_accessible method in your parent class. If you don't use attr_accessible in your parent model (you would do...
Written by Sean Behan on 06/17/2012
Nested Attributes in a Form for Has_One Model Association in Rails
Just for reference...
class Member < ActiveRecord::Base
has_one :member_profile
accepts_nested_attributes_for :member_profile
end
...
Written by Sean Behan on 06/17/2012
Custom Date Formats for Your Rails Application
If you use a consistent date format often in your Rails applciation, it is worth it to add the format to your application environment. You can do this by adding it to the bottom of the config/environment.rb file.
Time::DATE_FORMATS[:my_custom_format] ...
Written by Sean Behan on 06/17/2012
Using Prototype to Access Form Data
Prototype has a powerful API for accessing and manipulating the Document Object Model, A.K.A the DOM. The following code will let you interact with a simple web form.
Suppose we have a form that contains hidden/or locked inputs and they need to be update...
Written by Sean Behan on 06/17/2012