database updates using custom routes
I have a custom route:
map.resources :user_requests, :member => {:confirm => :any}
I have two methods in my Request controller for this purpose:
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
And the view confirm.erb:
<% form_for(:user_request, :url => {:action => :confirm}) do %>
Request: <%= select_tag(:id, options_from_collection_for_select(@requests, :id, :request_status_id)) %&g开发者_如何学编程t; <br />
Password: <%= password_field_tag :password %> <br />
<%= submit_tag 'Confirm Request' %>
<% end %>
The updating occurs in the update_confirm action, so my question is after user picks an option from confirm action, when they click to submit the form, I would like to trigger the update_confirm action from the confirm page. Any suggestions? Thanks.
Easy, just check for request method:
def confirm
if request.post?
update_confirm
end
# ...
end
精彩评论