0

我有两个模型,我正在尝试整合它,我可以带来另一个模型,问题是提交时,id 被更新为 null

轨道模型:

class Orbituarysite < ActiveRecord::Base
attr_accessible :birth_place, :death_place, :dob, :dod, :name, :living_place, :orbiturerimage, :salutation, :user_id
belongs_to :user
mount_uploader :orbiturerimage, OrbiturerUploader
has_one :notice_display
end

通知型号

class NoticeDisplay < ActiveRecord::Base
  attr_accessible :message, :notice_type, :orbituarysite_id, :posted_by, :notice_event_places_attributes, :notice_event_contacts_attributes
  belongs_to :orbituarysite

end

轨道控制器:

def show
@orbituarysite = Orbituarysite.find(params[:id])
if @orbituarysite.notice_display.nil?
  @notice_display = @orbituarysite.build_notice_display
end
respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @orbituarysite }
end

结尾

在轨道站点显示视图:

      <% if @orbituarysite.notice_display.nil?  %>
        <%= render 'notice_displays/form' , :remote => true %>
      <% end %>

当我提交表单时,我遇到了以下问题,即提交后我将其重定向到轨道站点页面,所以我在服务器中得到了这个,

Started GET "/orbituarysites/1" for 127.0.0.1 at 2013-09-05 08:58:45 +0530
Processing by OrbituarysitesController#show as HTML
Parameters: {"id"=>"1"}
User Load (1.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Orbituarysite Load (1.8ms)  SELECT "orbituarysites".* FROM "orbituarysites" WHERE  "orbituarysites"."id" = $1 LIMIT 1  [["id", "1"]]
NoticeDisplay Load (0.8ms)  SELECT "notice_displays".* FROM "notice_displays" WHERE "notice_displays"."orbituarysite_id" = 1 LIMIT 1
History Load (0.9ms)  SELECT "histories".* FROM "histories" WHERE "histories"."orbituarysite_id" = 1 LIMIT 1
(0.5ms)  BEGIN
(0.9ms)  UPDATE "histories" SET "orbituarysite_id" = NULL, "updated_at" = '2013-09-05 03:28:46.214455' WHERE "histories"."id" = 2
(25.0ms)  COMMIT
Rendered histories/_form.html.erb (125.2ms)
Rendered memories/_form.html.erb (9.1ms)
Rendered condolences/_form.html.erb (14.4ms)
Rendered orbiturer_share_images/_form.html.erb (6.2ms)
Rendered orbituarysites/show.html.erb within layouts/application (162.6ms)
Rendered orbituarysites/_form.html.erb (26.4ms)
Completed 200 OK in 944ms (Views: 504.5ms | ActiveRecord: 64.5ms)

如何改变这一点,因为在

     <% if @orbituarysite.notice_display.nil?  %>

但是在控制台中,当它为 nil 时我得到 true,当有内容时我得到 false

请帮我解决这个问题

4

1 回答 1

0

。零?并不总是保证真实性。使用@orbituarysite.notice_display.present?

本质上,所有 nil 都返回 true,因为在 ruby​​ 中,除了 false 和 nil 之外,所有事物都是真实的。因此,您的代码将始终返回 TRUE,因为它将返回 nil。:-)

于 2013-09-05T03:45:06.507 回答