How to override ‘Create New’ for activescaffold associations

In the helper file, ie tablenames_helper.rb, override the render_action_link method

 def render_action_link(link, url_options, record = nil, html_options = {})
    if link.parameters[:association] == :my_association
       # special handling here
    else
       super  # in case you have other associations for which you do want the default behavior
    end
 end

This confounded me for a while as it was easy to create a helper to override the link to associated records, but if the records did not yet exist the ‘Create New’ appeared hardcoded. After inspection of ./bundler/gems/active_scaffold-45451d963672/lib/active_scaffold/helpers/list_column_helpers.rb and ./bundler/gems/active_scaffold-45451d963672/lib/active_scaffold/helpers/view_helpers.rb the above override became apparent. Is there an easier way to determine this sort of thing?

2 Comments

  1. Volker Hochstein says:

    Maybe the following helps you:
    https://github.com/activescaffold/active_scaffold/wiki/API%3A-Column

    actions_for_association_links

  2. iwork says:

    Hi Vokler – thank you for answering! I really appreciate the work on active_scaffold, I am using it extensively. I’m not sure though that any of the API: Column functions lets me modify the action; maybe I am setting it up wrong at an earlier step. Here is my actual code:

    def render_action_link(link, url_options, record = nil, html_options = {})
       if link.parameters[:association] == :qua_result and link.action == 'new'
          link_to "Retrieve Results", :controller => "qua_results", :action => "retrieve", :job => record
       else
          ...
       end
    end

    So what I am doing is maybe not kosher, in that I am changing the action from :new to :retrieve. I do not want the user to be prompted with a form to create a new ‘results’ record, instead I want to retrieve a set of results from a remote location. I guess I could have allowed :new and added a callback, but I do not want the user to have to see a form at all. I just want to retrieve the results and then update the screen so that the user can see how many results there are and do things with them once they are retrieved.

    I am new to Rails so it is very possible I am not doing this the ‘right’ way.

Leave a Reply