2013-03-08 67 views
0

我需要能够点击图像(在一堆图像中)并根据点击图像更新配置文件表。点击图片并更新数据库在轨道上的行

在我看来图像列表:

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

然后,我有我的控制器,称为save_new_scores_to_profile的方法,即平均从图中数据并更新配置文件值。

当我的link_to(图像)被点击时,我该如何调用我的控制器方法?有没有这样的东西可用?

if link_to clicked 
    perform_controller_or_helper_method 
end 

因为用户需要选择多个图像,我希望他们能够点击图片(这就是为什么我有链接指向“#”后留在页面上。我也有在年底提交按钮。页面

的,是否可以帮助我愿意用比其他的link_to东西

编辑:

在这里,在现在也正是我在路线

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

和视图:

<% @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 %> 

我有功能checked_average_with_profile和save_new_scores_to_profile在我的控制,我知道工作(有辅助功能测试后)。

+0

你可以包括你的路线,所以我们可以提供更具体的回答? – jvnill 2013-03-08 01:25:47

+0

match'quizzes/index',:to =>'quizzes#index'and resources:preferences我需要在点击图像时重新路由回preferences_path – dblarons 2013-03-08 02:09:37

+0

如果我只是将link_to的路径部分留空,它会自动重定向回相同的页面,这是完美的 – dblarons 2013-03-08 02:23:06

回答

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放置了控制器的名称。 对于:image_id,您已经传递了一个参数,您可以在控制器操作中使用params哈希参考:params[:image_id]

不要在该控制器的动作所有的工作(或与辅助方法额外话费), 发现有image_id的图片,并作出redirect_to的@picture_with_that_id

+0

你能解释一下:action和controller是做什么的?我已经阅读了很多关于它们的内容,但不能明确区分。我想:控制器被更新版本的Rails所取代(请告诉我,如果我错了)。为什么save方法需要:action和controller中的其他东西? – dblarons 2013-03-08 02:24:43

+0

哦,和:image_id部分真的帮助。我只是无法获取调用的方法:控制器工作。 – dblarons 2013-03-08 02:28:06

+0

The:controller,:action和:image_id是链接的所有部分,将生成并调用您的Controller。你必须在你的控制器中有一个名为save_new_scores_to_profile的动作(一个动作是一个控制器方法,看看你的控制器,它可能有一些方法,比如'new','create','show','index')。这些都是控制器方法aka动作。然后在控制器方法“save_new_scores_to_profile”的定义中,您必须将所有代码放在您想做的任何事情上,并在最后放置redirect_to @picture_with_that_id以及它。 – Zippie 2013-03-08 02:35:42