Search code examples
ruby-on-railsformtasticstrong-parameters

How do I permit date selector attributes in Rails and Formtastic


Latest

I have switched to Simple form. No problem with permitting date attributes there.

Recent attempts

I have put a demo repository on Github illustrating the problem:

This one uses formtastic and displays my problem with:

Unpermitted parameters: date_of_receipt(1i), date_of_receipt(2i), date_of_receipt(3i), date_of_receipt(i)

https://github.com/bigos/permit_date_selector/commit/9f142b79c51e71dca35c988125a2912b83b91972

This one doesn't use formtastic and works fine;

https://github.com/bigos/permit_date_selector/commit/4c53b934ac5cd3f04241bf462e7b677ef5d28335

Initial post

When I try to submit my form I get this message

Unpermitted parameters: date_of_receipt(i)

I have :date_of_receipt in the list of permitted parameters.

My form input selecting the date looks as follows:

<%= f.input :date_of_receipt, as: :date_select %>

Should I give up on formtastic and go back to standard forms?


Solution

  • I've created a fresh Rails app (using Rails 4.1.5 and Formtastic 2.3.1) to try to replicate, and I can't, so I'm closing. Here's what I had:

    # Migration
    class CreatePosts < ActiveRecord::Migration
      def change
        create_table :posts do |t|
          t.string :title
          t.string :body
          t.datetime :published_at
    
          t.timestamps
        end
      end
    end
    
    # Model
    class Post < ActiveRecord::Base
    end
    
    # Controller
    class PostsController < ApplicationController
     def new
        @post = Post.new
      end
    
      def create
        @post = Post.new(post_params)
        if @post.save
          redirect_to @post
        else
          render :new
        end
      end
    
      def show
        @post = Post.find(params[:id])
      end
    
      protected
    
      def post_params
        params[:post].permit(:title, :body, :published_at)
      end
    end
    
    # View
    <%= semantic_form_for @post do |f| %>
      <%= f.inputs do %>
        <%= f.input :title %>
        <%= f.input :body %>
        <%= f.input :published_at %>
      <% end %>
      <%= f.actions do %>
        <%= f.action :submit %>
      <% end %>
    <% end %>
    

    By simply permitting :published_at, I was able to successfully save a Post into the database with the time I had selected. Here's the development.log:

    Started POST "/posts" for 127.0.0.1 at 2014-09-06 21:13:37 +1000
    Processing by PostsController#create as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"Jv4Pd7aNgvjkCtzrX+gHNeCNfX3L8t6IpEOEAWzdeIo=",     "post"=>{"title"=>"sdfgs", "body"=>"sdgfdfg", "published_at(1i)"=>"2019", "published_at(2i)"=>"1",     "published_at(3i)"=>"1", "published_at(4i)"=>"00", "published_at(5i)"=>"01"}, "commit"=>"Create Post"}
       (0.1ms)  begin transaction
      SQL (0.2ms)  INSERT INTO "posts" ("body", "created_at", "published_at", "title", "updated_at") VALUES (?, ?, ?,     ?, ?)  [["body", "sdgfdfg"], ["created_at", "2014-09-06 11:13:37.685160"], ["published_at", "2019-01-01 00:01:00    .000000"], ["title", "sdfgs"], ["updated_at", "2014-09-06 11:13:37.685160"]]
       (8.8ms)  commit transaction
    Redirected to http://localhost:3000/posts/3
    Completed 302 Found in 12ms (ActiveRecord: 9.1ms)
    

    There's no extra trickery required, this is how you do it :)