使用自定义路由更新数据库

时间:2010-02-11 21:33:15

标签: ruby-on-rails

我有自定义路线:

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

我的Request控制器中有两个方法用于此目的:

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操作。有什么建议?感谢。

1 个答案:

答案 0 :(得分:0)

简单,只需检查请求方法:

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