I have no idea how to write clear, confident front-end code for my rails app

First and foremost:

I am not a very experienced web-developer or developer for that matter.

I’ve only spent about a year with Rails, however there is something I
have

noticed about my projects and would like to get off my chest. Please
chime
in

and share your thoughts.

The Model-part of things is clearly structured and well tested and as a
result

of that I have a lot of confidence in that code.

In my opinion this is one positive effect of Convention over
Configuration,

even as a newcomer it didnt take a lot of time till I learned via IRC

or a Screen/Podcast: Hey! This code belongs in the model.

Also there are a lot of discussions going on in the community

(Jim W., Avdi G., James Edward Grey II and the rest

of the Rogues, Gary Bernhardt and many more) about

how to organize models/objects in Rails, which I find very helpful.

As I move towards the view though, the messier my code becomes.

It starts with the controller and a lot of instance variables as I tend
to

move away from RESTful-Rails.

To be more specific: I merge the edit, show and new actions into one.

Controllers are still tested though and therefore I am confident that
they
too,

behave as expected.

But the intersection between jQuery and CSS tends to be a real problem.

One reason is that DOM elements (classes and ids) get mixed with
jQuery’s

Selectors and I havent found a way to decouple this behavior from the
classes and ids.

Another is I have no real clue where to put my JS, honestly.

Currently its all in the application.js. Here my code also tends to be
untested

and as a result I have no confidence in the UI-Code,

as soon as a class or id is renamed I have to manually test the whole
page.

(I understand that there are a lot of JS frameworks trying to solve
these

problems, but my apps don’t have a ton of JS (800-1000 LOC) and the

introduction of a client-side framework might be overkill. I am also
under
the

impression that these frameworks are still under heavy development and
the

community has not decided which one to use yet.)

Now, these problems starting with the controller and ending with the
view

are a result of a motion towards one-page applications and away from the

(in my understanding) Restful-Rails-apps. But it is obviously the way
that

a lot of us want to experience and build the web.

I guess it boils down to this:

I have no idea how to write clear, confident front-end code for my rails
app.

Could you help me?

pw wrote in post #1068883:

I have no idea how to write clear, confident front-end code for my rails
app.

Could you help me?

I’m really not sure what you’re expecting here. You’re asking no
specific question that I can discern.

For example at one point you state the following:

It starts with the controller and a lot of instance variables as I tend to move
away from RESTful-Rails.

Given Rails is designed around REST it’s no wonder you’re having trouble
with the framework as you “move away” from its central design.

You also mentioned having difficulties testing controller and view logic
(as well as JavaScript). I think you’re probably right that a JS
framework may be overkill. But, that has little to do with your
difficulty in testing you’re code. There are tools and techniques
available for testing all aspects of the MVC stack. Really any of the
popular testing frameworks can handle most of your needs, whether it be
Test::Unit, RSpec or Cucumber. For unit testing JavaScript there is a
nice tool called Jasmine for that.

http://pivotal.github.com/jasmine/

In any case I can’t really see you getting any of the answers you hope
to receive from a forum such as this one. It’s far more likely that any
“advice” you get by asking such open-ended and broad-scoped questions
will serve to confuse you even further.

My advice would be to find a mentor. Preferably someone local where you
can sit down with him/her and review the solutions they’ve come up with.
It might also be helpful to review some open source code that does
similar things to what you want to achieve.

Thank you for your reply, I do realize the open-ended nature of the
question,
thought I should give it a shot anyway.

I love the suggestion of a mentor, unfortunately there are no Rails or
even
Ruby groups
in my area.

I couldn’t help but notice your message on the list.

You don’t have to have a mentor necessarily in your area. I have been
teaching and mentoring Ruby since 2008, and doing it all from remote.

Now that Google Plus has hangouts that let you share a terminal easily,
and
voice and video and full desktop sharing if it is needed, along with
DropBox, you can pair program and share code and do all of those things
that you can do at a desk.

There are other options besides those of course, one of which is screen
on
Linux or Mac, and potentially on Windows as well.

So please don’t let that stop you.

By the way, I am a mentor at RubyLearning.org you may have seen our blog
at
http://rubylearning.com/blog and our study notes at
http://rubylearning.com

Unfortunately, I don’t know Rails. But Ruby we can help you with.
Warmest Regards,

Victor H. Goff III
維克多
My GPlus Profile http://bit.ly/rqK71A
Voice (307) 215-9107

On 14 July 2012 11:47, pw [email protected] wrote:


To be more specific: I merge the edit, show and new actions into one.

If you find yourself repeating code in these in order to setup data
that they all need then you can use before_filter to run that code.

Colin

"

To be more specific: I merge the edit, show and new actions into one.

"
Well just refrain from the urge to compact controllers code to the point
of
having unreadable, and most importantly, intertwined code (even for
yourself, when you come back to that piece of code in a year). STICK to
REST. Use redirects or explicit template rendering from a REST action,
but
NEW is NEW, and CREATE is CREATE. Abstract the same code into
private/protected methods. Eventually you’ll come to an implementation
that
both looks awesome and is well structured, and easy to change (for
instance
let’s say you want to put 1 extra computation or whatnot into your NEW
action, but only that (i.e. not CREATE). Then you got to rethink many
actions and test them, branch logic and end with ugly code just 1 thinko
from breaking the app. This is totally unnecessary work you are doing,
and
I guess you’ve realized that because you are asking these questions.

I also suggest you diving into RSpec and BDD right off. Don’t just ‘try’
it
for a few hours, cause then you will probably just drop it. It takes
practice to make it extremely useful; at first things go slow, but it
pays
out in the long run when you have 1000 specs describing behavior and you
don’t even have to test stuff while developing, and test for Complete a
(simple) project with BDD style. Or just at least write a spec for every
bug you encounter while developing the project. You’ll soon enough
witness
the benefits for yourself.

P.S. Some specs, especially view (template) specs seems like an
overkill,
but it is key for a reliable development IMO. Probably no need for a
spec
if you are presented with HTML mock, but I am talking about situations
when
you just have a mental image of what user would see. Then you describe
it
in specs. And while doing so you’ll often identify weak points of your
UI.

2012/7/18 Victor G. [email protected]

There are other options besides those of course, one of which is screen on

Thank you for your reply, I do realize the open-ended nature of the
To post to this group, send email to [email protected].
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Pagarbiai,
Gintautas