0

I need to be able to click an image (out of a bunch of images) and update a profile table based on the image clicked.

List of images in my view:

 <% @pages_selection.each do |pages_selection| %>
    <img> 
      <%= link_to image_tag(pages_selection.page_picture, '#') %>
    </img>
 <% end %>

Then I've got a method in my controller, called save_new_scores_to_profile, that averages the data from the picture and updates the profile values.

How do I call my controller method when my link_to (the image) is clicked? Is there something like this available?

if link_to clicked
   perform_controller_or_helper_method
end

Because the user needs to select multiple images, I want them to stay on the page after clicking the images (that's why I have the link directed to '#'. I also have a submit button at the end of the page if that helps.

I'm open to using something other than link_to.

EDIT:

Here's where I'm at now in routes

 resources :preferences do
   member do
     get 'save_new_scores_to_profile'
     get 'checked_average_with_profile'
   end
 end

and the view:

<% @pages_selection.each do |pages_selection| %>
    <img> 
      <%= link_to image_tag(pages_selection.page_picture, :controller =>      
          :checked_average_with_profile, :action => :save_new_scores_to_profile, :image_id 
          => pages_selection.id) %>
    </img>
<% end %>

I have functions checked_average_with_profile and save_new_scores_to_profile in my controller that I know work (after testing with helper functions).

dblarons
  • 123
  • 2
  • 11
  • can you include your routes so we can provide a more specific answer? – jvnill Mar 08 '13 at 01:25
  • match 'quizzes/index', :to => 'quizzes#index' and resources :preferences I need to reroute back to preferences_path when an image is clicked – dblarons Mar 08 '13 at 02:09
  • If I just leave the path part of link_to blank it automatically redirects back to the same page, which is perfect – dblarons Mar 08 '13 at 02:23
  • you need to add `save_new_scores_to_profile` in your routes. – jvnill Mar 08 '13 at 02:25
  • ah, that is crucial. Thanks! – dblarons Mar 08 '13 at 02:38
  • So i added this (in addition to the stuff below) with no luck. resources :preferences do member do get 'save_new_scores_to_profile' get 'checked_average_with_profile' end end I've made a helper method with the same code that works for ALL the items in the loop. I just can't create a method that works for JUST the item selected. This is where the comment below comes into play. It's almost there, the controller just isn't being called. Any suggestions? – dblarons Mar 08 '13 at 04:06

1 Answers1

1
link_to image_tag(pages_selection.page_picture, :controller => :my_controller, :action => :save_new_scores_to_profile, :image_id => pages_selection.page_picture.id )

Insted of my_controller put the name of your controller. With the :image_id you have passed a parameter that you can reference in your controller action with the params hash like: params[:image_id].

Do all your work in that controller action (or with additional calls of helper methods), find the picture that has that image_id and make a redirect_to @picture_with_that_id

Zippie
  • 6,018
  • 6
  • 31
  • 46
  • Can you possibly explain what :action and :controller do? I've read a lot about them but can't quite make a clear distinction. I thought :controller was replaced in a newer version of Rails (please tell me if I'm wrong). Why does the save method need to be in :action and the other stuff in :controller? – dblarons Mar 08 '13 at 02:24
  • Oh, and the :image_id section has really helped. I just can't get the method called in :controller to work. – dblarons Mar 08 '13 at 02:28
  • The :controller, :action and :image_id are all parts of a link that will be generated and call your Controller. You have to have a action in your controller called save_new_scores_to_profile (an action is a controllers method, look at your controller it probably has some methods in it like 'new', 'create', 'show', 'index'). Those are all controller methods aka actions. Then in the definition of your controller method called 'save_new_scores_to_profile' you will have to put all your code to do whatever you want it to do, and at the end put redirect_to @picture_with_that_id and thats it. – Zippie Mar 08 '13 at 02:35