【发布时间】:2013-03-08 00:59:26
【问题描述】:
我需要能够单击图像(从一堆图像中)并根据单击的图像更新配置文件表。
我视图中的图像列表:
<% @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 我知道它们可以工作(在使用辅助函数测试之后)。
【问题讨论】:
-
您能否提供您的路线,以便我们提供更具体的答案?
-
匹配 'quizzes/index', :to => 'quizzes#index' 和资源 :preferences 我需要在点击图片时重新路由回preferences_path
-
如果我只是将 link_to 的路径部分留空,它会自动重定向回同一页面,这是完美的
-
您需要在路由中添加
save_new_scores_to_profile。 -
啊,这很关键。谢谢!
标签: ruby-on-rails ruby-on-rails-3 image helper link-to