0

I am trying to create a photo upload function for a website, and there are two ways to upload. One is by specifying a remote url link, another is by uploading from local computer. So this requires me to write two partials. What's an efficient way to create a photo_upload view with two links to the two situations above, using the same controller for photo? I am stuck on how to pass the redirection information to the controller. Ideally I want to set a photocontroller variable called upload_source when I click on one of the links, but I don't know how to use link_to to pass such variable and where would be the best place to define this boolean variable.

Some code examples:

from the view:

  <h2>Upload a Photo: Step 1 of 3</h2>
    <br>
    <h3><strong>Where is your photo?</strong></h3>
    <br>
    <div class="button_border">
      <a href="add_photo_web">On the Web</a>
    </div>
    <div class="button_border">
      <%= link_to "On My Computer", new_photo_path%>
    </div>
  </div>

From photoController (not sure if this is the right thing to do):

  helper_method :get_source, :set_source

  private

  def set_source=(_source)
    @@source = _source
  end  

  def get_source
    @@source
  end 

Helps are greatly appreciated!

Thank you all for very generous help! I am constantly amazed by the efficiency of stackoverflow.

2 Answers 2

2

If I understand you correctly, you could achieve what you want by having links defined like this:

link_to "On my computer", new_photo_path, :source => "local"
link_to "Remote source", new_photo_path, :source => "remote" 

and then in the controller you could have something like:

def new
 if params[:source] == "local"
   render :partial => 'local'
 else
   render :partial => 'remote'
 end
end
Sign up to request clarification or add additional context in comments.

5 Comments

You advanced my understanding of the params table by quite a bit. By the way is there a good place to gain a thorough understanding of what kind of things can be put in the params table?
Think of it as the params hash, and I would start with the api apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
Thanks for the link, it definitely cleared up a few things. I am currently lost about how to use the controller together with :source hash to determine which view to use. Can you give some hint on that?
So here is what I tried: <% if Photo.params[:source] = "computer" %> <%=render "photos/form" %> <% end %> which I put in the photo/new view. But apparently it doesn't work. I basically would like to create two forms, one for the computer source and the other for web, both rendered as a partial under new view.
Oh I think I figured out what to do. I need to pass the params[:source] surrounded by a pair of parentheses after new_photo_path, since the origin view does not belong to the photo class (it belongs to the chaperone class).
1

I know you didn't ask for UX help, but I would consider dropping this first step and simply showing the new photo form with the field you expect to by most commonly used (either a file upload field or a text field for the URL).

Then have a link to switch to the other option ... but have that link just replace the current field via javascript.

Take a look at how Tumblr does photo uploads ... same principle.

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.