1

I have problem with creating more than object with json. Question is how should I change create action in controller and permited parameteres to make this action possible. Unfortunatelly I didn't find any solutions in net... Hope to find someone more experienced here.

I have read suggested article: how to permit an array with strong parameters but it doesn't work for me.

Error I have is:

NoMethodError: undefined method `permit' for #Array:0x007ff6e0030438

I have changed parameters, but still have the same error!!!!

I want to create posts from external service. At the moment I send this json with Postman:

{   "post":
    [
        {
            "post_title":"Title 2",
            "post_body":"body of the post 2",
            "user_id": 1
        },
        {
            "post_title":"Title",
            "post_body":"body of the post",
            "user_id": 1
        }
    ]
}

My controller:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :update, :destroy]

  def create
    @post = Post.new(post_params)

    if @post.save
      render json: @post, status: :created, location: @post
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end

  private

    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:post_title, :post_body, user_ids:[])
    end
end
Community
  • 1
  • 1
elmuzyk
  • 93
  • 12
  • You are trying to pass array of post and permit will not work on array. this may help you http://stackoverflow.com/questions/16549382/how-to-permit-an-array-with-strong-parameters – Pardeep Dhingra May 11 '17 at 13:26
  • Possible duplicate of [how to permit an array with strong parameters](http://stackoverflow.com/questions/16549382/how-to-permit-an-array-with-strong-parameters) – Pardeep Dhingra May 11 '17 at 13:28
  • As I've written above, I've tried the solution from this article and didn't work – elmuzyk May 11 '17 at 13:58
  • @elmuzyk Did I understand you right you want to create multiple posts by a time? As I see your Postman query – DjezzzL May 11 '17 at 20:42
  • Exactly. that's what I want to do. – elmuzyk May 15 '17 at 06:41

2 Answers2

3

I finally found a solution to my problem. When creating an object from an array, there have to be another method in controller that iterates through passed array

The JSON:

{   "posts_list":
    [
       {
           "post_title":"Title 2",
           "post_body":"body of the post 2",
           "user_id": 1
       },
       {
           "post_title":"Title",
           "post_body":"body of the post",
           "user_id": 1
       }
   ]

Controller:

def mass_create
   statuses = []
   params[:posts_list].each do |post_params|
      auth = Post.new(select_permited(post_params))
      statuses << ( auth.save ? "OK" : post.errors )
end if params[:posts_list]

  render json: statuses
end   


def select_permited(post_params)
  post_params.permit(:post_title, :post_body, :user_id)
end

Routes:

resources :posts do
  post 'mass_create', on: :collection
end
elmuzyk
  • 93
  • 12
0

Your error results from calling params.require(:post). As your posts is an array, and not a regular parameters hash, Rails won't allow to call permit on it. You should go for

def post_params
  params.permit(post: [:post_title, :post_body, :user_id])
end
xlts
  • 136
  • 5
  • When set params like that I have an error **unknown attribute 'post'**. Should I change my create action? – elmuzyk May 15 '17 at 06:31