RailsCasts Pro episodes are now free!

Learn more or hide this

Pierre Urban's Profile

GitHub User: purban

Comments by Pierre Urban

Avatar

If you use this with more than 1 model here is an example of how you could refactor your PermittedParams class to allow more flexibility:

ruby
class PermittedParams < Struct.new(:params, :user)
  %w(topic user).each do |m|
    define_method(m) { params.require(m.to_sym).permit(*send("#{m}_attributes".to_sym)) }
  end
  def topic_attributes
    if user && user.admin?
      [:name, :sticky]
    else
      [:name]
    end
  end
  def user_attributes
    [:email, :password, :password_confirmation]
  end
end