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
5
  • You are trying to pass array of post and permit will not work on array. this may help you stackoverflow.com/questions/16549382/… Commented May 11, 2017 at 13:26
  • Possible duplicate of how to permit an array with strong parameters Commented May 11, 2017 at 13:28
  • As I've written above, I've tried the solution from this article and didn't work Commented May 11, 2017 at 13:58
  • @elmuzyk Did I understand you right you want to create multiple posts by a time? As I see your Postman query Commented May 11, 2017 at 20:42
  • Exactly. that's what I want to do. Commented May 15, 2017 at 6:41

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

When set params like that I have an error unknown attribute 'post'. Should I change my create action?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.