1

UPDATE

I answered my question below, but I am still asking for a prettier way to achieve my goal. I have a feeling my controller knows too much about how to do things.


I have a create action in my VideoController :

def create
  method = 'get_' + params[:video][:provider] + '_video_id'
  provider_video_id = Video.send(method, params[:video][:url])
  thumb = Video.get_thumb_from_youtube(provider_video_id)

  @video = Video.new(params[:video], :provider_video_id => provider_video_id, :thumb => thumb, :views => 0, :likes => 0)
  if @video.save!
    redirect_to video_path('1'), notice:'Video added successfully.'
  else
    render :new
  end
end

I call Video.new with params[:video] with get its information from a form the user fills in. I then manipulate the URL the user passed in with the form to recover the video_provider_idand thethumb.

However, Rails is not saving the video with provider_video_idand thumb... I get this error on save :

Validation failed: Thumb can't be blank, Thumb is invalid, Provider video can't be blank

My guess is the newmethod doesn't accept extra parameters...

4 Answers 4

1

How about a before save callback in your model? This is an off-the-top-of-my-head default value example, but you could pass params in too.

after_initialize :url


private
def url
  self.provider_video_id ||= "default value"
end
Sign up to request clarification or add additional context in comments.

1 Comment

Your solution seems good, but I'm not sure I understand it well. I need to call a method to get provider_video_id, so could I write : self.provider_video_id = get_provider_video_id(params[:video][:url]?
1

You may want to use .merge():

Instead of

method = 'get_' + params[:video][:provider] + '_video_id'
params[:video][:provider_video_id] = Video.send(method, params[:video][:url])
params[:video][:thumb] = Video.get_thumb_from_youtube(params[:video][:provider_video_id])
params[:video][:views] = params[:video][:likes] = 0    

@video = Video.new(params[:video])

You can do (modifying code from the original question):

@video = Video.new(params[:video].merge(:provider_video_id => provider_video_id, :thumb => thumb, :views => 0, :likes => 0))

That will merge the params into one hash.

Comments

1

But if you want, you can always override the default new action with something like

class Video 

   def new(attr1, attr2)
     super
     #do something with attr2
   end

end

1 Comment

Nice solution, but it is not exactly what I am looking for. Thanks a lot!
0

I solved the problem. I think Ruby on Rails' newmethod doesn't accept more than one parameter, which usually is params. What I did is I the following :

method = 'get_' + params[:video][:provider] + '_video_id'
params[:video][:provider_video_id] = Video.send(method, params[:video][:url])
params[:video][:thumb] = Video.get_thumb_from_youtube(params[:video][:provider_video_id])
params[:video][:views] = params[:video][:likes] = 0    

@video = Video.new(params[:video])

Now, it seems to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.