A question of model actions and routes interaction

6 views
Skip to first unread message

LadyBug

unread,
Oct 27, 2009, 7:59:13 AM10/27/09
to Hobo Users
This may be more of a general Rails routing question but I have come
across an interesting problem.

In my test application I have a model, Project, and the associated
ProjectController. The controller has the following declarations and
they work just fine:

hobo_model_controller
auto_actions_for :owner, [:index, :new, :create]
auto_actions :destroy, :edit, :show

Now, I defined a totally new action in the controller, which basically
generates some default stuff for my project:

def new_default_model
...
end

And I put a link/button/whatever in my project table view to call that
new action, like this:

<controls:>
<a class="new-link" with="&this"
action="new_default_model" >New default model</a><br/>
<a class="edit-link" with="&this" action="edit" >Edit</
a><br/>
<delete-button label="Remove project" update="self" />
</controls:>

Now, what happens is that both the edit link and the delete button get
presented nicely, but the new_default_model action link is not. Just
text, no link. Ok, a routing issue then. I added:

map.project_new_default_model 'projects/:id/new_default_model',
:controller => 'projects',
:action => 'new_default_model',
:conditions => { :method => :get }

to my routes.rb. But this did not work. Just text is displayed, no
link (i.e. no <a> tag at all).

However, if I remove the above route and use Hobo to generate the
routing for me using in the ProjectController:

show_action :new_default_model

The link gets filled out as it should be.

I get exactly the same output line with "rake routes" using both of
these methods:

project_new_default_model GET /projects/:id/
new_default_model
{:action=>"new_default_model", :controller=>"projects"}

Hobo of course generates the .format route as well, but it should not
be the issue here?

The location of the above route is also different. Hobo places the
route in the middle of list among other Hobo routes. The manual
mapping places the line either on top or bottom of route list,
depending on the placement of the mapping line. Neither manual
placement (before or after Hobo mappings) works.

I have experimented with member routing as well, but the above
mentioned manual definition was the one producing exact match with the
Hobo route.

So, why does the Hobo show_action generate working routing and the
manual mapping doesn't?

Matt Jones

unread,
Oct 27, 2009, 8:14:12 AM10/27/09
to hobo...@googlegroups.com

The <a action=""> form is checking to see if the action specified
exists and is linkable. There's no good way to automatically read that
out of the manual routes, which is why that doesn't work. show_action
and friends add their actions to some internal Hobo arrays so they are
known.

Note that the action= attribute is a shortcut; you can always just use
href= and get plain HTML link behavior without magic.

--Matt Jones

LadyBug

unread,
Oct 27, 2009, 8:44:43 AM10/27/09
to Hobo Users
Thanks for the clarification, this feature is something I think should
be more explicitly said in the Hobo cookbook.

So, if I have defined a new action I want to access with POST (i.e.
somewhat different case than above), what would
be the best Hobo-compatible way to get it visible in a view?

Bryan Larsen

unread,
Oct 27, 2009, 9:34:39 AM10/27/09
to Hobo Users
You can use a web method, or a lifecycle transition method. From
your example, it looks like the right thing to do is to use a
lifecycle creator method.

Bryan
Reply all
Reply to author
Forward
0 new messages