English 中文(简体)
铁路开始器,初始物体
原标题:Rails beginner, initialize object

在铁路违约控制器中,新方法使物体成为物体,而制造方法后来用于节省。

我想在非行建立一个模版的用户领域,而没有输入形式。

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
问题回答

你们永远不应超越你积极礼仪的原始方法。 铁路在幕后铺设了一只 st子,它为我所知。

相反,只是把你的属性传给你们的首批寄人。

因此,假设:n_publisher_id是贵国的AR物体(表格中的栏目)的真正属性,例如:

@page = Page.new(params[:page].merge({:n_publisher_id => session[:n_publisher_id]}) 

应当工作。

这还假设,本届会议[:n_publisher_id]也不是零(当然,它将在(b)中作为零予以拯救)。

您也可以在初始化方法中称之为超级。

def initialize
  @something = false
  @value_you_set = 0
  super() # NOTE: This *must* be called
end

Remove attr_accessor :n_publisher_id 页: 1 它是 d的一栏,因此,铁路对此进行了照顾。 或许是<代码>attr_accessor。

你们是否已经核实了这届会议的价值!

快速核查你的控制人员,努力:

def create
    page = Page.new(params[:page])
    page.n_publisher_id = 3
end

如果所有新创建的网页都有N_publisher_id=3,那么,会议[:n_publisher_id] = nil,而且必须在您的口中确定。

诸如<代码>[:n_publisher_id]等声音为零。 或者说,你在某个地方对它进行过手。

如果你在创造行动方面拥有价值,就把行动放在那里的布局中,那么你就不必在新的行动中把行动放在一边。

如果你只是出于某种原因在新行动中提供,就使用一个隐蔽的领域。

我的格乌特告诉我,你应该明确确定这一点,实际上不允许进行大规模派任,但我不知道你的使用情况。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签