strong-parameters

Permit extra params in special cases with Strong Params in Rails 4

一个人想着一个人 提交于 2019-12-06 05:42:10
问题 So for an organization, I want users to be able to be able to edit some things about it. params.require(:organization).permit(:name, :location) But in special cases, I want administrators to be able to edit extra attributes params.require(:organization).permit(:name, :location, :secrets) Now I know I can just have an if statement to choose which line I want to use, but since the admin will always be able to edit the original attributes, I wanted to easily be able to include them like so:

rails 4 has_many :through not saving associations

我只是一个虾纸丫 提交于 2019-12-06 03:29:13
I have 2 models with many to many association as follows: class User < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :designated_remarks, :through => :remark_users, :source => :remark end class Remark < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :users, :through => :remark_users accepts_nested_attributes_for :users end And the relationship: class RemarkUser < ActiveRecord::Base belongs_to :remark belongs_to :user end The remarks_controller action that should do the save: # PATCH Save users def save_users @remark = Remark.find(params[

Strong parameters and multidimensional arrays

馋奶兔 提交于 2019-12-06 00:34:14
问题 I'm using Rails 3.2.6 with strong parameters gem. So, I' have a controller with the typical update action: # PUT /api/resources/:id def update @resource.update_attributes! permited_params respond_with_json @resource, action: :show end Then, I have the permited_params method def permited_params params.permit(:attr1, :attr2, :attr3) end The problem is that :attr3 is a multidimensional array like this: [[1, 2], [2, 5, 7]] Following the documentation, I need to specify :attr3 as an array. But...

How to use Rails Action Controller Nested Params to Permit a Specific Attributes Hash

折月煮酒 提交于 2019-12-05 17:48:40
There are a lot of questions about Nested Parameters, but I can't seem to find one that addresses my specific, simple situation. I'm trying to permit a nested hash that is NOT an array. I expected this to work: params.require(:book).permit(:title, :description, style: {:font, :color}) But it resulted in a syntax error. This, however, worked: params.require(:book).permit(:title, :description, style: [:font, :color]) But my issue with this, it it seems to permit style values that are arrays of items with attributes :font and :color. I only want to permit a single hash with those 2 attributes. I

Rails: Merging a nested attribute with strong_params

落爺英雄遲暮 提交于 2019-12-05 16:03:40
问题 In Rails 4, it's possible to merge extra parameters with user generated ones like so: params.require(:post).permit([:title, :body]).merge(user: current_user) It's also possible to include nested attributes like so: params.require(:post).permit([:title, :body, sections_attributes: [:title, :section_type]]) Now, what if I wanted to merge extra parameters into a nested model. I tried this: params.require(:post).permit([:title, :body, sections_attributes: [:title, :section_type]]).merge(user:

Nested strong parameters in rails - AssociationTypeMismatch MYMODEL expected, got ActionController::Parameters()

≡放荡痞女 提交于 2019-12-05 15:25:59
问题 I'm rendering a model and it's children Books in JSON like so: {"id":2,"complete":false,"private":false, "books" [{ "id":2,"name":"Some Book"},..... I then come to update this model by passing the same JSON back to my controller and I get the following error: ActiveRecord::AssociationTypeMismatch (Book (#2245089560) expected, got ActionController::Parameters(#2153445460)) In my controller I'm using the following to update: @project.update_attributes!(project_params) private def project_params

Can't Save Image Attributes with Paperclip in Rails 4

自古美人都是妖i 提交于 2019-12-05 13:11:36
I have two associated models in my Rails 4 app: product.rb and image.rb . The Image model allows attached files using the Paperclip gem. Images belong_to a Product, and a product has_many Images. I would like to use the Product's new view to create and attach an image when I create a product. Each time I try, the parameters associated with the Paperclip image do not get saved, and end up nil . Here are the models: Product.rb class Product < ActiveRecord::Base validates :name, :part_number, presence: true has_many :images, dependent: :destroy belongs_to :category accepts_nested_attributes_for

rails 4 strong params: make them conditional?

寵の児 提交于 2019-12-05 10:27:53
Is there a way to make strong_params conditional? Without the need to write 2 separate methods? In case where one would like to add certain attributes to the permit list when a certain condition is true For example: devise_parameter_sanitizer.for(:user) {|u| u.permit(:user, :email, :role, )} I have this :role attribute permitted in above example. I only want this attribute to be permitted when in Rails.env.development is there a way to do this? Does this achieve the desired results? user_params = [ :user, :email, (:role if Rails.env.development?) ].compact devise_parameter_sanitizer.for(:user)

rails 4 with link_to and method post with strong parameters

一曲冷凌霜 提交于 2019-12-05 09:46:27
I'm stuck in a problem which can't be that complicated, but I'm just not getting things right. Assuming I've got two Models: class Notification < ActiveRecord::Base belongs_to :device validates :number, presence: true end and class Device < ActiveRecord::Base belongs_to :user has_many :notifications, :dependent => :destroy //rest omitted for brevity end with nested routes like so: resources :devices do resources :notifications end and a notifications controller like so: class NotificationsController < ApplicationController before_filter :authenticate_user! before_action :set_device, :only => [

whitelisting deeply nested strong parameters in rails

一曲冷凌霜 提交于 2019-12-04 21:14:16
I'm trying to store a serialized Ransack search in a text column. It's deeply nested and I'm struggling coming up with permit call for it. Here's a sample hash: { "c"=>{ "0"=>{ "a"=>{ "0"=>{ "name"=>"column_1" } }, "p"=>"eq", "v"=>{ "0"=>{ "value"=>"value_1" } } }, "1"=>{ "a"=>{ "0"=>{ "name"=>"column_2" } }, "p"=>"cont", "v"=>{ "0"=>{ "value"=>"value_2" } } } } } How would you write a permit for that? This is my best guess for reading the doc but it isn't working. def course_listing_params params.require(:course_listing).permit({ q: { c: [{ a: [:name] }, :p, { v: [:value] }] } }) end I ended