2

I'm doing a Rails project for class where a user can enter a search term, the results are displayed (images), then the user can click on an image to get a larger version of it.

On the larger version page, I have a button_to :back.

<%= button_to "Back", :back %>

But when I hit the button, the image results page it returns to shows the default images, not the ones returned from the search. I need to be able to pass params[:search_term] back to the previous page (as without a :search_term value, the default images are returned).

I tried this:

<%= button_to "Back", :back, :search_term => params[:search_term] %>

And when viewing the page source, it looks like it should pass the parameter back:

   <form action="http://localhost:3000/albums/7/add" class="button_to" method="post">
   <div>
   <input search_term="puppy" type="submit" value="Back" />
   <input name="authenticity_token" type="hidden" value="lkjsakvgjwhatever" />
   </div></form>

The authenticity token gets returned back, but my search_term doesn't.

EDIT:

Here's the view:

<%= image_tag("http://farm#{@pic["farm"]}.staticflickr.com/#{@pic["server"]}/#{@pic["id"]}_#{@pic["secret"]}_c.jpg") %>
<br><br>
<%= @pic["title"] %>'

<br><br>
<br><br>

<%= button_to "Back", :back, {:search_term=> params[:search_term]} %>
wolf2600
  • 565
  • 2
  • 8
  • 17
  • Look http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to, I have not used button_to before, but look the examples, and try using a path, something like back_path(params[:search_term]) and add the route with the match – pablomarti Dec 08 '12 at 23:20
  • Also, in the options, you can pass something like {:data => :search_term=> params[:search_term]} for additional attributes – pablomarti Dec 08 '12 at 23:23

3 Answers3

3

Use the params option to have a hash of values added as hidden fields:

= button_to "Back", :back, params: { search_term: params[:search_term] }

Will produce:

<form action="/back" class="button_to" method="post">
    <div>
        <input type="submit" value="Back" />
        <input name="authenticity_token" type="hidden" value="uSCQbSaZdbIrTD2Z2FA0FQNe5CF+tK/jViInc1b7Kb0=" />
        <input name="search_term" type="hidden" value="whatever" />
    </div>
</form>
Matt Gibson
  • 14,616
  • 7
  • 47
  • 79
1

You have to change the action of the form. Something like this

<%= form_for(@model, :url => 'YOURURL',  :method => 'get') do %>
Jean
  • 5,201
  • 11
  • 51
  • 87
  • There's no way to simply add parameters to the button_to? – wolf2600 Dec 08 '12 at 22:53
  • could paste the view, please – Jean Dec 08 '12 at 22:58
  • And when viewing the single picture, these are the params: {"pic"=>{"farm"=>"9", "id"=>"8254283877", "isfamily"=>"0", "isfriend"=>"0", "ispublic"=>"1", "owner"=>"26742588@N04", "secret"=>"2e1e383879", "server"=>"8211", "title"=>"OH HEY!"}, "search_term"=>"puppy", "action"=>"single", "controller"=>"public_pages"} – wolf2600 Dec 08 '12 at 23:07
  • So I just want to take the params[:search_term] and pass it back when the Back button is pressed. – wolf2600 Dec 08 '12 at 23:08
  • I'm sorry man, I thinks you have to create a link_to something like this <%= link_to " Back ", :back %> and style the link like a button because I'm no sure if you can do this without javascript – Jean Dec 08 '12 at 23:14
1

As I knew, button_to "Back", :back would be as a javascript code like: javascript:history.back(), so you should define the back url yourself instead of using default :back of UrlHelper

An Nguyen
  • 126
  • 8