My name is Tyler. I'm learning Ruby on Rails. This is a blog for me to talk about what I've learned. I'm no longer completely new to Ruby and while I don't feel comfortable calling myself a coder or programmer yet, I am absolutely a Ruby Enthusiast and a Rails Fancier. I've been working with Ruby on Rails since February 1, 2011. You know that song about "lovin' every minute of it?" Bingo.
Monday, August 22, 2011
Css, page design and Rails applications
For example, I created a basic navigation partial, wrapped it in a div and styled it to appear at the top of all the pages in my Manticore app. I'm not sure how this will change in the final version, or if I'll keep it at all, but it certainly helps out right now, when I want to quickly switch back and forth to test navigation or to see how a change in the code works. Or if it works at all!
How do you work with styling when developing a new application? Do you leave it until the end, or do you do some basic styling and then polish it up later? Do you have someone else do it? I know it doesn't make sense to style something that may not be included in the final version of an application, but I'm not sure how other developers work when it comes to basic styling.
Agile Web Development with Rails did styling early on in the development, with some minor tweaks here and there later. It was coming from the perspective of working for a client who wanted an e-commerce site created, so that may have had something to do with getting page styles hammered out early.
Sunday, July 24, 2011
More Heroku Rails Deployment Issues
Thursday, July 21, 2011
Setting up a virtual Apache server
So here's where I'm at now. I'm near the end of the practical section of Agile Web Development. The chapter I'm on now is about deployment and I'm having trouble setting up a virtual Apache server and actually getting Depot to deploy. Everything appears to be installed correctly, but I can't get everything tied together correctly.
How do I find my host name? I'll need to define it as depot.hostname.com in my Apache configuration file. I thought it would just be depot.localhost.com, or perhaps depot.127.0.0.1.com, but neither of those worked.
I also had trouble editing my hosts file. I'm able to open it through the terminal, but it won't allow me to make changes.
Still, this headache is a minor set back. I know it's something I need to learn, but for now I think learning more actual coding will be more useful to me. Any good recommendations for new books? I really liked Agile Web Development, so I might look into something else by the Pragmatic Programmers. Not sure if Advanced Rails Recipes would be beyond me or not.
Monday, July 11, 2011
Editing Functional Tests in Rails
Case in point:
I've been working through the Depot application with Agile Web Development with Rails. This latest chapter had me working through creating a mailer and writing tests for it. Creating the mailer went well, and I can use my own gmail account to send mail through my Depot application! SUCCESS!
However, I got hung up on the functional tests. And I know what the problem is, but not how to fix it.
Here are the errors I get:
1) Error:
test_should_destroy_line_item(LineItemsControllerTest):
ActiveRecord::RecordNotFound: Couldn't find LineItem with ID=980190962 [WHERE ("line_items".cart_id = 980190963)]
app/controllers/line_items_controller.rb:79:in `destroy'
test/functional/line_items_controller_test.rb:45:in `test_should_destroy_line_item'
test/functional/line_items_controller_test.rb:44:in `test_should_destroy_line_item'
2) Error:
test_order_shipped(NotifierTest):
ActionView::Template::Error: undefined method `protect_against_forgery?' for #<#
app/views/line_items/_line_item.html.erb:9:in `_app_views_line_items__line_item_html_erb___1505233713_2172764620_6072364'
app/views/notifier/order_shipped.html.erb:8:in `_app_views_notifier_order_shipped_html_erb___2127495373_2172826120_0'
app/mailers/notifier.rb:13:in `order_shipped'
test/functional/notifier_test.rb:13:in `test_order_shipped'
I have an idea why I'm getting the first error. I've been following the optional exercises at the end of each chapter and I changed the :destroy method in line_items_controller without updating the functional test. Here's the OFFENSIVE code:
def destroy
@cart = current_cart
@line_item = @cart.remove_product(@cart.line_items.find(params[:id]))
respond_to do |format|
format.html { redirect_to store_url }
format.js
format.xml { head :ok }
end
end
end
Any idea how to fix this? I'm not super concerned about it, but I'd like to know if I'm on the right track and I think this would be an easy fix. The second error I'm less sure about. I don't get why I'm getting an error for protect_against_forgery? since I'm not calling that method anywhere. Is it a default Rails thing that I'm overlooking?
Anyway, my current strategy has been simply to comment out the tests until later. Which may never come, depending on a variety of factors! I have a sneaking suspicion writing tests will never be one of my strong suits.
Getting back to my original point, it made more sense simply to comment these tests out rather than spend a bunch of time fixing them, especially since my application DOES work. Still, it's a good idea to get a handle on writing tests. I'm doing this for Future Tyler.
Tuesday, July 5, 2011
Ajax and CSS with Rails
However, the :blind_down visual effect I was using had some issues. Take a look:
Yuck! What are those disgusting bars doing there? Messing up my beautiful flashing green Ajax notification, that's what! There has to be a way to get rid of those. And there is!
Here's the solution. I know the Ajax notification is going off in the shopping cart table. And I know how it's styled in my depot.css file. These are pretty good clues for figuring out how to get rid of those bars. A quick Google search later and I find a little command called border-collapse.
So I just throw that into the css for the cart table:
#cart table {
border-top: 1px dotted #595;
...
border-collapse: collapse;
}
And here's the result:
Easy enough!
I find myself drawn into doing some styling whenever Agile Web Development is working on it. Styling should really be left until functionality is hammered out, but I like this approach. I like messing around with padding, margins and color schemes. On the other hand, this ties into something I had wrong about coding for a long, long time. I always kind of assumed programmers had a lot more memorized than they do. Now it's apparent that coders memorize methods they use a lot, but still have to look up quite a bit of information. The important thing is understanding how an application works, having a plan to accomplish what you want done, and knowing how to find the specific information when you need it. So in that case, yes, this DOES pertain specifically to Rails and not just CSS!
I also ran across this page while I was searching for Jquery examples. Seems like a good resource.
Monday, July 4, 2011
Rails 3.1 Tutorials
But I just found some Rails 3.1 tutorials on Git the other day, and that sounds like a good choice, too.
Another Rails friend of mine (@SpencerCooley) was talking about how he feels more productive when he's less afraid of breaking his code. I've felt this way for a while now. Remember when I used to just restart an application as soon as I broke the code, because it was less of a hassle to repeat what I knew how to do than to figure out how to fix the things I didn't? How are you supposed to grow and develop working that way? You won't, dummy!
So now that I'm more experienced, I feel way more confident in my ability to not only figure out what is giving me an error, but I feel better equipped to go about trying to solve it. I've got a better grasp on how to talk about what I'm doing. You may have noticed I haven't been updating this blog as often lately. My higher level of confidence in my ability to solve my own problem is partly to blame for this. Still, I need to get back in the habit of updating my blog whenever I code, and I need to get back in the habit of coding every day. No more lazy Fridays, you hear me?
Also, I think I'm just going to use Twitter handles when referring to other Rails enthusiasts from now on.
Monday, June 27, 2011
Session Counter in Rails
I've had a lot of luck translating things I'm learning into methods that work with my other projects (mostly just Chorenivore) and this time through Agile Web Development, I'm doing all the extra stuff. Tonight I learned a method for displaying how many times a user has viewed the index page, as well as a way to reset the session counter and how to change how often it's displayed.
I'm guessing this is the same sort of thing that's used to track how often things are viewed online, right? With some editing, this method could be used on a photo blog to show how many views a particular photo has had, etc. You see how things are coming together?
I've also been using github extensively, mostly just to record what I've done and as a means of source control. Much like firing up the terminal and booting up webrick, I no longer have to think about how to commit changes and push to git. Memorization isn't learning, but it can definitely make learning easier.
Wednesday, June 22, 2011
Rails Validation Syntax
No big deal! I fired up Rails API and found what I was looking for.
validates_length_of :title, :minimum => 10, :message => 'must be at least 10 characters.'
Ahh, perfect! Plus, editing the default message is the second optional exercise. Easy enough. But wait, what's this?
The book had me write validation like so:
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}, :message => 'must be a number.'
This is a slightly different syntax than the validation I found on the Rails API and I don't like it as much. First, it's not as immediately obvious what this validation is doing. Second, it just feels clunkier. So since I'd already written a validation method in a different syntax, I figured this would do the same thing and be in a more readable format:
validates_numericality_of :price, :greater_than_or_equal_to => 0.01, :message => 'must be a number.'
And what do you know? It works! Is there anything I should be aware of when writing validation like this? Any ideas why Agile Web Development with Rails wrote it the other way? Personal preference of the programmer, perhaps? I'm assuming this is a standard way of writing validation, both because it's the same syntax I found on the Rails API, and because it's more readable. (It's also 5 characters shorter, but who's counting? A piece of Javascript I found online, that's who!)
Ahh, feels good to be making progress. I got hung up for a bit writing tests in the last chapter, until I realized I'd added a stray period and it was holding everything up. Anyone else try running test:unites by mistake? I've probably done it 3 or 5 times in the last 18 hours.
Sunday, June 19, 2011
Creating a new application with an older version of Rails
First off, I noticed the CSS wasn't working exactly as it did the first time I went through Agile Web Development with Rails. Mostly some minor issues that I was able to work with, and this wasn't that big a deal, since styling is best left until later. But the book deals with some styling up front, so this issue threw a red flag.
Next up, I noticed my :destroy method wasn't working. I ran into this issue a couple weeks ago with Chorenivore, and it was a matter of configuring Jquery. So I tried the fix that worked for me before, but I'm not sure why it wasn't working with a Rails 3.1 app. Is there more configuration needed? I generated the code with a scaffold command, so I'm not sure why it wasn't working by default.
At any rate, whether this is a bug (doubtful, even though this is a beta version of Rails 3.1) or I just didn't configure everything correctly, I decided the best solution for now is to create a new application using the same version used in Agile Web Development with Rails.
So the command for creating a new application with a previous version of Rails is easy enough:
rails _3.0.5_ new depot
In this case, I want to use Rails 3.0.5 and my project is named depot, so that's all there is to it! I think the gemfile may need to be edited as well, but so far I haven't run into any issues.
Rails is a framework that is updated and changed regularly, so I know in the long term I'll want to stay current with new versions. But for now, it's more important for me to keep learning and reinforcing what I already know, so using an older version of Rails is the way to go.
Saturday, June 18, 2011
Applying Tutorial Methods to My Own Rails Projects
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 rubymeltdown.blogspot.com to codeitlikeyoustoleit.blogspot.com would DESTROY all my followers, so I put another blog up on rubymeltdown.blogspot.com, 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
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.
Tuesday, June 7, 2011
Styling flash notices in Rails
So sure, I have some questions.
Here's some code from my Tasks controller in Chorenivore:
respond_to do |format|
if @task.update_attributes(params[:task])
format.html { redirect_to(tasks_path, :notice => 'Task was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end
The problem is whenever a Task is updated (or created, or destroyed, etc) the :notice hangs around until the page is reloaded. I'd like to be able to make it disappear after a couple of seconds and style it using CSS. Back when I was working through Agile Web Development with Rails, I created a way to make this notice fade away after a few seconds using Ajax. The problem is, I haven't been able to duplicate it in Chorenivore.
A few questions about this:
First, is this :notice the same between methods? Is the :notice for creating a Task the same :notice for editing a Task? I'm sure there's a way to make this uniform.
Second, how do I do this? Is Ajax the best way? Should I use Javascript? I don't think reloading the entire page is the best way to handle this, and to my understanding Ajax doesn't reload the entire page.
I've seen a lot of references to flash[notice], is this the same thing as :notice, from the code I posted? Also, what does "flash" refer to in this instance? Does it mean Rails is FLASHING a notice, or that it's a notice Rails stores in the flash because it doesn't need to save it after it displays it? Could it be both? Or something else entirely?
Any recommended reading for understanding and working with Ajax/Javascript in Rails? I've read through the API and it's good for reference, but tutorials are my main jam. I'll look around for some on my own as well, but I figured some of you guys would know of some special favorites.
It's also becoming increasingly clear the time is right for me to go back to Agile Web Development with Rails, since I'm definitely thinking about things tutorials are having me do in a different way. Now it's more "What can I do with this?" and less "Am I getting the syntax exactly right so Rails won't complain and barf up a string of errors?"
One last thing: I've been really into Github, and while I feel I'm not using it as efficiently as I could, I really like being able to document and store my code as it progresses. I've been uploading new versions of Chorenivore every time I solve a problem or make an update. I don't know if that's how most Rails programmers use it, but it definitely adds to my feeling of accomplishment once I run that git push command.


