Saturday, June 18, 2011

Applying Tutorial Methods to My Own Rails Projects

So even though I picked Agile Web Development with Rails back up the other day (after about 2 months away from it) I decided to start the entire book over again. I don't typically like doing this, but I felt my reasoning was sound. For one thing, going back through building the shopping cart application would make more sense to me now, since I'm thinking about how Rails applications work and not just making sure I'm following the tutorial correctly so I don't get errors. For another, I was pretty sure I could find some techniques I could apply to my personal projects. Lastly, the first time around I skipped all the optional coding at the end of every section and this time I'm going to do it. I'm more interested in learning new ideas than in making this one specific application work correctly.

On this last part I was correct. I just went through the beginning part, which walks me through generating a scaffold for the project, then some styling to spruce the code up a bit and I already found a couple of ideas I want to apply to Chorenivore.

This is something I need to be doing every time I'm doing a tutorial or looking at code. What can I do with this? How does this apply to my projects?

Something funny I noticed this morning: back when I was learning CSS, I had a hard time understanding what a class was. And now with Rails, it's something I don't even think about. I don't know if I've just seen it often enough that I get it now, or if Rails has so many other things that are more complex that it just doesn't register. Probably a mixture of both, I'm guessing!

I felt a bit lazier this week, because I feel like Chorenivore is about as done as it's going to be for now and I didn't feel like digging back into Agile Web Development with Rails. What do you guys do to stay motivated when you're between projects like this? Does that even happen to more experienced Rails developers?

Oh and one last thing. Eli emailed me asking what happened to my old blog. I didn't realize switching from to would DESTROY all my followers, so I put another blog up on, redirecting people to this blog. That should take care of that problem!

Tuesday, June 14, 2011

Using Rails console to add a user to a database

It became apparent to me last week that pretty soon I'd need to go back and finish working on Depot, the shopping cart application I was building by following along with Agile Web Development with Rails. I got off track a while back because I'd forgotten to create a user, then continued on with the guide until I got to a point that I needed to log in. Uh oh!

Unfortunately, what I didn't realize is that the very next page after I'd left off walked me through using Rails console to add a user. Still, it's not like I've been lax in the two months since I looked at this program, right?

So now I'm back on track. Right now I'm working on translation to different languages based on location. Interesting, though I don't know how practical this will be. My current plan is to finish up this book, mostly because I'm kind of sick of hopping around between projects. Then again, when is a project ever really done? Hopefully inspirato will strike between now and then and I'll either think of something new to do with Chorenivore, get back to work on Manticore, or create another app! I actually might go back and do more work with Ruby itself. I've been on that Rails tip for a while now.

Remember when I said I think more about what the code is doing when I follow a guide now? Completely true. While backtracking through Agile Web Development with Rails, trying to figure out where I'd left off, I found myself thinking about the code as I worked. Digging around in the deep, smelly guts of a Rails application isn't as intimidating as it once was. Sometimes I feel like I'm progressing really slowly, while other times I'm amazed at how far I've come in the nearly 5 months I've been working with Ruby on Rails.

Bruce Lee said "A man must constantly exceed his level." Does anyone else read that and immediately apply it to Rails?

Constantly leveling up.

Sunday, June 12, 2011

Fixing the delete method in Jquery

Here's a little problem I ran into the other day. I've switched to Jquery from Prototype, and while I was initially able to get Javascript working with Jquery, my :delete method stopped working. Rather than destroying the thing it was called to destroy, it redirected me to the show page.

After a few google searches, I found this is a fairly common problem. However, none of the solutions I found fixed my problem entirely, but a combination of suggestions DID fix the problem.

I'd installed the jquery gem, as suggested here but the problem was in the javascript_includ_tag in the header of my application.html.erb file.

At first I had <%= javascript_include_tag :defaults %>, which I assumed would work since Jquery was the new default Javascript library after installing the gem, but I had to edit it like so:

<%= javascript_include_tag :defaults, 'jquery_ujs' %>

Bingo! Once again, my :delete method works, and my flash[:notice] script is working, too.

Now for a little question.

You may recall my previous question about passing multiple values to a hidden_field. David showed me this code:

  def status
    finished? ? 'finished' : 'unfinished'

This goes in the Task model and defines these two statuses. However, I don't know how to call them and mark a Task as finished or unfinished then have that change updated in the view.

Here's the code I've got now:

<%= form_for(task) do |f| %>
  <%= f.hidden_field :finished, :value => !task.finished %>
  <div class="actions">
    <% if task.finished %>
      <%= image_submit_tag 'finished.png' %>
    <% else %>
      <%= image_submit_tag 'unfinished.png' %>
  <% end %>

Basically, I want it to work this way: A new Task is defined as unfinished by default, a user can mark a task as finished by clicking on 'unfinished.png', or mark it as unfinished again by clicking on 'finished.png'. This change should mark the Task with that status, either 'finished' or 'unfinished.'

Will I need to update the table for Task and add this as a field? Or can this information be stored in the database just by using the hidden_field? I want to be able to manipulate Tasks based on whether they're marked as finished or unfinished later.

I didn't do much coding this weekend, but it definitely felt good to sit down and fix my Jquery problem after thinking about it most of the day today.