诊断 Rails 应用程序在元素更新时挂起

发布于 2024-07-24 23:50:15 字数 6413 浏览 2 评论 0原文

在构建 Rails 应用程序来帮助小型酒店管理预订的过程中,我遇到了一个奇怪的应用程序挂起问题。 建立预订工作正常; 添加客人、将他们分配到房间等,没有问题。 然而,当商人去“确认”询问时,Mongrel却把我晾在一边。

预订的索引页面列出了当前查询和已确认的预订:

# reservations/index.html.erb

<% unless @reservations.empty? %>
<h2>Inquiries</h2><hr/><br/>
<%= render :partial => 'reservation', :collection => @reservations.reject { |r| r.confirmed } %>
<h2>Confirmed Reservations</h2><hr/><br/>
<%= render :partial => 'reservation', :collection => @reservations.reject { |r| not r.confirmed }  %>
<% else %>
<%= link_to 'Search for rooms', :action => 'index', :controller => 'search' %>
<% end %>

在预订部分本身中,调用确认/取消确认逻辑的位只是到控制器操作的直接链接:

# _reservation.html.erb

(<% unless reservation.confirmed? %>
    <%= link_to 'Confirm Reservation', :action => 'confirm', :id => reservation.id %>
<% else %>
    <%= link_to 'Unconfirm Reservation', :action => 'unconfirm', :id => reservation.id %>
<% end %>)

确认预订的控制器操作,映射到 PUT在我的路线中,看起来像这样:

def confirm
  puts "\n\n\nConfirming reservation #{params[:id]}..."
  @reservation = Reservation.find(params[:id])
  puts "Found reservation! Confirming..."
  @reservation[:confirmed] = true
  puts "Confirmed, saving..."
  respond_to do |wants|            
    if @reservation.save
      flash[:notice] = 'Reservation has been confirmed.'
      wants.html { redirect_to :action => 'index' }
    else
      wants.html { render :action => 'index' }
    end
  end    
end

“@reservation.save”就是诀窍。 每次肯定应用程序挂起。

有人可以帮助我了解这里发生了什么吗?

/更新:

通过使用控制台,我能够以另一种方式演示该问题:

?> r = Reservation.find(36)
=> #<Reservation id: 36, customer_name: "buyer", customer_email: "[email protected]", begin: "2009-06-22 00:00:00", end: "2009-06-26 00:00:00", request_timestamp: nil, notes: "Thanks!", created_at: "2009-06-13 20:36:50", updated_at: "2009-06-13 20:36:50", yacht_id: 7, adults: 1, children: 0, buyer_id: 3, confirmed: nil>
>> r.confirmed = true
=> true
>> r
=> #<Reservation id: 36, customer_name: "buyer", customer_email: "[email protected]", begin: "2009-06-22 00:00:00", end: "2009-06-26 00:00:00", request_timestamp: nil, notes: "Thanks!", created_at: "2009-06-13 20:36:50", updated_at: "2009-06-13 20:36:50", yacht_id: 7, adults: 1, children: 0, buyer_id: 3, confirmed: true>
>> r.save!
IRB::Abort: abort then interrupt!!
    from C:/Ruby/lib/ruby/1.8/irb.rb:81:in `irb_abort'
    from C:/Ruby/lib/ruby/1.8/irb.rb:247:in `signal_handle'
    from C:/Ruby/lib/ruby/1.8/irb.rb:66:in `start'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:275:in `call'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:275:in `run_callbacks'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:344:in `callback'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:318:in `valid?'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:221:in `send'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:221:in `method_missing'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:906:in `validates_associated'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:906:in `collect'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:906:in `validates_associated'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:399:in `validates_each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:396:in `each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:396:in `validates_each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:182:in `call'
 ... 2204 levels...
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `run'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `send'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `run'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:276:in `run_callbacks'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:1029:in `valid_without_callbacks?'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:315:in `valid?'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:1018:in `save_without_dirty!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/dirty.rb:87:in `save_without_transactions!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:200:in `save!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:182:in `transaction'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:200:in `save!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:200:in `save!'
    from (irb):5>>

请注意,控制台也被锁定,我不得不中止保存! 使用 Ctrl-C 命令。

到底为什么要拯救! 正在做这个?

/更新:

明白了!!! 它在我的模型中。 我试图验证一个关联,但我已经得到了 has_many/belongs_to 的东西。

感谢大家!

In the course of building a rails application to help small hotels' manage bookings, I'm encountering a strange application hang. Building the reservation works fine; adding guests, assigning them to rooms, etc, without issue. However, when the merchant goes to 'confirm' an inquiry, Mongrel hangs me out to dry.

The index page for reservations lists the current inquiries and confirmed bookings:

# reservations/index.html.erb

<% unless @reservations.empty? %>
<h2>Inquiries</h2><hr/><br/>
<%= render :partial => 'reservation', :collection => @reservations.reject { |r| r.confirmed } %>
<h2>Confirmed Reservations</h2><hr/><br/>
<%= render :partial => 'reservation', :collection => @reservations.reject { |r| not r.confirmed }  %>
<% else %>
<%= link_to 'Search for rooms', :action => 'index', :controller => 'search' %>
<% end %>

In the reservation partial itself, the bits which invoke the confirm/unconfirm logic are simply direct links to controller actions:

# _reservation.html.erb

(<% unless reservation.confirmed? %>
    <%= link_to 'Confirm Reservation', :action => 'confirm', :id => reservation.id %>
<% else %>
    <%= link_to 'Unconfirm Reservation', :action => 'unconfirm', :id => reservation.id %>
<% end %>)

The controller action to confirm a reservation, which is mapped to a PUT in my routes, looks like this:

def confirm
  puts "\n\n\nConfirming reservation #{params[:id]}..."
  @reservation = Reservation.find(params[:id])
  puts "Found reservation! Confirming..."
  @reservation[:confirmed] = true
  puts "Confirmed, saving..."
  respond_to do |wants|            
    if @reservation.save
      flash[:notice] = 'Reservation has been confirmed.'
      wants.html { redirect_to :action => 'index' }
    else
      wants.html { render :action => 'index' }
    end
  end    
end

'@reservation.save' is what does the trick. Definite app hang every time.

Can someone help me understand just what is going on here?

/UPDATE:

From playing around with the console I was able to demonstrate the issue in another way:

?> r = Reservation.find(36)
=> #<Reservation id: 36, customer_name: "buyer", customer_email: "[email protected]", begin: "2009-06-22 00:00:00", end: "2009-06-26 00:00:00", request_timestamp: nil, notes: "Thanks!", created_at: "2009-06-13 20:36:50", updated_at: "2009-06-13 20:36:50", yacht_id: 7, adults: 1, children: 0, buyer_id: 3, confirmed: nil>
>> r.confirmed = true
=> true
>> r
=> #<Reservation id: 36, customer_name: "buyer", customer_email: "[email protected]", begin: "2009-06-22 00:00:00", end: "2009-06-26 00:00:00", request_timestamp: nil, notes: "Thanks!", created_at: "2009-06-13 20:36:50", updated_at: "2009-06-13 20:36:50", yacht_id: 7, adults: 1, children: 0, buyer_id: 3, confirmed: true>
>> r.save!
IRB::Abort: abort then interrupt!!
    from C:/Ruby/lib/ruby/1.8/irb.rb:81:in `irb_abort'
    from C:/Ruby/lib/ruby/1.8/irb.rb:247:in `signal_handle'
    from C:/Ruby/lib/ruby/1.8/irb.rb:66:in `start'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:275:in `call'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:275:in `run_callbacks'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:344:in `callback'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:318:in `valid?'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:221:in `send'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:221:in `method_missing'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:906:in `validates_associated'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:906:in `collect'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:906:in `validates_associated'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:399:in `validates_each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:396:in `each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:396:in `validates_each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:182:in `call'
 ... 2204 levels...
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `run'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `each'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `send'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:90:in `run'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:276:in `run_callbacks'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:1029:in `valid_without_callbacks?'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/callbacks.rb:315:in `valid?'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:1018:in `save_without_dirty!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/dirty.rb:87:in `save_without_transactions!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:200:in `save!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:182:in `transaction'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:200:in `save!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:200:in `save!'
    from (irb):5>>

Note that the console locked up as well, and I had to abort the save! command using Ctrl-C.

Why in the world would the save! be doing this?

/UPDATE:

Got it!!! It was in my model. I was trying to validate an association and I had gotten the has_many/belongs_to stuff backwards.

Thanks, everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2024-07-31 23:50:15

您的问题可能是这样的:

@reservation[:confirmed] = true

您直接更新属性哈希,这有点奇怪。 通常,您只需更新属性即可。

@reservation.confirmed = true

之前当我弄乱属性哈希时,我遇到过奇怪的问题。 尤其是布尔值 - 也许是因为发生了某种类型的强制。

Your issue may be this:

@reservation[:confirmed] = true

You're directly updating the attributes hash, which is a little odd. Normally, you'd just update the attribute.

@reservation.confirmed = true

I've had odd issues before when I messed with the attribute hash. Booleans especially - maybe because there is some type coercion happening.

国产ˉ祖宗 2024-07-31 23:50:15

使用 thin而不是杂种。 从您的日志中发布更多详细信息。 它通常不会像这样挂起来。

Use thin instead of mongrel. Post more details from your log. It doesn't just hang like that usually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文