Ben, Thanks for the tips. I understand the strip method just removes leading and trailing whitespaces, so this can't be the problem.
I noticed from my server log that the app creates the group, saves it to the user, then tries to create the group again (which it shouldn't), sees that it's already created, and throws the error. I'll try the debugging tools you recommended. Thanks again, Daniel On Wednesday, September 10, 2014 11:53:54 AM UTC-7, Benjamin Wanicur wrote: > > Hi Daniel > > Based on the comments from your StackOverflow post, it appears you are not > getting a "Rails Error", but your validation is failing. So it could be > any of your validation rules. Are you familiar with the debugger gem or > pry ? You can also try logging out your errors. The quickest/dirtiest way > is probably logging like this in your else statement in the > controller#create method: > > logger.debug(@group.errors) > > However, I encourage you to check out tools like debugger or pry. They > can save you lots of time! > > Also, it's worth noting that the strip() method might not be accomplishing > your goal. strip() will only remove trailing and preceding whitespace. > > [45] pry(main)> 'The A Team'.strip > => "The A Team" > [46] pry(main)> ' The A Team '.strip > => "The A Team" > > Cheers > > Ben > > > On Wed, Sep 10, 2014 at 11:26 AM, Daniel Bogart <[email protected] > <javascript:>> wrote: > >> I have a User Group table with a group_name value that is entered when >> the group is created. If there is a space in the group name, for example >> "The A Team", the group name is still created, but it is not assigned to >> the current user, and it throws the error message in my if/else statement >> in the controller. Code is as follows: >> >> Controller: >> >> def create >> @group = current_user.create_user_group(group_params) >> current_user.user_group = @group >> current_user.save >> if @group.valid? >> redirect_to '/user_groups/'[email protected]_name, :notice => "Your >> group has been created" >> else >> redirect_to '/user_groups/', :error => "Error: group name may >> already be taken. Search, or try a new name." >> endend >> >> Model >> >> class UserGroup < ActiveRecord::Base >> >> has_many :users >> >> has_secure_password >> validates :password, :presence => true >> >> validates :group_name, :presence => true, :uniqueness => true >> >> before_validation :strip_blanks >> def strip_blanks >> self.group_name = self.group_name.stripend >> >> Create form: >> >> <div class="form-group"> >> <label for="Group Name">Enter group name</label> >> <%= f.input :group_name, :required => true, :autofocus => true, >> :maxlength => 40, :input_html => { :class => "form-control" }, :label => >> false, :placeholder => "Group name" %></div><div class="form-group"> >> <label for="Password">Password</label> >> <%= f.input :password, :required => true, :autofocus => true, :maxlength >> => 40, :input_html => { :class => "form-control" }, :label => false, >> :placeholder => "Group password" %></div> >> <%= f.button :submit, :class => "btn btn-md btn-warning" %></div> >> >> >> Link to StackOverflow post if you want some >> points:http://stackoverflow.com/questions/25757686/rails-spaces-in-string-used-for-name-causes-validation-error >> Thanks! >> >> Daniel >> >> -- >> -- >> SD Ruby mailing list >> [email protected] <javascript:> >> http://groups.google.com/group/sdruby >> --- >> You received this message because you are subscribed to the Google Groups >> "SD Ruby" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- -- SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby --- You received this message because you are subscribed to the Google Groups "SD Ruby" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
