views:

216

answers:

1

I have a portion of my app that sometimes will need to inform the user of their action, but the app does not know of the requirement to inform the user until after the form has been submitted (which eliminates Javascript).

Basically what happens is that the user uploads one or more files containing records, if the number of records within the files is greater than X, then I have to determine a sample size and only use that number of the articles, the articles that get selected are randomly chosen. The problem I have is that it is possible for the user to upload files on the create action, as well as the edit action. So previously a user could have uploaded 2 files that never reached a record count greater than X, and on a recent edit they did - and now we need to create a sample set.

This isn't a big issue, but because they have created articles in the past, we now need to delete SOME of the records from the previous files, because we are creating a sample set - so I need to inform the user of what is about to happen.

So, once the user clicks submit, I determine the number of records in the file, and if its greater than X I need to show a message and the user has to click continue or cancel (Ideally I could just render a view here so that I can provide nice, styled information to the user). If its less than X then I don't care and just continue.

+2  A: 

You should be able to check the number of files after the create happens, and redirect if it's over a X amount. I'll assume you know what the current_user is and that you're in the FileController uploading files to the current user's account.

def create
  @file = current_user.files.create(params[:file])
  if current_user.files.count >= X
    redirect_to :controller => "files", :action => "too_many_files"
  else
    redirect_to file_path(@file)
  end
end
Sam C
I think this may work.. my only concern is that I'm using the accepts_nested_attributes_for here so its creating the files as part of a nested element - let me work with this and I'll let you know. Keep me posted if you think of something else as well, thank you for the input!
Rabbott
"Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return"."
Rabbott
What version of Rails? Rewrote in the example
Sam C
I ended up creating a solution much like, and just sending the user to another action in the same controller, thanks for your help!
Rabbott