Showing posts with label ajax. Show all posts
Showing posts with label ajax. Show all posts

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.

Sunday, April 24, 2011

Lightening the overload

The longer I work with Ruby on Rails, the more things become clear to me. The more I understand what various acronyms stand for, what they do and how they work. Here's an example, torn from the pages of REAL LIFE:

I work for an e-commerce website, mostly doing SEO work, photography and editing products for the store. The IT manager recently changed our shopping cart because customers were having problems with the Ajax based shopping cart. Before this weekend, I didn't have a clear understanding of what Ajax was. I assumed it was another coding language like Ruby, but it turns out it's just a tool used in web application design. It stands for Asynchronous JavaScript and XML and Agile Web Development had me use it to create an indicator that the shopping cart has been updated in the application I'm working on.

So what have I learned this weekend? I got familiar with Ajax, what it can do and what it's used for (not to mention what it actually IS) and it's given me a better understanding of how everything, from development to design, works together. I first got interested in web design a few years ago, when I fell in love with CSS, and learning Ruby has been nice because I've had to learn how to implement CSS within a Rails application.

When I got started with Ruby a few months ago, I felt like there was a ton of information to process and somehow wrap my head around. That's still true, but the information overload gets lighter every day.