Rails beginner, initialize object
In rails default controller the new method makes an object, and the create method is used later to save that.
I want to set a mod_user field in the DB, without it being input into the form.
Based on this link http://api.rubyonrails.org/classes/ActiveRecord/Base.html I've tried adding the following to my pages controller.
def new
@page = Page.new(:n_publisher_id => session[:n_publisher_id])
or
def create
page = Page.new(params[:page])
page.n_publisher_id = session[:n_publisher_id]
But it is saving as NULL If I put this in the controller and model then I get nil object errors from ActiveRecord
def new
@page = Page.new(1)
def initialize(n_publisher)
@n_publisher_id = n_publisher
end
I have attr_accessor :n_publisher_id included in my page model. This works in the console...
>> @i = Page.new
=> #<Page id: nil, fk_issue: nil, n_status_id: nil, dt_published_datetime: nil, dt_offline_date: nil, dt_created_date: nil, n_publisher_id: nil, created_at: nil, updated_at: nil, page_name: nil>
>> @i.n_publisher_id
=> nil
>> @i.n_publisher_id = 1
=> 1
>> @i.n_publisher_id
=> 1
Here is schema of the pages table
mysql> show fields from pages;
+-----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fk_issue | int(11) | YES | | NULL | |
| n_status_id | int(11) | YES | | NULL | |
| dt_published_datetime | datetime | YES | | NULL | |
| dt_offline_date | datetime | YES | | NULL | |
| dt_created_date | date | YES | | NULL | |
| n_publisher_id | int(11) | YES | | NULL | |
| created_at | datetime 开发者_JAVA技巧 | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| page_name | varchar(255) | YES | | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
Here is the model
class Page < ActiveRecord::Base
has_many :slots, :dependent => :destroy
accepts_nested_attributes_for :slots
#attr_accessor :n_publisher_id
#attr_accessible :n_publisher_id
end
Create Action
def create
page = Page.new(params[:page].merge({:n_publisher_id => 1}))
#page.dt_created_date = Date.today
page.n_publisher_id = 1
respond_to do |format|
if page.save
format.html { redirect_to(page, :notice => 'Page was successfully created.') }
format.xml { render :xml => page, :status => :created, :location => page }
else
format.html { render :action => "new" }
format.xml { render :xml => page.errors, :status => :unprocessable_entity }
end
end
end
You should never overwrite the initialize method of your ActiveRecord object. Rails is doing a bunch of stuff behind the scenes and it's known to mess things up.
Instead just append your attribute onto your initial params that you're passing in.
So, assuming :n_publisher_id is a real attribute of your AR object (column in the table), something like:
@page = Page.new(params[:page].merge({:n_publisher_id => session[:n_publisher_id]})
should work.
This also assumes that session[:n_publisher_id] is also not nil (otherwise, of course it will be saved as nil in the db)
You could also call super in your initialization method.
def initialize
@something = false
@value_you_set = 0
super() # NOTE: This *must* be called
end
Remove attr_accessor :n_publisher_id
from your model. It is column in db, so Rails took care for it. Maybe your attr_accessor
overrides something.
Did you verify already that session[:n_publisher_id] has a value != nil?
A quick verification in your controller, try:
def create
page = Page.new(params[:page])
page.n_publisher_id = 3
end
If all the newly created pages have n_publisher_id == 3, then session[:n_publisher_id] == nil and must be set somewhere in your app.
It sounds like session[:n_publisher_id]
is nil. Or you are overwriting it somewhere.
If you have the value available in the create action, merge in the params there and you don't need to bother setting it in the new action.
If you only have it available in the new action for some reason, use a hidden field
My gut tells me that you should be setting this explicitly and actually not allowing mass assignment, but I don't know your use case.
精彩评论