Basic role management
def manage_roles
if params[:id]
@user = User.find(params[:id])
@roles = Role.find(:all)
# if user submits the form then update
if request.post?
@user.roles.delete_all
params[:role_ids].each {|r| @user.roles <<
Role.find_by_id(r.to_i)}
# EXECUTION STOPS HERE
# IF I REMOVE THE LINE ABOBE, THE CODE WORKS
# if ajaxed -> do some visual thingy
if request.xml_http_request?
render :update do |page|
page.visual_effect :SlideUp,
''userroles''+params[:id]
end
else
# elseif regular request -> go back to the list of users
redirect_to(:action => ''list'')
end
else
# if not submited just display the form
if request.xml_http_request?
render :update do |page|
page.replace_html ''userroles''+params[:id], :partial
=>
''manage_roles''
page.visual_effect :SlideDown,
''userroles''+params[:id]
end
else
render :partial => ''manage_roles'', :layout =>
''admin''
end
end
end
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Alex Copot wrote:> Basic role management > > def manage_roles > if params[:id] > @user = User.find(params[:id]) > @roles = Role.find(:all) > > # if user submits the form then update > if request.post? > @user.roles.delete_all > params[:role_ids].each {|r| @user.roles << > Role.find_by_id(r.to_i)} > > # EXECUTION STOPS HERE > # IF I REMOVE THE LINE ABOBE, THE CODE WORKSparams[:role_ids] is most likely a string. $ irb irb(main):001:0> ''ab''.each { |s| puts "eacher: #{s}" } eacher: ab => "ab" How are you assembling the role_ids parameter on the webpage? Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<% form_remote_tag :url => { :action =>
''manage_roles'', :id => @user} do
%>
<% for role in @roles -%>
<% if session[:usersuper] %>
<input type="checkbox" id="role_id_<%=
role.id%>"
name="role_ids[]" value="<%= role.id %>" <%if
@user.roles.include?
role%> checked="checked"<%end%>/>
<% end %>
<label for="role_id_<%= role.id %>"><%=
role.name
%></label><br/>
<% end %><br/>
<%= submit_tag ''Save'' %>
<% end -%>
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Yeap, I got it. The role_ids array was nul because none of the inputs
were checked and therefore it returned nil.
params[:role_ids].each {|r| @user.roles << Role.find_by_id(r.to_i)} if
params[:role_ids] -> this seemed to help
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
What is the error you get? Stephan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Good. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I didn''t get any error. The code simply stopped executing after that
line.
But I got it now.
When the user didn''t selected anything (role_ids is an array of values
returned from checkboxes), params[:role_ids] was nil and it stopped the
execution.
# returns nil when params[:role_ids] is nil -> stopping the execution ->
the code after this line never executed
params[:role_ids].each {|r| @user.roles << Role.find_by_id(r.to_i)}
#This was a problem only when the user didn''t check any of the
checkboxes.
#If he checked an inputbox the code following that line worked.
#To prevent this from happening in this case (when none of the values
are selected), I simply added ''if params[:role_ids]'' to ensure
that this
line is only executed when the user selects something.
@user.roles.delete_all - #deletes all roles
params[:role_ids].each {|r| @user.roles << Role.find_by_id(r.to_i)} if
params[:role_ids] # - only executed when at least one value is checked
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---