Shawna C. Scott

Learning to hack, one day at a time

Menu Close

Tag: rails

`link_to` as a block with HTML options

Have you ever wanted to add an icon of some sort to a link, but couldn’t figure out how to add that <span><i class='glyphicon glyphicon-star'></i></span> in there?

The solution is to use the link_to helper as a block!

The formal syntax, as seen in the link_to documentation, is:

link_to(options = {}, html_options = {}) do
  # name
end

And translated into a more concrete example in an ERB template, it looks like:

<%= link_to your_path, class: 'your-class' do %>
  <span><i class='glyphicon glyphicon-star'></i></span>
<% end %>

Thanks to this Stack Overflow answer for inspiring this post!

On Tumblr

Save yourself pain; make strong params yell at you

I’d like to tell you a (cheesy, overly dramatic) story:

One dark and stormy night, you’re given a ticket that requires you to rename some data columns and rearrange a few form inputs. In your arrogance, you confidently determine this will take you no time at all. Thunder flashes. This is totally not foreshadowing.

You successfully migrate the database. You successfully rearrange the form inputs. All your tests pass. You are feeling pretty self sufficient, like a god/dess among junior developers. You decide, since you are a conscientious team member, to manually fill in the form and watch the magic happen.

Only nothing happens. The form cheerfully proclaims it’s success, but your data has clearly not been saved. You vainly try to find the problem for an hour, but eventually, BAMFness severely deflated, have to ask for help. This is when your boss/mentor discovers within minutes that you’ve forgotten to update the permitted parameters in your controller.

As you may have guessed, it’s my own hubris I’m describing here. But! I have learned something that can help you avoid this terrible tale of woe.

Add this line to your config/environments/test.rb and config/environments/development.rb:

# Raise an error when sending unpermitted parameters
config.action_controller.action_on_unpermitted_parameters = :raise

This lovely little incantation (which you can read more about on the strong parameters repo) will throw an exception when you send unpermitted keys, rather than silently swallowing them. So, instead of this:

You’ll get this:

Never thought you’d be happy to see that page, huh?

Best of luck, and always check your hubris, lest it bring you to ruin. ;D

On Tumblr

Properly Factored MVC in Rails Applications

EDIT: After feedback from Katrina Owen Herself, I wanted to include the “much improved” version of the tutorial, as well as Katrina’s amazing five-part series on Golden Master testing


 

Recently, I wanted to learn more about refactoring, and about general best practices for structuring Rails code. Generally, when I’m working on a feature, I don’t spend the majority of my time figuring out how to make something work. I spend most of my time trying to find the best way to make it work.

As a new programmer, though, I have to rely on external resources, since I don’t yet have that internal lexicon of good and bad design patterns to point me in the right direction. So, this past weekend, I decided that I wanted to learn more about Rails best practices, and about refactoring. Imagine my delight when I found this great workshop and accompanying tutorial, “Properly Factored MVC”  [tutorial] (see edit) from RailsConf 2013, presented by Katrina Owen and Jeff Casimir.

I finished the video, although I haven’t worked all the way through all the tutorial iterations yet, and I’ve already learned a lot about the theory of refactoring, and how to insure your refactoring doesn’t cause regressions.

If the 1:43 runtime of the video is daunting, you can just work through things from the tutorial alone. Personally, though, I found the video helpful, since Katrina Owens is talking through her thought process as she goes in more detail.

Hope you find this resource helpful!

PS: If you feel inspired to help refactor some Real Live Code &trade; after this, feel free to hop on over to the very beginner-friendly open source project, Calagator. I’m a contributor there, and there’s plenty of places to test and expand your skills!

On Tumblr: http://junior-dev-lessons.tumblr.com/post/99540321305/properly-factored-mvc-in-rails-applications

New Job, and Junior Dev Lessons

I haven’t announced it here until now, but recently I was hired at a startup called:

38_Zeros_Logo

 

We make a data logger that connects to our cloud aggregation service. We’re doing everything from making custom boards to embedded programming to data cleaning, all with a web-based Rails GUI on top. I’m three weeks in, and absolutely loving both the work and my coworkers!

Currently, I’m working on the Rails app, so I’m using Ruby daily, which is awesome. And because our software team is small and the app is young, I’m living the dream of helping to establish code conventions and working mostly on greenfield problems. And when I’m not feeling like working on Rails for a bit, there’s plenty of room for me to learn more anything about embedded programming and hardware.

Here’s where I work:

Why yes, that is an arctic exploration pod. Why do you ask?

I’ve blogged about code here before, and will continue to on occasion, but I often put off blogging because I feel like I don’t have something ‘big enough’ to say. Simultaneously, I’ve been feeling the need to catalog some of the things I’ve learned, and some of the silly things I’ve done, in the hopes that my pain can help someone else avoid headaches in the future.

So, I’ve decided to launch a Tumblr specifically for small snippets and quick tips that junior devs may find helpful. Tumblr just feels like a more natural place for something intended to be short form. Junior Dev Lessons posts will crosspost here, on their own page. If you want to follow those tips on Tumblr, you can go to junior-dev-lessons.tumblr.com.

If you want to share junior dev lessons you’ve learned the hard way, contact me and I’ll post them, too! Maybe we’ll save some other junior devs some headaches along the way.