Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Tuesday, August 16, 2011

Validation errors in Rails

Whoa! I've been chugging along with Manticore the last 5 days or so and making some good progress. However, I've hit another wall (can you believe it?)

Basically here's the problem. I have a model Ac that creates Armor information for a Character. I've created some methods to calculate various data from fields from a Character's Ac model, and to avoid any errors when doing these calculations I need to validate presence_of and numericality_of several fields.

So my problem is two-fold. First off, when creating the Ac, all of the validation errors appear before the user has attempted to enter any data. Furthermore, the user can still create an Ac with no data at all. Clearly my validation isn't working.

Here's the code itself from my Ac model:

class Ac < ActiveRecord::Base
belongs_to :character
validates_presence_of :base_ac, :armor, :shield, :dex, :size, :natural, :misc, :deflection validates_numericality_of :base_ac, :armor, :shield, :dex, :size, :natural, :misc, :deflection

...
end


And the code from my AcsController:

class AcsController < ApplicationController
def new
  @character = Character.find(params[:character_id])
  @ac = @character.create_ac(params[:ac])
end

def edit
  @character = Character.find(params[:character_id])
  @ac = @character.ac
end

def create
  @character = Character.find(params[:character_id])
  @ac = @character.ac(params[:ac])
  redirect_to character_path(@character), :notice => 'Armor Class was successfully created.'
end

def update
  @character = Character.find(params[:character_id])
  @ac = @character.ac
  if @ac.update_attributes(params[:ac])
    redirect_to character_path(@character), :notice => 'Armor Class was successfully updated.'
  else
    render :action => "edit"
    end
  end

end

I'm not sure what could be causing this problem. I've got similar validation for the Character model and it works fine. Any advice? The form_for a new Ac is rendered from a partial in the Character show page, but I'm not sure if that would have an effect on this.

Also, on a slightly tangential note, when rendering a form like this, is that what's meant by a "nested form" ? I've seen this term a few times while searching for a solution and I'm unclear as to what it means.

Here's a link to the project on Github: https://github.com/illbzo1/Manticore

Monday, July 4, 2011

Rails 3.1 Tutorials

Since I got back into working with Agile Web Development with Rails, I've been thinking about what my next step should be. I suffer under the burden of too many options of things I want to learn and/or work on. I want to work on my knowledge of Ruby, and I think that's a good place to start.

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

Since I've been working through Agile Web Development with Rails, I've felt less like blogging about what I'm doing. It's not that I don't find this book valuable, but I'm less excited about talking about my progress than when I'm working on a personal project. Still, I haven't been slacking in my coding.

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.

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 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 7, 2011

Styling flash notices in Rails

So I've made some pretty good progress with Chorenivore the last couple of days. Last night I dug into CSS to clean up the view, mostly making sure that my <:th>'s and my <td>'s were lined up. It's kind of hard not to go nuts obsessing over padding and font size and palette choices. Save that for later, dummy! Work on your functionality first.

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.

Monday, May 30, 2011

Gettin my git on

Oh so you want to know about Memorial Day weekend coding? First off, at my friend Eli's insistence, I've been getting more familiar with keyboard shortcuts in TextMate. Who no longer digs through files to find what he's looking for? This guy. Who keeps everything in a project, then performs a little search magic with CMD+T? THIS GUY!

I also started using Git Hub, after almost three years of having an account. I understand how commits and pushing changes work, but I had some trouble last night. I was able to create a repository and upload the readme file, but then I couldn't get the entire project uploaded.

So here's what I think happened. I'm working with Chorenivore (my to-do list application) and I created a new directory at users/tylermoore/chorenivore, ran git init, tossed a README file in there and pushed it to git. Then when I went to upload the whole project, I was trying to push from users/tylermoore/code/chorenivore. Obviously having duplicate directories is a stupid idea!

So if I want to make a folder into a Git directory, ready for upload, all I have to do is navigate to that directory in terminal and use git init? Or do I need to make a brand new directory every time, then copy all my code into it? It seems like I should just be able to set up the necessary files with an existing directory. Also, what does git touch do? Can you just enter git touch . to touch everything in the directory?

In more concrete code-related news, Chorenivore is coming along pretty well. After creating the initial files with a scaffold (something my friend Matt says is a bad idea, even at early stages, and while I DO agree with him, I'm still finding it useful) I was able to edit the default code to do a few things I wanted to do. For example:

After creating a new Task, the user is taken back to the Task index page. A user is also taken back to the Task index page after deleting a Task and a flash :notice is displayed.

Creating a new Task does not allow a user to mark a Task as complete, but Editing a Task DOES allow this. Since scaffold generates New and Edit actions that use the same form, I simply copied the code and modified it so the boolean column for Finished? wasn't included.

Not bad! I've definitely got an application that works and functionality that makes sense. I think I've been good about thinking about how things should work, but actually making that happen is getting easier.

Oh, here's my git profile.