views:

34

answers:

1

I have a list of users that have requested access to my application.

They appear in a table inside a view:

<table>
    <thead>
        <th>Company</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Role</th>
        <th>Action</th>
    </thead>
    <% for ar in @brand.access_requests %>
        <% user = ar.user %>
        <tr id="user_<%= user.id %>">
            <td><%= user.company %></td>
            <td><%= user.first_name %></td>
            <td><%= user.last_name %></td>
            <td><%= user.email %></td>
            <td><%= collection_select :role, :id, Role.all, :id, :name %></td>
            <td><%= link_to "Create", brand_responsibilities_path(@brand), { :remote => true, :method => :post } %></td>
        </tr>
    <% end %>
</table>

So I want to assign a Role to each user, then approve that user.

Obviously this does not work, because I do not know how to refer to the "role_id" field from my link_to.

How do I include a value from another field in my link_to? Is there a way to do a ":with" or something like that? I want to pass whatever is chosen in the select box to:

brand_responsibilities_path(@brand)

I know it would work if I created a form for every single row, but is that overkill, or the Rails Way? Any help is seriously appreciated.

+1  A: 

Hi, I would definitely use the form. The only way that occurs to me to do this using a link is with javascript. Apparently when you do link_to :method => :post, Rails creates a form with javascript!. So I think it's definitely cleaner to use the form:

<% form_for user do |f| %>
  <%= f.collection_select :role_id, Role.all, :id, :name %>
<% end %>
jordinl
The only problem here is where do I put the form submit button/link? I want it to be in another column. Is it even permissible in HTML to embed forms within tables outside the confines of a TD tag?
AKWF
Stick the form outside the table. You can snag the link using Javascript and trigger the form's submit action. I don't know if you're using Prototype of jQuery, but in either case, just set up a `click()` handler to watch the link. Then in the link handler, you submit the form. However... this can be somewhat hostile to accessibility. It's your design call.
Steve Ross
Ok, stepping back, thinking... You want each row to have a separate form. My interpretation is that you can put a form in a table cell so long as it is completely contained inside the TD. If you want a form per row, that would work. Otherwise do like I said before and shift the load to the Javascript to figure out what's being submitted.
Steve Ross
He can't put the form outside the table, because there should be a form for each row. I don't think there should be a problem doing <td><% form_for ... </td>, but some HTML expert would give you a better answer.
jordinl
Agree with @jordinl.... Just have the <td><form><select><button></td> all in the same table cell.
Jesse Wolgamott
I actually have it like this: <table><form><tr><td><field1></td><td><select1></td><td><submit></td></tr></form></table>.It's working, at least in Safari. Is this OK or am I violating a ton of HTML rules?"Putting the load on Javascript", that's what's been driving me crazy. Do I just put a link out there, put a function in application.js, bind the function to the link, and do the submission that way? I have never bound any Javascript to anything before, which is why I ask.
AKWF