Redirecting to create new page with input

Rails 3.1.3

I believe this is a very very fundamental question, but since I am new to Rails, it's more productive to ask someone to point out what I need to do.

Say, I have a search method that generates a list of youtube videos. I have completed this part. And also, I have set up a database, Video, whose fields are video_title and URL.

I need to place a button to each of these video outcomes. My question is :

when clicking the button, how can I automatically send the URL of the corresponding video and simultaneously redirect to NEW page where users can create a new video entry??? In other words, in the creating new page, the URL (and hopefully the video_title) is to be already filled out.

I am thinking that I need to set "routes.rb" so that redirecting can take some values together. But routing is very confusing to me so far.

Could anyone help me out?

soichi

Let me see if I understand correctly what you want. When you say "send the URL" you don't mean that the user should be sent to that page (whether in the same tab or window, or a new one), but simply that that should be the pre-filled value on the "New Video" page. Right?

There may be a more "Railsy" way to do this, but what comes to my mind is:

Have the links link to #new, but add a parameter called something like video_url. You could make it a URL parameter, so that rather than going to myvideostore.com/videos/new, you'd go to myvideostore.com/videos/new?video_url=YouTube or whatever. (Actually the video_url should be url-encoded, but I think Rails will handle that for you.) To do that, I think you can pass a hash of params to link_to, or IWCTW you can tack it on manually.

Or you can make them form params, so they don't show up and look ugly on the URL after the user clicks. Make a tiny form for each one. Have the URL as a hidden field. Have the link submit the form, to the videos_controller#new.

Either way, in the controller, after you call Video.new, set the object's url to params[:video_url]. Then when you display it, the value should show up just fine.

Let us know how that works for you.

-Dave

Thanks for your explanation. It is exactly what I wanted.

in search.html.erb, I added

  <%= link_to('RegisterThisMovie', {:controller => 'video', :action => 'new', :url => y }) %>

'y' contains the url of the movie after executing the search.

in video_controller.rb, I added

  def new     @video = Video.new     @video.url = params[:url] #<---HERE     respond_to do |format|       format.html # new.html.erb       format.json { render json: @video }     end   end

then it worked fine! Thanks for your help.

soichi

Thanks for your explanation. It is exactly what I wanted.

Great! Now let's take it to the next level. I think you can make it a little more "canonical". Instead of:

<%= link_to('RegisterThisMovie', {:controller => 'video', :action => 'new', :url => y }) %>

'y' contains the url of the movie after executing the search.

try:

<%= link_to( 'Register This Movie', new_video_path, :url => y ) %>

Also you can try getting used to the newer hash syntax, and do:

<%= link_to( 'Register This Movie', new_video_path, url: y ) %>

I'm not 100% sure off the top of my head that these will work, but it should be at least close. :*) The built-in url helpers like new_whatever_path, edit_whatever_path(some_object), etc. will save you lots of typing and thus errors.

-Dave

Thanks again!