What is the ActiveScaffold syntax for a nested 'has_many' relation link in list view ??

190 views
Skip to first unread message

Jonas Bang

unread,
May 2, 2012, 4:21:24 AM5/2/12
to ActiveScaffold : Ruby on Rails plugin
What is the ActiveScaffold syntax for a nested 'has_many' relation
link in list view ??

The 'has_many' relation is defined and the nested column link in list
view is working out of the box, but I want to overwrite the link
definition in the helper.

So, what is the syntax if I overwrite the link in the helper ??

I.e. I want the exact same functionality as ActiveScaffold generate
automatically, I just want to define it explicitly in the helper to be
able to wrap an 'if' condition around it.

This is my best proposal:
link_to("link text", :controller => "products", :association =>
"parallel_products", :parent_scaffold => "products", :product_id =>
record.id, :adapter => "_list_inline_adapter")

This creates the link, it opens the form, but in html format and on a
new page (not nested).

I have already posted the question here (read for more details):
http://stackoverflow.com/questions/9564028/what-is-the-activescaffold-syntax-for-a-has-many-relation-link-in-list-view-if

Hope someone can help.

Br,
Jonas

Sergio Cambra

unread,
May 2, 2012, 4:39:43 AM5/2/12
to actives...@googlegroups.com
On Miércoles, 2 de mayo de 2012 01:21:24 Jonas Bang escribió:
> What is the ActiveScaffold syntax for a nested 'has_many' relation
> link in list view ??
>
> The 'has_many' relation is defined and the nested column link in list
> view is working out of the box, but I want to overwrite the link
> definition in the helper.
>
> So, what is the syntax if I overwrite the link in the helper ??
>
> I.e. I want the exact same functionality as ActiveScaffold generate
> automatically, I just want to define it explicitly in the helper to be
> able to wrap an 'if' condition around it.
>
> This is my best proposal:
> link_to("link text", :controller => "products", :association =>
> "parallel_products", :parent_scaffold => "products", :product_id =>
> record.id, :adapter => "_list_inline_adapter")
>
> This creates the link, it opens the form, but in html format and on a
> new page (not nested).

You need to set data attributes and add as_action class, like
action_link_html_options;
https://github.com/activescaffold/active_scaffold/blob/master/lib/active_scaffold/helpers/view_helpers.rb#L142

Also, you could add a condition setting a security_method:
config.columns[:column].link.security_method

Jonas Bang

unread,
May 2, 2012, 5:22:12 AM5/2/12
to ActiveScaffold : Ruby on Rails plugin
Ok, so I added the as_action class:

link_to("link text", :controller => "products", :association =>
"parallel_products", :parent_scaffold => "products", :product_id =>
record.id, :adapter => "_list_inline_adapter", :class => "as_action")

It still opens on a new page, not as nested.

How exactly should I set the data attributes ?? I can't seem to figure
that out.

Br,
Jonas

On May 2, 10:39 am, Sergio Cambra <ser...@entrecables.com> wrote:
> On Miércoles, 2 de mayo de 2012 01:21:24 Jonas Bang escribió:
>
> You need to set data attributes and add as_action class, like
> action_link_html_options;https://github.com/activescaffold/active_scaffold/blob/master/lib/act...

Sergio Cambra

unread,
May 2, 2012, 12:28:53 PM5/2/12
to actives...@googlegroups.com
On Miércoles, 2 de mayo de 2012 02:22:12 Jonas Bang escribió:
> Ok, so I added the as_action class:
>
> link_to("link text", :controller => "products", :association =>
> "parallel_products", :parent_scaffold => "products", :product_id =>
> record.id, :adapter => "_list_inline_adapter", :class => "as_action")
>
> It still opens on a new page, not as nested.
>
> How exactly should I set the data attributes ?? I can't seem to figure
> that out.

As you can see in ViewHelpers#action_link_html_options you have to set
:remote, and :data hash.Also, you have added :class to url, instead to options
hash. Don't set adapter, is added by AS with javascript.

link_to("link text", {:controler => "products", :association =>
"parallel_products", :parent_scaffold => "products", :product_id =>
record.id}, :class => 'as_action', :remote => true, :data => {:position =>
:after, :action => 'index'})

Jonas Bang

unread,
May 3, 2012, 5:39:45 AM5/3/12
to ActiveScaffold : Ruby on Rails plugin
Thanks for the help, Sergio, we are almost there now.

Status is, the link generated with your parameters works, i.e. it
opens in nested form. The url is:
http://<server>/products?association=parallel_products&parent_scaffold=products&product_id=23

But, it only works once! After I click the link the first time, the
url changes (adding: /23/row):
http://<server>/products/23/row?association=parallel_products&parent_scaffold=products&product_id=23

When clicking this 'new' link I get a 500 error:
ActiveRecord::RecordNotFound (Couldn't find Product with id=23 [WHERE
"products"."master_product_id" = 23])

Which in itself is true, a product can't have itself set as master,
but why the url changes I have no clue.

So, it works, if only it wouldn't change the url after first click ...

Any ideas to what causes this change of url ??

Br,
Jonas

Sergio Cambra

unread,
May 3, 2012, 5:55:15 AM5/3/12
to actives...@googlegroups.com
If you remove the helper, is the url generated by ActiveScaffold the same?

Jonas Bang

unread,
May 3, 2012, 6:22:26 AM5/3/12
to ActiveScaffold : Ruby on Rails plugin
Yes, if I remove the helper, this is the url (i.e. the same):
http://<server>/products?association=parallel_products&parent_scaffold=products&product_id=23

And this is from Firebug:
<a id="as_products-index-parallel_products-23-link" class="index
as_action" data-remote="true" data-position="after" data-
action="index" href="/products?
association=parallel_products&parent_scaffold=products&product_id=23">link
text</a>

I have added "id" as I found that to be a difference:
link_to(link_text, {:association => "parallel_products",
:parent_scaffold => "products", :product_id => record.id}, :class =>
'as_action',
:id => "as_products-index-parallel_products-#{record.id}-link",
:remote => true, :data => {:position => :after, :action => 'index'})

I did not add "class=index" even that is also a difference.

So, from Firebug I get this link with the helper link_to (works):
<a id="as_products-index-parallel_products-23-link" class="as_action"
data-remote="true" data-position="after" data-action="index" href="/
products?
association=parallel_products&parent_scaffold=products&product_id=23">link
text</a>

And, this link after first click (does not work):
<a id="as_products-index-parallel_products-23-link" class="as_action"
data-remote="true" data-position="after" data-action="index" href="/
products/23/row?
association=parallel_products&parent_scaffold=products&product_id=23">link
text</a>

Br,
Jonas

Jonas Bang

unread,
May 7, 2012, 3:25:57 AM5/7/12
to ActiveScaffold : Ruby on Rails plugin
I added the 'index' to 'class' now, but as expected it still fails
after first click on the link. But at least the links are now
identical when inspected with Firebug.

Any clue to what is missing in order to replicate an exact AS link ??

Br,
Jonas

Jonas Bang

unread,
May 31, 2012, 3:58:48 AM5/31/12
to actives...@googlegroups.com
Been offline for a while (got a baby son), but is now back and struggling with this issue.

Almost got it to work (see my latest post), but only almost.

Sergio, did you have time to look into this? I'm really at a dead end here.

Br,
Jonas

ser...@entrecables.com

unread,
May 31, 2012, 4:46:56 AM5/31/12
to actives...@googlegroups.com
It's working here but I don't have a self association. Have you tried
setting the link in the controller? Is the column an association?

Jonas Bang

unread,
Jun 6, 2012, 3:48:19 AM6/6/12
to actives...@googlegroups.com
Got it to work now, thanks a lot for your help :o)

Here is what I got to work:
    link_to(link_text, {:controller => "products", :association => "parallel_products",
      :parent_scaffold => "products", :product_id => record.id}, :class => 'index as_action',

      :id => "as_products-index-parallel_products-#{record.id}-link",
      :remote => true, :data => {:position => :after, :action => 'index'})

Somewhere in the process I managed to remove the ":controller => 'products'" section. But due to the fact that this is a self association then it almost worked without it, and took a while to find the problem.

Br,
Jonas
Reply all
Reply to author
Forward
0 new messages