RAILS: Calling a parent model within a nested partial form - ruby-on-rails

Im trying to call a variable from the 'product' model in a 'variant' partial. I can call an existing variable from the 'variant' using:
<%= f.object.product_id %>
But I can't get this to work:
<%= f.object.product.id %>
I can do this:
<%= f.object.product %>
But it returns #<Product:0x007fee2c9a8ec8> and I don't know what to do with it!
Here's a more detailed overview of the situation:
I have 'store' model that has_many 'products'. Each 'product' has_many 'variants'. The variants are nested. Here's the '/store/show.html.erb' file:
<% #store.products.each do |product| %>
<tr>
<td>
<%= form_for(product) do |f| %>
<%= f.fields_for :variants do |builder| %>
<%= render 'variant_fields', f: builder %>
<% end %>
</td>
</tr>
<% end %>
And here's the '/store/_variant_fields' partial:
<fieldset>
</table>
<tbody>
<tr>
<td><%= f.label :variant_name %><%= f.text_field :variant_name %></td>
</tr>
</tbody>
</table>
</fieldset>

the easiest solution would be to pass the product to your partial:
<% render 'variant_fields', f: builder, product: f.object %>
in your partial:
<fieldset>
</table>
<tbody>
<tr>
<td><%= f.label :variant_name %><%= f.text_field :variant_name %><%= product.inspect %></td>
</tr>
</tbody>
</table>
</fieldset>
See what product.inspect shows you.

Related

rails ransack remote true with pagination

I've a controller for WeekDay. Then I tried to add RanSack in my project. It works good except when I tried to enable remote true, it shows only search form. AJAX call is being made, but nothing shows up.
Controller code
def index
#q = WeekDay.ransack(params[:q])
#week_days = #q.result().page(params[:page]).per(10)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #week_days }
end
end
index.html.erb
<div id="week_days"><%= render 'week_days' %></div>
_week_days.html.erb
<%= search_form_for #q,remote: true do |f| %>
<%= f.label :datum_gteq %>
<%= f.date_field :datum_gteq%>
<%= f.label :datum_lteq %>
<%= f.date_field :datum_lteq %>
<%= f.submit %>
<% end %>
<table>
<thead>
<tr>
<th><%=sort_link #q, :datum %></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #week_days.each do |week_day| %>
<tr>
<td><%= l week_day.datum %></td>
<td><%= link_to 'Show', week_day %></td>
<td><%= link_to 'Edit', edit_week_day_path(week_day) %></td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate #week_days %>
<br>
<%= link_to 'New Week Day', new_week_day_path %>
some idea from me is you split the _week_days into partial and then render part of data using ajax, I added new div with name display-area as target for ajax to render and you should create javascripts to render it without reload the page with escape javascripts (j)
_week_days.html.erb
<%= render "search" %>
<%= render "my_data" %>
_search.html.erb
<%= search_form_for #q,remote: true do |f| %>
<%= f.label :datum_gteq %>
<%= f.date_field :datum_gteq%>
<%= f.label :datum_lteq %>
<%= f.date_field :datum_lteq %>
<%= f.submit %>
<% end %>
_my_data.html.erb
<div class="display-area">
<table>
<thead>
<tr>
<th><%=sort_link #q, :datum %></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #week_days.each do |week_day| %>
<tr>
<td><%= l week_day.datum %></td>
<td><%= link_to 'Show', week_day %></td>
<td><%= link_to 'Edit', edit_week_day_path(week_day) %></td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate #week_days %>
<br>
<%= link_to 'New Week Day', new_week_day_path %>
</div>
write _weekdays.js.erb inside app/assets/javascripts
$('.display-area').html("<%= j render(partial: 'my_data') %>")

Rails 5 ActiveRecord Update Serialized Array in Form

I have a field that is a serialized array. It's loaded into my model and accessed in a form:
class Site < ApplicationRecord
serialize :steps, Array
end
<table class="listing" summary="Site list">
<tr class="header">
<th>Name</th>
<th>Step 1</th>
<th>Step 2</th>
<th>Step 3</th>
<th>Actions</th>
</tr>
<% #sites.each do |site| %>
<tr>
<td><%= site.name %></td>
<% site.steps.each do |step| %>
<td><%= step %></td>
<% end %>
<td class="actions">
<%= link_to("Show", site_path(site), :class => 'action show') %>
<%= link_to("Edit", edit_site_path(site), :class => 'action edit') %>
<%= link_to("Delete", delete_site_path(site), :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
I'm trying to update my edit form so that I can edit each "step" in the array.
<%= form_for(#site) do |f| %>
<table summary="Site form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<% a=1 %>
<% #site.steps.each do |step| %>
<tr>
<th>Step <%= a %>
<td><%= text_field :site, :steps, :value => step %></td>
<% a += 1 %>
</tr>
<% end %>
</table>
<div class="form-buttons">
<%= f.submit("Update Site") %>
</div>
<% end %>
The edit form displays the steps field correctly as each individual string in the array. However, when I attempt to submit the form I get the following error:
Attribute was supposed to be a Array, but was a String.
So steps is being submitted as the last entry in the array. How can I display the form correctly and also present the updated array back to the controller for processing?
Your text_field would need to multiple set to true. I believe in your case, something like this should work.
<%= f.text_field(:steps, { multiple: true, value: #site.steps[step] }) %>

Dynamic checkboxes to send array to controller only sending first choice

I have a table of data created like so:
<% #products.each do |product| %>
<tr>
<td><%= form_tag push_to_products_path(product), :id =>
'submit_products' do %>
<%= check_box_tag('send[]', product.sellersku) %>
<% end %>
</td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
<% end %>
</tr>
I created the nested form_tag because I have a button outside of the form that uses the checkbox data to send to my Product controller. This is the button code:
<div class="row pad_bottom col-md-4 col-md-offset-1 text-center">
<button type="submit" form="submit_products" class="btn btn-primary">
<span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Push
Selected Products
</button>
</div>
The checkboxes are correctly created in the view, but when I test only the first box has the sellersku value.. all the other checkboxes don't pass anything. They should also be passing the sellersku.
Pretty sure its my loop, just not sure what I did wrong??
Declare your form outside of the loop. Right now, you're looping through your #products and creating a form for each one.
<%= form_tag push_to_products_path, :id => 'submit_products' do %>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag('send[]', product.sellersku) %></td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
</tr>
<% end %>
<%= submit_tag("Push Selected Products") %>
<% end %>
<%= form_tag products_push_to_path, :id => 'submit_products' do %>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag('send[]', product.sellersku) %>
</td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
</tr>
<% end %>
<%= submit_tag "Push Selected Products" %> #replace this with glyphicon part
<% end %>
routes.rb #assuming your action name is push_to with resources products
resources :products do
collection do
post 'push_to'
end
end

How can I edit a single field for an array of object?

I'm trying to edit a collection of users. I want to render table with a list of names and checkboxes. I'm close, but I'm missing something.
My form looks like so:
<%= form_for 'users[]', :url => approve_users_path, :html => {:class => 'form-horizontal'} do |f| %>
<tbody>
<% for user in #users %>
<%= fields_for user do |u| %>
<tr>
<td><%= user.name %></td>
<td><%= u.check_box :vouched %></td>
</tr>
<% end %>
<% end %>
</tbody>
<% end %>
which generates
<tr>
<td>steve cabillero</td>
<td><input name="user[vouched]" type="hidden" value="0" /><input id="user_vouched" name="user[vouched]" type="checkbox" value="1" /></td>
however, I need the input name in the form of users[id][vouched] and vouched is a virtual attribute.
when I try to use f.check_box I get a method not found error.
you should use form_tag if you are not using a specific resource in the form. I also suggest using #each instead of a for loop and check_box_tag for the checkbox
<%= form_tag approve_users_path, :class => 'form-horizontal' do %>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= check_box_tag "users[#{user.id}][vouched]" %></td>
</tr>
<% end %>
</tbody>
<% end %>

Nested Model Form - Render not working

I'm new in rails and need some help creating a Nested form.
I have this in teams/_form.html.erb
<%= form_for #team do |f| %>
<div class="field">
<%= f.label "Name" %><br />
<%= f.text_field :name, :required => true %>
</div>
<%= f.fields_for :players do |builder| %>
<%= render :partial => 'players_field', :f => builder %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What I want is create a list of players in the team form. The problem is that the render don't work and players_field.html.erb don't render.
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Position</th>
</tr>
</thead>
<tbody id="tableRow">
<tr>
</tr>
</tbody>
</table>
TR is added to the table with js.
UPDATE:
Another problem appear now :/
This is my js:
var newRow = document.createElement('tr');
newRow.innerHTML = "<td>"+ counter +"</td>"+
"<%= f.text_field :name %>"+
"<%= f.text_field :position %>"+;
document.getElementById("tableRow").appendChild(newRow);
Error:
undefined local variable or method `f' for #<#<Class:0x0000000288dd38>:0x007f77cc1226e0>
Why I can't add this ?
The block
<%= f.fields_for :players do |builder| %>
<%= render :partial => 'players_field', :f => builder %>
<% end %>
is basically a loop through #team.players. It might be the case that #team simply doesn't have any players to loop through. To show the form for at least one player you can change this to
f.fields_for :players, #team.players.build do |builder| ...

Resources