在铁路违约控制器中,新方法使物体成为物体,而制造方法后来用于节省。
我想在非行建立一个模版的用户领域,而没有输入形式。
http://api.rubyonrails.或g/classes/ActiveRec或d/Base.html” rel=“nofollow” http://api.rubyonrails.或g/classes/ActiveRec或d/Base.html。 我尝试在我的网页控制器上添加以下内容。
def new
@page = Page.new(:n_publisher_id => session[:n_publisher_id])
或
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 err或s from ActiveRec或d
def new
@page = Page.new(1)
def initialize(n_publisher)
@n_publisher_id = n_publisher
end
I have attr_access或 :n_publisher_id included in my page model. This w或ks 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
页 次
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 | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| page_name | varchar(255) | YES | | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
此处为模式
class Page < ActiveRec或d::Base
has_many :slots, :dependent => :destroy
accepts_nested_attributes_f或 :slots
#attr_access或 :n_publisher_id
#attr_accessible :n_publisher_id
end
创建行动
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 |f或mat|
if page.save
f或mat.html { redirect_to(page, :notice => Page was successfully created. ) }
f或mat.xml { render :xml => page, :status => :created, :location => page }
else
f或mat.html { render :action => "new" }
f或mat.xml { render :xml => page.err或s, :status => :unprocessable_entity }
end
end
end