0

There are similar questions, but after 18h of reading them and several guides trying and trying I still haven't been able to solve my problem. That's why I'm posting this question. Thanks in advance!

Trying to POST data to create a new record but get "param is missing or the value is empty: todo"

FORM

<div class="addactionform">
  <%= form_for(Action.new, :url => { action: "create" }) do |todo| %>
    <div class="input-field"> <%= todo.text_field :title %> </div>
    <div class="hidden"> <%= todo.submit %> </div>
  <% end %>
</div>

ROUTE

root 'actions#inbox'
get '/actions' => 'actions#inbox'
post '/actions/create' => 'actions#create', as: :create

CONTROLLER

class ActionsController < ApplicationController

def inbox
    @todos = Action.all
end

def new
   @todo = Action.new
end

def create
    @todo = Action.new(todo_params)
    @todo.save    
    redirect_to(:action => 'inbox')
end

private
    def todo_params
       params.require(:todo).permit(:title)
    end
end

ACTION MODEL

class Action < ActiveRecord::Base
  has_many    :tag, through: :action_tags
  belongs_to  :folder
  belongs_to  :user
  belongs_to  :state
end

MIGRATION

class CreateActions < ActiveRecord::Migration
  def change
    create_table :actions do |t|
      t.string :title
      t.text :note
      t.references :tags
      t.integer :assignee_id
      t.references :state
      t.datetime :due_at
      t.integer :parent_id
      t.references :folder
      t.timestamps
    end
  end
end
sinkesh
  • 45
  • 6

3 Answers3

1

Thanks for all the help. I was finally able to solve the issue this way:

VIEW

<%= form_for Action.new, as: :todo do |action| %>
  <div class="input-field"><%= action.text_field :title %></div>
  <div class="hidden"><%= action.submit %></div>
<% end %>

Controller

  def create
   @action = Action.create(action_params)
   redirect_to(:action => 'inbox')
  end

  private
    def action_params
       params.require(:todo).permit(:title)
    end
end

The part where I name the Action.new in VIEW as todo "as: :todo" is because my model is called Action and since Rails by default sends an :action param containing the action, I was returning the name of the action requested as a string.

Part of the solution is thanks to @guilherme-franco in the answer to the following. Rails 4, strong parameters, nested resources, build and undefined method permit

Community
  • 1
  • 1
sinkesh
  • 45
  • 6
0

This line in your form

<%= form_for(Action.new, :url => { action: "create" }) do |todo| %>

should be like this

<%= form_for(@todo, :url => { action: "create" }) do |todo| %>

and your todo_params should be like this

def todo_params
   params.require(:action).permit(:title)
end
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • Tried it your suggestion Pavan, but now receive "First argument in form cannot contain nil or be empty" for <%= form_for(@todo, :url => { action: "create" }) do |todo| %> – sinkesh Jun 21 '15 at 06:28
  • @sinakes What happens you didn't change that. I mean `Action.new` instead of `@todo`? – Pavan Jun 21 '15 at 06:31
  • Now went back to <%= form_for(Action.new, :url => { action: "create" }) do |todo| %> but kept params.require(:action).permit(:title) Results is that I get "undefined method `permit' for "create":String" for params.require(:action).permit(:title) – sinkesh Jun 21 '15 at 07:45
  • @sinakes try giving it like this `<%= form_for(Action.new) do |todo| %>` – Pavan Jun 21 '15 at 07:49
  • Now get "No route matches [POST] "/actions"" (see my routes in my initial question. Because I got that I also tried to add a new route "post '/actions' => 'actions#create'" but again got "undefined method `permit' for "create":String". So I also tried removing "post '/actions/create' => 'actions#create', as: :create" . But the same error message again. – sinkesh Jun 21 '15 at 08:20
0

You have a model named Action so Rails cannot map its name to "todo" parameters. I'm pretty sure that your code renders HTML input tags with name like "action[title]". So params.require(:todo).permit(:title) cannot find "todo" inputs because they are named as "action".

Try to use a symbol in form_for:

<%= form_for(:todo, url: actions_create_path) do |todo| %>

Or just use :action in params.require (like @Pavan said):

params.require(:action).permit(:title)
phts
  • 3,889
  • 1
  • 19
  • 31