activerecord

How best to sanitize fields in ruby on rails

北城余情 提交于 2020-01-10 19:30:07
问题 I currently have a controller capturing some html from TinyMCE on the front end. If I tinker with firebug it is possible to submit script tags and inject alert messages etc on to the screen. edit: Currently I am fixing this in the model by using the sanitize helper: require 'action_view' class NotesController < AuthApplicationController include ActionView::Helpers::SanitizeHelper ... def update params[:note][:content] = sanitize(params[:note][:content], :tags => %w(a object p param h1 h2 h3

Passing @object into a rails partial render

一笑奈何 提交于 2020-01-10 12:32:07
问题 I have a partial: 'profiles/_show.html.erb' that contains code like <%= @profile.fullname %> I'm trying to render the partial but I'm not sure how to pass the @profile. I tried using local but apparently it sets 'profile' on my partial instead of '@profile'. <%= render :partial => 'profiles/show', :locals => {:profile => @app.profile} %> Is there anyway to pass it as @object instead of object, or is it designed this way? 回答1: Why is it so important that you use an instance variable(variables

Check if db->update successful with Codeigniter when potentially no rows are updated

↘锁芯ラ 提交于 2020-01-10 12:04:28
问题 I've been using $this->db->affected_rows() to check if updates have been successful. But this doesn't work when a user inputs the same data to be stored that is already stored (because no column is actually updated). My workaround has been to get the stored data, compare to the inputted data, update if different, return a NO_CHANGE constant if same. With JSON data, there's an extra parsing step. Is there an easier way to check that there was no error with the update? As in, something that

Polymorphic Association with multiple associations on the same model

好久不见. 提交于 2020-01-10 08:26:04
问题 I'm slightly confused about a polymorphic association I've got. I need an Article model to have a header image, and many images, but I want to have a single Image model. To make matters even more confusing, the Image model is polymorphic (to allow other resources to have many images). I'm using this association in my Article model: class Article < ActiveRecord::Base has_one :header_image, :as => :imageable has_many :images, :as => :imageable end Is this possible? Thanks. 回答1: Yep. That's

ActiveRecord validates… custom field name

别来无恙 提交于 2020-01-10 05:35:27
问题 I would like to fix up some error messages my site generates. Here is the problem: class Brand < ActiveRecord::Base validates_presence_of :foo ... end My goal is to make a message "Ticket description is required" instead of "Foo is required" or may not be blank, or whatever. The reason this is so important is because lets say previously the field was ticket_summary. That was great and the server was coded to use that, but now due to crazy-insane business analysts it has been determined that

DateTime arithmetic in an ActiveRecord query (PostgreSQL)

时间秒杀一切 提交于 2020-01-10 04:08:15
问题 I have two datetime columns in a User / users table: created_at and birthdate . I'd like to find users whose birthdate is less than 13 years before their creation date. The equivalent Rails if statement would be ((created_at - birthdate) < 13.years) How do I translate this to an ActiveRecord query (one that'll work on PostgreSQL)? Is this even possible, or will I have to iterate over all records manually? 回答1: The easiest way to do this is to use an interval, then it is pretty much a straight

Rails select subquery (without finder_sql, if possible)

廉价感情. 提交于 2020-01-09 11:29:12
问题 I have an model called Object (doesn't really matter what it is) It has a default price (column is called "price"). And then there is a Schedule object that allows to override the price for specific dates. I want to be able to determine the MINIMUM price (which is by definition the MINIMUM between the default and "current" price) during the SQL-query just in order to be able to ORDER BY the calculated minimum price I want to make my search query as efficient as possible and I was wondering if

In Ruby on Rails, what's the difference between create and create! and API docs doesn't have it?

久未见 提交于 2020-01-09 10:24:53
问题 ActiveRecord has create and some people use create! ... Is it that create! can raise an exception while create doesn't? I can't find create! in the current Rails API docs... 回答1: Yes, create! will raise an exception on failure, create just returns false. Documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-create-21 回答2: I have tested it in Rails 4.2.0 . In this version of Rails, it seems, #create! works as said in the other answer, but

In Ruby on Rails, what's the difference between create and create! and API docs doesn't have it?

感情迁移 提交于 2020-01-09 10:23:09
问题 ActiveRecord has create and some people use create! ... Is it that create! can raise an exception while create doesn't? I can't find create! in the current Rails API docs... 回答1: Yes, create! will raise an exception on failure, create just returns false. Documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-create-21 回答2: I have tested it in Rails 4.2.0 . In this version of Rails, it seems, #create! works as said in the other answer, but

Why does Rails ignore a Rollback in a (pseudo)nested transaction?

孤者浪人 提交于 2020-01-09 10:00:43
问题 As per the docs ActiveRecord::Transactions::ClassMethods, a non-new nested transaction will ignore a Rollback. From the docs: User.transaction do User.create(username: 'Kotori') User.transaction do User.create(username: 'Nemu') raise ActiveRecord::Rollback end end The raise ActiveRecord::Rollback is ignored because it is in a child transaction (or rather, it is still within the parent transaction and not its own). I do not understand why the Rollback call would be ignored by both? I can see