On deciphering views

1 view
Skip to first unread message

Tony Olivo

unread,
Jul 5, 2010, 8:20:52 PM7/5/10
to freehu...@googlegroups.com
Hey all,

I have been over the HAML tutorial (http://haml-lang.com/tutorial.html) in an effort to understand the views. For the most part I understand what's going on. I get most of the markup, but there are these link_to ruby statements that I'm having trouble with.

For instance...
=link_to 'Remove', visit_path(:person_id => visit.person, :id => visit, :destination => day_visits_path), :method => :delete
...from app/views/visits/day.html.haml

but a lot of stuff seems to go on between there and rendering. Basically, visit_path looks like a function call to me, but I can't find it anywhere else so I'm guessing not, unless it's another one of those things that's inferred by rails by it "knowing" about visits and paths.

As it turns out, just writing the question gave me an idea on what to look up, so I'll share what I've gleaned, to either be corrected or serve future developers. link_to is a rails method to create an anchor tag. The line above creates an anchor with "Remove" as the text. The href is given by the visit_path method (function?). That seems to me to be a built in thing with awareness of the models. By passing the :person_id and :id to it, it constructs a URL in the form of "person/<person_id>/visit/<id>". The :destination parameter passes the source URL of the anchor through GET so that the page knows where to put you back when it's done, so would add something like "?<organization>/visits/2010/07/02" to the URL above.

What I still don't know is how to handle those URLs. I can understand how to deal with the one layer URLs, like "<organization>/visits"; those pages will be under "app/views/visits" in the source. Multi layer URLs like "people/<id>/visits/<id>" I don't understand yet, because there is not an explicit view for them. I'm guessing to move any further, I need to stop treating "app/controllers" like a black box and get in there.

Does anyone have any helpful reading to get me on track?
Thanks,
-Tony (Thav)

Tony Olivo

unread,
Jul 6, 2010, 12:29:45 PM7/6/10
to freehu...@googlegroups.com
On further investigation, it seems like routes and controllers may be what I need to learn about.

Some reading on routes here http://guides.rubyonrails.org/routing.html

-Tony

Alon Salant

unread,
Jul 6, 2010, 2:59:58 PM7/6/10
to freehu...@googlegroups.com
Yep. You're on the right track. Freehub makes extensive use of Rails'
support for the REST model of web programming - "RESTful routes". The
magic methods you refer like visit_path are called url helpers and are
generated for you when the routes are parsed by Rails. If you run
'rake routes' you will see a list of the actions that your app will
respond to and the corresponding helpers.

> --
> You received this message because you are subscribed to the Google Groups
> "Freehub Users" group.
> To post to this group, send email to freehu...@googlegroups.com.
> To unsubscribe from this group, send email to
> freehub-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/freehub-users?hl=en.
>

Tony Olivo

unread,
Jul 6, 2010, 11:02:19 PM7/6/10
to freehu...@googlegroups.com
And a bit of success, I have at least added the sign in button I was after, and it does get put into the database! There are some things I would need to do to clean it up, and what I have in mind screams for AJAX, but for the moment I am happy.

I added some member routes to visits, added a controller action, and used some RESTful path helpers. I guess I'm starting to get it.

-Tony

J.P. Lien

unread,
Jul 7, 2010, 9:03:25 AM7/7/10
to freehu...@googlegroups.com
For the AJAX interface check out "link_to_remote" and
"button_to_remote". The guts are in the PrototypeHelper, which
generates the Javascript for you.

J.P.

> freehu...@googlegroups.com <mailto:freehu...@googlegroups.com>.


> > To unsubscribe from this group, send email to
> > freehub-user...@googlegroups.com

> <mailto:freehub-users%2Bunsu...@googlegroups.com>.


> > For more options, visit this group at
> > http://groups.google.com/group/freehub-users?hl=en.
> >
>
> --
> You received this message because you are subscribed to the Google
> Groups "Freehub Users" group.
> To post to this group, send email to freehu...@googlegroups.com

> <mailto:freehu...@googlegroups.com>.


> To unsubscribe from this group, send email to
> freehub-user...@googlegroups.com

> <mailto:freehub-users%2Bunsu...@googlegroups.com>.

Reply all
Reply to author
Forward
0 new messages