L
L
Lilion2014-01-13 14:47:44
ruby
Lilion, 2014-01-13 14:47:44

Ruby, Sinatra selector for specific parameter

There is a standard Sinatra blog code:

post '/articles' do
  article = Article.new(params[:article])

  if article.save
    redirect '/articles'
  else
    redirect '/articles/new'
  end
end

The form has, respectively: id, title, content.
How to immediately apply the UpCase parameter to the title in such code, so that the result would be sent to the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jj_killer, 2014-01-13
@Lilion

If it's an ActiveRecord, then you can use the before_save callback: api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
Like this:

class Article < ActiveRecord::Base
  before_save :upcase_title

  private

  def upcase_title
    self.title.upcase!
  end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question