​Daniel,

​
Rails (as usual) has a built-in way to handle taking strings and
​m​
aking them URL safe.
​"​
Wow thi$ is greÅt✈︎
​"​
.parameterize gives you "wow-thi-is-great”

Spaces aren’t the only thing you have to worry about, so #parameterize
takes that all into account.

Also, Rails leverages the fact that “1-foo”.to_i = 1, and by default
creates IDs for URLs that look like /user_groups/1-the-a-team.  Now, some
folks don’t like that integer in the URL, and do things to get rid of it,
but it has the nice property of surviving renames, since even if the text
changes the ID won’t change. So if they change the group name to The B
Team, the new url (/user_groups/1-the-b-team) and the old url
(/user_groups/1-the-a-team) will both work. Something to think about.

​
​- john
​





On Wed, Sep 10, 2014 at 1:08 PM, Daniel Bogart <[email protected]>
wrote:
>
> I think I got it - it was a URI issue.
> When a name with a space was created, the user was routed to
/user_groups/The A Team.
>
> It my routes I have /user_groups/:group_name routing to that group's home
page.
> However, since those spaces don't register correctly, the app seems to
run another POST request to create a group instead.
>
> I simply changed my redirect to redirect_to
'/user_groups/'[email protected]_name.gsub(/\s/,'%20'), :notice => "Your group
has been created"
> Let me know if you think there's any problems with the way I handled this.
>
> Thanks,
> Daniel
>
> On Wednesday, September 10, 2014 1:00:13 PM UTC-7, Daniel Bogart wrote:
>>
>> Kevin,
>>
>> When I submit the form to create a group my app creates the group, saves
it to the user, then tries to create the group again (which it shouldn't),
sees that there is already a group with that group name (obviously, because
it was just created seconds ago), and throws the error.
>>
>> Your refactoring of my controller method helped - now the group is
created, assigned to the user, user gets redirected to the
'/user_groups/'[email protected]_name appropriately, but the error message:
"Error: group name may already be taken. Search, or try a new name." still
gets posted.
>>
>> I just can't figure out why the create method is being run more than
once...
>>
>> -Daniel
>>
>> On Wednesday, September 10, 2014 12:24:03 PM UTC-7, Kevin Thompson wrote:
>>>
>>> Daniel,
>>>
>>> Is it creating multiple groups with the same name in your database?
What it looks like could be happening is that your group name may already
exist. If that’s the case, the `create_user_group` method call fails due to
your uniqueness validation (see Ben’s note about `strip`), then the user’s
user_group is assigned to a record that has not been persisted. Ultimately
the `valid?` check on @group fails and redirects to your page that includes
your error message.
>>>
>>> The simplest change you could make to account for this is to check the
validity of your group before saving the user record:
>>>
>>> def create
>>>   @group = current_user.create_user_group(group_params)
>>>   if @group.valid?
>>>     current_user.user_group = @group
>>>     current_user.save
>>>     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."
>>>   end
>>> end
>>>
>>>
>>> Also, if your reasoning for removing the spaces from the group name is
to use the name as a URL parameter, you may want to consider using gsub in
addition to strip in order to substitute spaces for another character:
>>>
>>> def strip_blanks
>>>   self.group_name = self.group_name.strip.gsub(/\s/,'-')
>>> end
>>>
>>>
>>> That gsub statement will replace any space character inside of the name
with a dash.
>>>
>>>
>>> On Wednesday, September 10, 2014 11:26:21 AM UTC-7, Daniel Bogart 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."
>>>>     end
>>>> end
>>>>
>>>> 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.strip
>>>> end
>>>>
>>>> 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]
> 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.

-- 
-- 
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.

Reply via email to