Reputation: 1113
Update
I'm beginning to think this isn't possible. If it was a link rather than a partial I could pass a param but because it's a partial I can't give my form the :id of individual miniatures from this index. Does that sound right? I may have to think of another way to do my popup?
I'm launching a form in a popup with this code:
<a href="#collection-popup" class="open-popup-link">
<i class="fa fa-plus"></i></a>
<div id="collection-popup" class="white-popup mfp-hide">
<%= render 'collections/pop_form' %>
</div>
<script>
$('.open-popup-link').magnificPopup({
type:'inline',
midClick: true // Allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source in href.
});
</script>
The form it launches starts
<%= form_for(@collection, :html => { :class => "form-horizontal"} ) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :miniature_id, :value => @miniature_id %>
<%= f.label :status, 'Got, Want or Had?', class: "control-label" %>
It works fine from my @miniature show view but I'm now trying to get it to work on the index.
The controller has
def set_collection
@miniature_id = params[:id]
@collection = Collection.new(miniature: @miniature)
end
So it's expecting an [:id] param. In the index that id would be miniature.id
but I can't find a way of passing miniature.id
as :id in the params.
The Miniatures index action is as follows
def index
@search = Miniature.search(params[:q])
@search.sorts = 'name ASC' if @search.sorts.empty?
@miniatures = @search.result.uniq.paginate(page: params[:page])
end
miniature.id DOES work on the index page alongside this because it's placed in the @miniatures partial that will_paginate uses. If I can show miniature.id then surely I can pass it to the form?
I've tried the following
<%= render 'collections/pop_form', { id: miniature } %>
<%= render 'collections/pop_form', { id: miniature.id } %>
<%= render 'collections/pop_form', { miniature.id :id } %>
<%= render 'collections/pop_form', { id: id } %>
But I can't get the right one.
Upvotes: 1
Views: 1014
Reputation: 8257
Rendering a partial, does NOT call an associated controller action. So you need to define all the objects used within the page and the partials inserted into that page, via the action that matches the page.
So if the controller has the action:
def show
@texts = ['foo','bar']
end
And the view has:
<h1><%= @texts.join(' ') %></h1>
<%= render 'some/partial' %>
Some partial can contain this:
<%= @texts.first %>
@texts defined in the controller action is available to both the host page and the partials.
The hash after the partial declarations allows you to pass in local attributes to the partials. It does NOT generate params that will be passed to the controller.
So you can do this in the view:
<%= render 'some/partial', locals: {something: texts.first}
And then in the partial:
<%= something %>
In both these examples the output will be:
<h1>foo bar</h1>
foo
That's just scraping the surface and I'd recommend you look at the Rails Guide: Layouts and Rendering in Rails
Upvotes: 1
Reputation: 6072
You should check out the Rails guide on passing variables in to partial templates. In particular, you want to explicitly set it as a partial, and name your local variables hash. If your first view is being rendered by the set_collection
controller action you posted, you'll have an @miniature_id
variable available to you. Thus, your render call should be:
<%= render partial: 'collections/pop_form', locals: {id: @miniature_id} %>
Upvotes: 0