rails has_one属于表单提交错误

时间:2013-09-05 03:41:45

标签: ruby-on-rails relationship has-one

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

orbituary model:

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
orbituary controller:

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

orbituary网站中的

显示视图:

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

当我提交表格时,我得到以下问题,即aftter提交我将其重定向到orbituary sites页面,所以我在服务器中得到这个,

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

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

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

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