Carol Brown
Carol Brown

Reputation: 19

Need Help passing an instance variable to another method for insert in ruby on rails 4

I am new to ROR and spent most of the day trying to get this to work. I have tried using the before_filter and I cannot get my object to insert in another method.

The view is index, a file is selected in the view and then the button to validate file is clicked which calls a method 'evaluate_media' in this method, I look up values based on the file path and name selected and I can successfully insert the record with all the values in this method. The problem is I don't want an automatic save. When the 'evaluate_media' method is done it displays back with either a save button or an update button based on if the file submitted already exists in the database. Then the user can choose to save/update the record or now. This button calls the 'save_file' action. The problem is all the information save in 'evaluate_media' action for the file is not accessible to save_file or update_file actions. I believe session variables might be my answer but I could not find any good examples to get it setup correctly and working in my application. Can someone please tell me and show me the proper code to pass the value from 'evaluate_media' action to save_file or update_file actions?

Here is the code where I assign the values for a new record in my evaluaate_media method:

   if @file_exists_flag == 'NEW'
      # Assign Parameter values for new save
      @file_alias_tfile = FileAliasTfile.new( {:src_location => @radio_button_value, :directory => @dir_path_choice, :full_path => @selected_filepath, :full_filename => @filepath, :file_ext => '', 
                                                  :assigned_status => 'Unassigned', :file_status => 'Saved', :alias_code => @file_alias.to_s, :validate_status => @file_status.to_s, :error_msg => @file_msg.to_s, 
                                                  :video_alias_match => @msg_dtl1.to_s, :audio_alias_match => @msg_dtl2.to_s, :video_format => @video_format.to_s, :video_bitrate => @video_bitrate.to_s, 
                                                  :video_width => @video_width.to_s,
                                                  :video_height => @video_height.to_s, :video_framerate => @video_framerate.to_s, :video_aspect_ratio => @video_aspectratio.to_s, :video_scan_type => @video_scantype.to_s,
                                                  :video_scan_order => @video_scanorder.to_s, :video_alias_code => '', :audio_alias_code => '', :bus_prod_initiative_id => 0, :status => 'Active', :start_date => DateTime.now.to_date, 
                                                  :end_date => '', :deleted_b => 0, :created_by => 'admin', :updated_by => 'admin'} )


   end

Then if the user clicks the save, the save_file method is called and here is the code to save the values from evaluate_media into the database:

def save_file
    @file_alias_tfile = FileAliasTfile.create(@file_alias_tfile) 
    @file_alias_tfile.save
end

I figure the update will be the same as the save so I only included one case here. Your help is appreciated. Thank you!

Upvotes: 0

Views: 159

Answers (1)

7stud
7stud

Reputation: 48649

First of all,

def save_file
    @file_alias_tfile = FileAliasTfile.create(@file_alias_tfile) 
    @file_alias_tfile.save
end  

ModelName.create() is equivalent to ModelName.new(....).save. So the second line should be deleted.

You could do this:

if @file_exists_flag == 'NEW'
   session[:my_file] = FileAliasTfile.new( .... )


def save_file
  session[:my_file].save
end  

However, generally it's not recommended to save model objects in the session. Instead, you should save the object in the database; then save the id of the object in the session. I know, "But I don't want to save the object in the database until the user confirms". Have you ever heard of the destroy() method? For instance,

FileAlias.destroy(session[:file_id])  

which deletes the record in the file_aliases table with an id equal to session[:file_id].

When the 'evaluate_media' method is done it displays back with either a save button or an update button based on if the file submitted already exists in the database.

So, create() the record (remember that saves the record), and if the user clicks the save button, do nothing. If the user clicks the update button, then delete the record from the database and delete the id from the session. For example:

   if @file_exists_flag == 'NEW'
     my_file = FileAlias.create( .... )
     session[:file_id] = my_file.id

def save_file
   #render some page
end

def update_file
  my_file = FileAlias.destroy(session[:id])
  session.delete[:file_id]

  #Do something with my_file?
end

Upvotes: 0

Related Questions