Question

I have a :name attribute which i use for linking to User's Show Profile.

I needed the storing in the Database to be without Spacing so i used Gsub.

def name=(value)
        write_attribute(:name, value.gsub(/\s+/, ""))
end

But upon Displaying that value again on some Show Page via

<%= link_to @show.name, @show %>

the name get's displayed without Spaces.

I Tried

raw(@show.name)

but this didn't worked either.

How do i maintain the un-spaced in the link but showing it with spaces on any page ?

My Model:

class Show < ActiveRecord::Base

    belongs_to :user

    validates :name, :presence => true, :uniqueness => true
    validates :announcement, length: { maximum: 250 }

    # Show Cover
    has_attached_file :cover, styles: { show_cover: "870x150#"}
    validates_attachment :cover,
                                             content_type: { content_type: ['image/jpeg', 'image/jpg',
                                                                                                            'image/png'] },
                                             size: { less_than: 5.megabytes }


    def to_param
        name
    end

    def name=(value)
        write_attribute(:name, value.gsub(/\s+/, ""))
    end

end
Was it helpful?

Solution

I Display it mainly. On a variety of controllers.

If it's just for display, don't strip out spaces. Store the actual name, then define a method username which removes spaces from name.

Something like following should work:

# Model
class Show < ActiveRecord::Base
    # Returns name removing all spaces
    def username
      self.name.gsub(/\s+/, "")
    end

    # Remove just the leading and trailing white spaces
    def name=(value)
        write_attribute(:name, value.strip)
    end
end

Then in your controller(s):

<%= link_to @show.username, @show %>

Update:

After having a chat with OP, understood that he is using to_param method in Show model which returned username (i.e. name with all spaces removed). This caused a problem in find method because the value in DB included spaces and the value being searched for did not have any spaces, as a result of which a DB specific function needed to be used e.g. where("replace(name, ' ', '') = ?", user_name) in case of MySQL. Since the OP is using different databases in development and production, i.e. SQLite in development and PG in production. So, instead of crafting a solution that worked across different databases, recommended OP to use FriendlyId gem.

OTHER TIPS

If spaces are the problem, why not convert them to underscores and then use a helper to display that?

def name=(value)
  write_attribute(:name, value.gsub(/\s+/, "_"))
end

You could then create a helper method that's just for printing/displaying the name.

  • Create a app/helpers/users_helper.rb
  • Put something like this in that helper:

    module UsersHelper
      def pretty_name(user)
        user.name.gsub('_', ' ')
      end
    end
    
  • In your view you could call it by doing something like: <%= pretty_name(@user) %>

If using a delimiter in the name field is not an option, then you should consider modifying the users table to have first_name, middle_name, last_name, etc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top