开发者

Rails how to allways gsub column before save?

I am trying to make gsub on my column before save or update.

Here is my controller:

        def dansk(text)
    self.text.gsub('å', 'å')
    self.text.gsub('Å', 'Å')
    self.text.gsub('æ', 'æ')
    self.text.gsub('Æ', 'Æ')
    self.text.gsub('ø', 'ø')
    self.text.gsub('Ø', 'Ø')
    end

def update  

        @photographer = Photographer.find(params[:id])          
        @photographer.update_attributes(params[:photographer])
        @photographer.text = dansk(params[:photographer][:text])
            @photographer.text = dansk(params[:photographer][:name])


                       !@photographer.save                              
            flash[:notice] = " "
            render_action 'edit'            


    end

What are I am doing wrong and why are the text and name not being "gsubbed" ?

UPDAT开发者_开发百科E: My helper:

def convert_html_entities(text)
    text.gsub(/å/,"å")
    text.gsub(/æ/,"æ")
    text.gsub(/ø/,"ø")   
    text.gsub(/©/,"©")
    text = text.gsub(/["]/, '"')       
end 


You should do this at the Model level (maybe put your dansk method in a module if you want to keep things DRY).

before_save :danskify

def danskify
  self.text = dansk(self.text) if text_changed?
  self.name = dansk(self.name) if name_changed?
end

def dansk(text)
  [['å', 'å'], ['Å', 'Å'], ['æ', 'æ'], ['Æ', 'Æ'], ['ø', 'ø'], ['Ø', 'Ø']].each do |rule|
    text = text.gsub(rule[0], rule[1])
  end
end

In your controller, you'd only need:

def update  
  @photographer = Photographer.find(params[:id])          
  @photographer.update_attributes(params[:photographer])
  flash[:notice] = " " #weird message BTW
  render :edit  
end


No offense, but your code doesn't make any sense. Is 'def dansk' in your controller. There are multiple problems.

How to get it work

  1. In dansk it should be "text.gsub" not "self.text.gsub". self in that context is a reference to the controller itself.
  2. Calling text.gsub a bunch of times won't do what you want. gsub returns a new string with the offending chars replaced, leaving the original string unchanged. So you're basically losing all those changes since you're only returning the final one. It should look like this

    def dansk(text)
      text.gsub('å', 'å').gsub('Å', 'Å')...
    end
    

or this (gsub! applies it's changes to the original string)

def dansk(text)
  text.gsub!('å', 'å')
  text.gsub!('Å', 'Å')
  ...
  text
end

The best way

This way, every time you call 'save' on a Photographer object, those chars you don't want will be replaced automatically.

model Photographer
  ...
  before_validation :dansk!
  ...

  private

  def dansk!
    self.text.gsub!('å', 'å')
    self.text.gsub!('Å', 'Å')
    ...
  end
end

@photographer = Photographer.find(params[:id])          
@photographer.update_attributes(params[:photographer])
@photographer.save


Neglecting other strange issues with your code, you want the gsub!() method (note the exclamation mark). gsub!() modifies the String in place, while gsub() (no exclamation mark) returns a new String object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜