[rails] - Сохранение объекта в БД
От: DemAS http://demas.me
Дата: 03.08.10 07:05
Оценка:
Вроде все по инструкции делаю, но что-то не работает.

Модель — запрос, который мы сохраняем в базе данных:

class Scr < ActiveRecord::Base
  def self.find_scrs
    find(:all, :order => "id")
  end
end


Представление в котором мы вводим новый запрос:

<div class="scr-form">
  <fieldset>
  
    <% form_for :scr, :url => { :action => :save_scr } do |form| %>
    <p> 
      <label for="scr_id">Id:</label>
      <%= form.text_field :id, :size => 40 %>
    </p>
    <p> 
      <label for="scr_description">Description:</label>
      <%= form.text_area :description, :rows => 3, :cols => 40 %>
    </p>

    <%= submit_tag "Save", :class => "submit" %>
    <% end %>

  </fieldset>
</div>


Контроллер:

class InboxController < ApplicationController
  def index
    @scrs = Scr.find_scrs
  end

  def add_scr
  end  
  
  def save_scr
    @src = Scr.new(params[:scr])
    puts "=============================="
    @a = params[:scr]
    puts @a
    puts "=============================="
    @scr.save
  end
end


Заполняем поля на форме, жмем клавишу Save и получаем ошибку:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.save


/home/demas/artefacts/dev/study/ruby/rails/tracker/app/controllers/inbox_controller.rb:15:in `save_scr'
/usr/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `perform_action'
/usr/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in `call_filters'
...


Request

Parameters:

{"authenticity_token"=>"m/QBN85+5Kj+Qewtl29mBl5kBtSBr1+Ixv4jxTq6Rfk=",
"scr"=>{"id"=>"as",
"description"=>"sa"},
"commit"=>"Save"}


В консольном окошке:

Processing InboxController#add_scr (for 127.0.0.1 at 2010-08-03 08:42:50) [POST]
Parameters: {"authenticity_token"=>"m/QBN85+5Kj+Qewtl29mBl5kBtSBr1+Ixv4jxTq6Rfk="}
Rendering inbox/add_scr
Completed in 18ms (View: 16, DB: 0) | 200 OK [http://0.0.0.0/inbox/add_scr]
==============================
{"id"=>"as", "description"=>"sa"}
==============================


Processing InboxController#save_scr (for 127.0.0.1 at 2010-08-03 08:42:53) [POST]
Parameters: {"authenticity_token"=>"m/QBN85+5Kj+Qewtl29mBl5kBtSBr1+Ixv4jxTq6Rfk=", "scr"=>{"id"=>"as", "description"=>"sa"}, "commit"=>"Save"}
WARNING: Can't mass-assign these protected attributes: id

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.save):
app/controllers/inbox_controller.rb:15:in `save_scr'
<internal:prelude>:8:in `synchronize'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'


Как я понимаю, почему то не создается экземпляр Scr, несмотря на то, что params вроде как корректно заполнен. Почему?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.