我正在使用bootstrap,我有一个链接,可以从images#show
重定向到home#index
操作。该链接以haml:
- url = @entry.class.to_s.downcase.singularize
= link_to(send("#{url}_path", @entry), data: { 'type' => @entry.class.to_s.downcase }) do
= yield
目前,home#index
显示图像列表,单击图像时,上面写的链接会将用户重定向到特定图像Ex:images/1
的视图。也就是说@entry
表示图像,点击时会重定向到特定图像或(@entry
)。
我不想离开主页(home#index
),我只想显示一个模态视图,该视图会考虑点击哪些图像(或@entry
)。当单击上面的链接时,如何修改此链接并添加将显示引导模式的代码?我应该遵循哪些其他步骤?我只是需要帮助才能显示模态。
这是我的追踪:
Started GET "/images/3" for ::1 at 2016-09-07 22:28:10 -0400
Processing by ImagesController#show as */*
Parameters: {"id"=>"3"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Image Load (0.3ms) SELECT "images".* FROM "images" WHERE "images"."id" = $1 LIMIT 1 [["id", 3]]
Group Load (0.4ms) SELECT DISTINCT "groups".* FROM "groups" INNER JOIN "group_memberships" ON "groups"."id" = "group_memberships"."group_id" WHERE "group_memberships"."member_id" = $1 AND "group_memberships"."member_type" = $2 AND "group_memberships"."group_type" = $3 [["member_id", 1], ["member_type", "User"], ["group_type", "Group"]]
GroupMembership Load (0.4ms) SELECT "group_memberships".* FROM "group_memberships" WHERE "group_memberships"."member_id" = $1 AND "group_memberships"."member_type" = $2 [["member_id", 3], ["member_type", "Image"]]
GroupMembership Load (0.5ms) SELECT "group_memberships".* FROM "group_memberships" WHERE "group_memberships"."group_type" = $1 AND "group_memberships"."member_type" = $2 AND "group_memberships"."member_id" = $3 AND "group_memberships"."group_id" IN (-1, -1) [["group_type", "Group"], ["member_type", "User"], ["member_id", 1]]
Rendered images/_modal.html.erb (0.0ms)
Rendered images/show.js.erb (1.2ms)
Completed 200 OK in 27ms (Views: 10.0ms | ActiveRecord: 1.9ms)
答案 0 :(得分:1)
一种选择是使用exec_then_eval('''x = 4
y = 5
x + y''')) # 9
exec_then_eval('''x = 4
y = 5;x + y''')) # 9
exec_then_eval('''x = 4
y = 5;(
x + y *
2)''') # 14
模板并将您的链接更改为使用js
选项:
remote: true
接下来在控制器中,响应类型可以设置为<%- @images.each do |image| -%>
<%= link_to '...', image_path(image) remote: true %>
<%- end -%>
<div id="modals"></div>
(可能还是保持js
选项的好主意):
html
然后在处理respond_to do |format|
format.js
end
:
js
最后为模态添加部分并设置所需的任何标记:
// app/views/images/show.js.erb
$("#modals").html('<%= j render "images/modal", image: @image %>');
$('#modals .modal').modal();
$('#modals .modal').modal('.toggle');