views:

14

answers:

1

Hey all,

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)) %> <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.

A: 

Easy, just check for request method:

def confirm
  if request.post?
    update_confirm
   end
  # ...
end
Alex Lebedev
Thanks, do you have any idea why after doing that, it gives me: undefined method `created_at=' for #<Enumerable::Enumerator:0x105420f58>
JohnMerlino
1. Don't touch `created_at` or `updated_at`, these attributes are changed automatically by `ActiveRecord`. 2. `@request` is a reserved name at least in some Rails versions, try using different name.
Alex Lebedev
Ok, but it's doing it for all of them, even the ones I made: undefined method `creator_id=' for #<Enumerable::Enumerator:0x105a12100>. creator_id is an attribute of the record they selected.
JohnMerlino
Well, `Enumerable` tells you that there's an array where you expect a record. Trace why it got there, perhaps `@requests.find` returns an array.
Alex Lebedev
I see what you're saying, so is it possible to capture one of the records from the dropdown using (params[:id]) at this point, after they have been filtered by the condition?
JohnMerlino