Determining checked checkboxes

Hi guys
Am a newbie in rails please forgive my ignorance.
i have set of search results

i have appended a checkbox to each of the results and would like to give
the user some freedom to choose what he/she likes and manipulate or
explore them further.

my problem is how to determine which checkboxes are checked and return
an array of their names, which the server can process.

how do you achieve that in rails?

Thank you.

If I understand you correctly, then in your controller you would like an
array of the checked items which you will access from params.

I have recently done this for assigning roles to users:

My controller action looked like this:

def assign_roles
user_roles = params[:role]
user_roles.each do |job|
UserRole.create(:user_id => params[:user_id], :role_id =>
job.to_i)
end
redirect_to ‘somewhere else’
end

The params[:role] in this case is an array of checked options from the
form.
To see how this is achieved here is the view code:

    <% for role in @roles %>
        <tr>
            <td><%= role.role_type %></td>
            <td><%= check_box_tag "role[]", role.id, checked=false

%>

<% end %>

As you can see, the name for the check_box_tag is an array - role[]. The
value that is returned is the id of the role (as a string - hence the
to_i
after job in the controller).
The value is only returned if the checkbox is checked so you get an
array of
all the checked id’s.

Regards
Ivor

Ivor P. wrote:

If I understand you correctly, then in your controller you would like an
array of the checked items which you will access from params.

I have recently done this for assigning roles to users:

My controller action looked like this:

def assign_roles
user_roles = params[:role]
user_roles.each do |job|
UserRole.create(:user_id => params[:user_id], :role_id =>
job.to_i)
end
redirect_to ‘somewhere else’
end

The value is only returned if the checkbox is checked so you get an
array of
all the checked id’s.

Thanks Ivor,

i got the method right but am wondering what is happening when i do
this:

My view has this
<%form_tag :action=>‘selected’ do%>
<%for result in @results%>

<check_box_tag “result[]”,result.id,checked=false%>
<%end%>
<%end%>

And the in the controller i have this method

def selected
@selected_items=params[:result]
end

The selected.rhtml
<%[email protected]%>

however i get a blank page. but when i inspect the logs i see that the
parameters were posted.

how do i get the checked ids rendered or displayed to the user?

Thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs