0

I created a small form with two fields and in the future intend to expand this form with one or two fields. It turns out that the inclusion of the data in sqlite this not being done, but gives no error.

The application is being developed to be a to-do list.

Can you tell me the possible reasons?

I have a model:

class ToDoList < ActiveRecord::Base
   attr_accessible :is_favorite, :name, :description
   has_many :tasks,  dependent: :destroy
   belongs_to :member
end

and controller:

class ToDoListsController < ApplicationController
    ...
     def new
        @todo_list = ToDoList.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @todo_list }
        end
      end
    ...

 def create
    @todo_list = ToDoList.new(params[:todo_list])

    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render json: @todo_list, status: :created, location: @todo_list }
      else
        format.html { render action: "new" }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end   
   end 

views: new.html.erb

<h2> add new task</h2>

<%= render partial: 'to_do_list' %>

_to_do_list.httml.erb:

<%= form_for(@todo_list = ToDoList.new) do |f| %>
<%#= form_for(@todo_list) do |f| %>
    <% if @todo_list.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@todo_list.errors.count, "error") %> prohibited this todo_list from being saved:</h2>

          <ul>
            <% @todo_list.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>
    <div class="field">
      <%= f.label :description %><br />
      <%= f.text_area :description %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
<% end %>
noob-rails
  • 25
  • 10
  • @DaveNewton, Name, Description, and the like can not be recorded, were never recorded. Only the date and id are saved (default). – noob-rails Aug 01 '14 at 23:43
  • 1
    Then you should show the code that saves the item. – Dave Newton Aug 02 '14 at 00:01
  • If the date and id are saved, you must be doing a save somewhere (not in the code you're showing, but somewhere). You now need to pass the form to the object when you save... something like @todo_item.create(params[:to_do_item]) – SteveTurczyn Aug 02 '14 at 00:01
  • Ya, @SteveTurczyn, code here: `def create @todo_list = ToDoList.new(params[:todo_list]) respond_to do |format| if @todo_list.save format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' } format.json { render json: @todo_list, status: :created, location: @todo_list } else format.html { render action: "new" } format.json { render json: @todo_list.errors, status: :unprocessable_entity } end end end` – noob-rails Aug 02 '14 at 00:09
  • That's helpful. Could we see the "new.hmtl.erb" file as well? Should be under app/views/to_do_list directory. – SteveTurczyn Aug 02 '14 at 00:33
  • Ya. Edited. (:. The project also is in git, if you prefer: https://github.com/larissaamancio/studies – noob-rails Aug 02 '14 at 00:41

2 Answers2

0

It looks like you are trying to mass assign attributes. If you are using Rails 4 attr_accessible is depricated you have to use strong parameters in the controller to mass-assign, see https://stackoverflow.com/a/17371364/3197124 . Or try assigning individual parameters such as todo_list.name individually in your create.

Community
  • 1
  • 1
mrc
  • 101
  • 5
  • yes, but OP hasn't reported a mass assignment error (or any error, for that matter) – SteveTurczyn Aug 02 '14 at 00:31
  • I use rails 3.2. I'll try to add an entry each time to see if records, but this is not a bad practice? – noob-rails Aug 02 '14 at 00:33
  • <%= form_for(@todo_list = ToDoList.new) do |f| %>... this confuses me. You try something like <%= form_for(@todo_list, url: todo_list_path) do|f| %>? or actually probably url: todo_lists_path – mrc Aug 02 '14 at 00:56
0

Thanks, the link to the github was very helpful.

The problem is the params hash that's coming in is called to_do_list NOT todo_list. So you're mapping nothing to the object when you do ToDoList.new(params[:todo_list] ... you should be doing ToDoList.new(params[:to_do_list]).

In fact, since your model class is ToDoList the correct underscore usage is always to_do_list

I suggest you scan everywhere you've used "todo_list" or "@todo_list" and change it to "to_do_list" and "@to_do_list". It's not absolutely necessary, but for consistency, and it's what people expect to see.

I've confirmed that fixes your problem.

Cheers Steve

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53