2010-02-11 130 views
1

我有一個自定義路線:數據庫更新

map.resources :user_requests, :member => {:confirm => :any} 

我在我的請求控制器兩種方法用於此目的:

def confirm 
x = current_user.contact.contactable 
@requests = UserRequest.find(:all, :conditions => ["location_id = ?", x]) #The results from this display in dropdown list. 
end 

def update_confirm 
UserRequest.transaction do 
@request = @requests.find params[:id] #Here I just want to grab the option they choose from dropdown. 
@request.created_at = Time.now 
if @request.save 
x = @request.student_id 
y = @request.id 
books = Book.find(:all, :conditions => ["book.location_id = ? && book.request_id = ?", x, y]) 
books.each do |book| 
    book.book_state_id = 3 
    book.save 
end 
end 
end 
end 

和視圖confirm.erb:

<% form_for(:user_request, :url => {:action => :confirm}) do %> 
Request: <%= select_tag(:id, options_from_collection_for_select(@requests, :id, :request_status_id)) %> <br /> 
Password: <%= password_field_tag :password %> <br /> 
<%= submit_tag 'Confirm Request' %> 
<% end %> 

更新發生在update_confirm動作中,所以我的問題是在用戶從確認動作中選擇一個選項後,點擊提交表單,我想從確認頁面觸發update_confirm操作。有什麼建議麼?謝謝。

回答

0

非常容易,只要檢查請求的方法:

def confirm 
    if request.post? 
    update_confirm 
    end 
    # ... 
end 
+0

謝謝,你有什麼想法,爲什麼這樣做之後,它給了我:未定義的方法'created_at =」#爲<可枚舉::枚舉:0x105420f58> – JohnMerlino 2010-02-11 21:56:36

+0

1.不要觸摸'created_at'或'updated_at',這些屬性會被'ActiveRecord'自動改變。 2.'@ request'至少在某些Rails版本中是保留名稱,請嘗試使用不同的名稱。 – 2010-02-11 21:57:55

+0

好吧,但它是爲所有人做的,即使是我所做的:未定義的方法'creator_id ='爲#。 creator_id是他們選擇的記錄的屬性。 – JohnMerlino 2010-02-11 22:01:44