Rails 4 Authenticity Token

I think I just figured it out. I changed the (new) default protect_from_forgery with: :exception to protect_from_forgery with: :null_session as per the comment in ApplicationController. # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. You can see the difference by looking at the source for request_forgery_protecton.rb, … Read more

ActiveRecord OR query

If you want to use an OR operator on one column’s value, you can pass an array to .where and ActiveRecord will use IN(value,other_value): Model.where(:column => [“value”, “other_value”] outputs: SELECT `table_name`.* FROM `table_name` WHERE `table_name`.`column` IN (‘value’, ‘other_value’) This should achieve the equivalent of an OR on a single column

Unable to install gem – Failed to build gem native extension – cannot load such file — mkmf (LoadError)

There are similar questions: `require’: no such file to load — mkmf (LoadError) Failed to build gem native extension (mkmf (LoadError)) – Ubuntu 12.04 Usually, the solution is: sudo apt-get install ruby-dev Or, if that doesn’t work, depending on your ruby version, run something like: sudo apt-get install ruby1.9.1-dev Should fix your problem. Still not … Read more

How to test if parameters exist in rails

You want has_key?: if(params.has_key?(:one) && params.has_key?(:two)) Just checking if(params[:one]) will get fooled by a “there but nil” and “there but false” value and you’re asking about existence. You might need to differentiate: Not there at all. There but nil. There but false. There but an empty string. as well. Hard to say without more details … Read more

Rails ActiveRecord date between

Just a note that the currently accepted answer is deprecated in Rails 3. You should do this instead: Comment.where(:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day) Or, if you want to or have to use pure string conditions, you can do: Comment.where(‘created_at BETWEEN ? AND ?’, @selected_date.beginning_of_day, @selected_date.end_of_day)

Generate model in Rails using user_id:integer vs user:references

Both will generate the same columns when you run the migration. In rails console, you can see that this is the case: :001 > Micropost => Micropost(id: integer, user_id: integer, created_at: datetime, updated_at: datetime) The second command adds a belongs_to :user relationship in your Micropost model whereas the first does not. When this relationship is … Read more