开发者

Save object from form to DB

开发者 https://www.devze.com 2023-01-10 12:14 出处:网络
I have a model: class Scr < ActiveRecord::Base def self.find_scrs find(:all, :order => \"id\") end end

I have a model:

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

View (form):

<div class="scr-form">
 <fieldset>
   <% form_for :scr, :url => { :action => :save_scr } do |form| %>
   <p> 
     <label for="scr_id">Id:</label&g开发者_如何学JAVAt;
     <%= 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>

And controller:

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

I fill the fields in the form, press Save button and get error:

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"}

As i see Rails can not create the instance of Scr class. Why?


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

精彩评论

暂无评论...
验证码 换一张
取 消