Topic: Partials question

I'm very new to Ruby on Rails.

Right now I have a list of names stored in a DB and each list has_many associations.  Each name is associated with a user.

I want to be able to display each name and have the user be able to click "expand" to see a bulleted list of associations (and hopefully click it again to go away, and click an "expand all" button to see everything).  Right now I'm just looping through both lists and displaying them like this

NAME1
- assoc1
- assoc2
- assoc3

NAME2
-assoc4
-assoc4

etc. etc.

What I thought I might be able to do was to link to a partial (or even link back to the page itself, and pass a parameter of the name ID.  If the name ID matches the current name being displayed, then it would list the associations below.

But I can't see how to pass parameters with link_to.

Also, if anybody has any other suggestions on how to do this, I'd like to know.

Thanks

Re: Partials question

I don't know so much about the other stuff, but passing parameters with link_to is easy!

You can simply type,

<%= link_to(:action => "whatever_action", :param => arbitrary_parameter, :etc => "You get the idea.", enough_examples => true) %>

Then you can pull them out in the whatever_action method by saying things like,

[code:ruby] display_string = params[:etc][/code]

or

[code:ruby]
def whatever_action
  @user = User.find_by_name("brokenidealism")
  if params[:enough_examples]
    render(:action => "shut_up", :id => @user)
  end
end

def shut_up
  @user = User.find(params[:id])
  @user.allowed_to_talk = false
  @user.save
end
[/code]

You get it?

God is a storyteller.

Re: Partials question

I'm assuming you want to do this with Javascript. In this case you can use link_to_function to stick a bit of Javascript in a link.

<% for name in @names %>
  <%= name.content %>
  <ul id="associations_<%= name.id %>"
    <% for association in name.associations %>
      <li><%= association.content %></li>
    <% end %>
  </ul>
  <%= link_to_function 'toggle associations', "Element.toggle('associations_#{name.id}')" %>
<% end %>

Untested.

Railscasts - Free Ruby on Rails Screencasts

Re: Partials question

Well, sure, if you wanna do it the cool way..

God is a storyteller.

Re: Partials question

If I don't have to write javascript to do it, that would be better.  But if I  can use the link_to_function to call a javascript, that might not be too bad.

I'm trying brokenidealism's way first... I'll report back if I still can't figure it out.