0

I m passing the id from link_to to locals. But it is not able to find the result, saying cannot find article without id. I need to pass the id for display articles in modal.

<% @articles.each do |a|%>
<p>
    <%= link_to a.title, {"data-toggle" => "modal", "data-target" => '#myModal', :id => a.id}%>
</p>
<% end %>

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria labelledby="myModalLabel" aria-hidden="true">
<%= render :partial => "site#show", :locals => {@article => Article.find(params[:id])}%>
</div>

In the controller

def home
  @articles = Article.order("created_at DESC").limit(5)
  @videos = Video.order("created_at DESC").limit(5)
end

And i m getting the id from the controller. It is not nill. The thing is i m able to find the articles in the block but not being able to send it to the locals.

Ahmad hamza
  • 1,816
  • 1
  • 23
  • 46

2 Answers2

0

this is because params[:id] is null or the record with params[:id] does not exist

In controller you should find where whether the article with the specific id exists if not handle it

@article = Article.find(params[:id])

good practise is to not use finders in views.

Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51
0

make sure that params[:id] is not nil and while passing locals you should always use symbols and access them as local variables in the partial e.g.

"site#show", :locals => { :article => Article.find(params[:id])}%>

in _show.html.erb

use it as

prcoder
  • 154
  • 1
  • 14
  • i m using bootstrap's modal.. http://twitter.github.io/bootstrap/javascript.html#modals – Ahmad hamza Jun 17 '13 at 07:57
  • somehow id is already passing but not the one which i need to pass.. `id = #myModal` this is passing.. but m not able to pass the `:id => a.id` – Ahmad hamza Jun 17 '13 at 07:59