Search code examples
ruby-on-railsrubyruby-on-rails-2

How can I put conditionals in my controller?


I'm having problems when I'm trying to put a condition in params in my controller. This code is inside my controller:

if params[:example] == 1
   @table = Model.find(:all,:conditions=>['column_table= ?',params[:example]  ]  )
else
   @table = Model.find(:all,:conditions=>['column_table2= ?',params[:example]  ]  )
end

Is this code correct? How can I put a conditional in params controller?


Solution

  • You code looks okay. Unfortunately you do not say what problem you have. The only thing I see that may fail is the condition. If you pass some value into your params they are not typecasted. Therefore I guess you should use to_i in your condition:

    if params[:example].to_i == 1
      ...