Ruby on Rails Sunday, April 10, 2016

> On Apr 10, 2016, at 4:30 PM, David Williams <lists@ruby-forum.com> wrote:
>
> Walter Davis wrote in post #1182737:
>
> Ok, I tested a second link method
>
> <%= link_to image_tag(post.photo.url(:medium), style: 'height: 300px;
> width: 500px;'), post, remote: true, data: {:toggle => 'modal', :target
> => '#reusable_modal'}, lazy: true %>
>
> When I remove the post local variable -> targeting the action. The modal
> doesn't pop up an image at all.
>

I suspect the issue is in how you are constructing this link, and how you have directed your controller to respond to a "format: js" request. When you say "remote: true", then your controller must be set up to respond with your desired layout in the format.js section of the respond_to block.

In the past, I have done these without using the remote: true thing at all. I suspect you can remove it as well. Try the following:

# posts_controller.rb
# add a new method next to show()

def modal
@post = Post.find params[:id]
end

# views/posts/modal.html.erb

<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4><%= @post.title %></h4>
</div>
<div class="modal-body">
<%= @post.whatever_else_you_want_to_render %>
</div>
<div class="modal-footer">
<button class="btn btn-default" data-dismiss="modal">Close</button>
</div>

(be sure to put whatever actual erb you want to use in your modal-body section)

# routes.rb

resources :posts do
member do
get :modal
end
end

Now to link to it, you will have a new modal_post_path() helper to play with, so your link would look like this:

<%= link_to image_tag(post.photo.url(:medium), style: 'height: 300px;
width: 500px;'), modal_post_path(post), data: {:toggle => 'modal', :target =>
'#reusable_modal'}, lazy: true %>

See how that goes for you.

Walter

>> Walter
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/45903a2d603d6f886193811f68cf5410%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F178C8C2-C8C9-423A-8697-E11870080E7F%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment