Deep rails nested attributes on the fly - ruby-on-rails

I have a form, the form has 'fields_for :addresses' and renders partial 'address_fields' in that 'fields_for :addresses', in that partial 'address_fields' there is 'fields_for :sub_address', when the browser is first loaded, the 'fields_for :sub_address' in the partial 'address_fields' appears, but when the 'Add Addresses' button is pressed the "fields_for :sub_address" element does not appear. So basically I have a nested form inside the nested form 'addresses', but it doesn't render when I click on 'Add Address'.
This is my 'Add Address' javascript code:
class AddFields {
constructor() {
this.links = document.querySelectorAll('.add_fields');
this.iterateLinks();
}
iterateLinks() {
if (this.links.length === 0) return
this.links.forEach(link => {
link.addEventListener('click', e => {
this.handleClick(link, e)
});
});
}
handleClick(link, e) {
if (!link || !e) return;
e.preventDefault();
let time = new Date().getTime();
let linkId = link.dataset.id;
let regexp = linkId ? new RegExp(linkId, 'g') : null
let newFields = regexp ? link.dataset.fields.replace(regexp, time) : null
newFields ? link.insertAdjacentHTML('beforebegin', newFields) : null
}
}
window.addEventListener('turbolinks:load', () => new AddFields());
here is my views:
new.html.erb
<%= form_with model: #data, url: address_path(token: params[:token]) do |form| %>
<div class="row">
<div class="col">
//another html here
</div>
</div>
<fieldset>
<div class="card mt-2">
<div class="card-body">
<%= form.fields_for :address do |addr| %>
<%= render 'sub_address_fields', form: addr %>
<% end %>
<%= link_to_add_fields 'Add Address', form, :addr %>
</div>
</div>
</fieldset>
<div class="form-group">
<%= form.submit :'Submit', name: 'submit', class: 'btn btn-primary mt-2' %>
</div>
<% end %>
```
partial view:
<div class="nested-fields">
<%= form.hidden_field :_destroy %>
<div class="row">
<div class="col">
//another html here
</div>
<%= form.fields_for :sub_address do |sub_addr| %>
<div class="row">
<div class="col">
<div class="form-group mt-2">
<div class="custom-file form-control">
<%= sub_addr.label :id_card, class: 'id-card-label', for: 'id-card-file' %>
<%= sub_addr.file_field :attachment_id_card, id: 'id-card-file' %>
</div>
</div>
</div>
<div class="col">
<div class="form-group mt-2">
<div class="custom-file form-control">
<%= sub_addr.label :self_assessment, class: 'self-assessment-label', for: 'self-assessment-file' %>
<%= sub_addr.file_field :attachment_self_assess, id: 'self-assess-file' %>
</div>
</div>
</div>
<div class="col">
<div class="form-group mt-2">
<div class="custom-file form-control">
<%= sub_addr.label 'SWAB', class: 'swab-label', for: 'swab-file' %>
<%= sub_addr.file_field :attachment_swab, id: 'swab-file' %>
</div>
</div>
</div>
</div>
<% end %>
<div class="form-group">
<%= link_to "Remove", '#', class: "remove_fields"%>
</div>
</div>
and this is the helper:
module Frontend::Data::DataHelper
def link_to_add_fields(name, form, association)
new_object = form.object.send(association).klass.new
id = new_object.object_id
fields = form.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + '_fields', form: builder)
end
link_to(name, '#', class: 'add_fields', data: {id: id, fields: fields.gsub('\n', '')})
end
end
Is javascript not able to display the form generated from the 'render' function on rails?

Related

What would the rails helper tag equivalent be of this select?

I have been struggling to convert this to some sort of rails helper select. Can anyone help with this?
<%= form_with(model: scheduleevent, class: "contents", data: { controller: 'nested-form', nested_form_wrapper_selector_value: '.nested-form-wrapper' }) do |form| %>
... schedtimerange_form start ...
<%= select_tag "days[]",options_for_select(Date::DAYNAMES.zip((1..7))), id:"days[]", multiple: true, class: 'multis' %>
<% end %>
The select tag is within a nested attribute partial called schedtimerange_form that is called like this:
<template data-nested-form-target="template">
<%= form.fields_for :schedtimeranges, Schedtimerange.new, child_index: 'NEW_RECORD' do |schedtimerange| %>
<%= render "schedtimerange_form", form: schedtimerange %>
<% end %>
</template>
<%= form.fields_for :schedtimeranges do |schedtimerange| %>
<%= render "schedtimerange_form", form: schedtimerange %>
<% end %>
Schedtimerange_form:
<div data-new-record="<%= form.object.new_record? %>">
<div class="w-full">
<div data-controller="select">
<%= select_tag "days[]",options_for_select(Date::DAYNAMES.zip((1..7))), id:"days[]", multiple: true, class: 'multis' %> </div>
</div>
<div>
<div class="w-full mb-2">
<label class="form_label"><%= t("scheduling_events_new_lbl_starttime") %> </label>
<div class="pr-4"><%= form.text_field :start_time, data: { controller: "flatpickr", flatpickr_enable_time: true, flatpickr_no_calendar: true,
flatpickr_date_format: "h:i K" }, class: 'form_control', placeholder: "#{t("scheduling_events_new_lbl_starttime")}" %> </div>
</div>
<div class="w-full mb-4">
<label class="form_label"><%= t("scheduling_events_new_lbl_endtime") %> </label>
<div class="pr-4"><%= form.text_field :end_time, data: { controller: "flatpickr", flatpickr_enable_time: true, flatpickr_no_calendar: true, flatpickr_date_format: "h:i K" }, class: 'form_control', placeholder: "#{t("scheduling_events_new_lbl_endtime")}" %> </div>
</div>
<div class="w-full">
<button type="button" class="font-medium text-red-600 dark:text-red-500 hover:underline" data-action="nested-form#remove">Remove Times</button>
</div>
</div><%= form.hidden_field :_destroy %>
</div>
<%= select_tag "days[]",options_for_select(Date::DAYNAMES.zip((1..7))), id:"days[]" %>

Rails 5 - show block on check box

In my rails view (am new to rails), I want to show a div when a check box is set to true.
How should I do that?
I thought of using
<%= check_box_tag 'advancedtagging', false %> Tag details
combined with:
<% if advancedtagging = '1' %>
<div class="row">
<%= render 'tags/advanced', :object => #annotation %>
</div>
<% end -%>
This is my view:
<div class="row">
<div class="col-md-4">
<div data-spy="affix">
<%= link_to 'Close', annotation_path(#annotation), :class => "btn btn-link" %>
<%= link_to 'List' , annotations_path, :class => "btn btn-link" %>
<%= check_box_tag 'advancedtagging', false %> Tag details
<%= render 'tags/form', :object => #annotation %>
<br>
<div class="panel panel-default" id="annotationResults">
<%= render 'tags/tag_list', :object => #annotation %>
</div>
</div>
</div>
<div class="col-md-8" id="file">
<%= content_tag :iframe, nil, src: pdf_annotation_path(#annotation), width: "100%", height: "875px", frameBorder: "0" %>
</div>
</div>
<% if advancedtagging = '1' %>
<div class="row">
<%= render 'tags/advanced', :object => #annotation %>
</div>
<% end -%>
Put an id to your div and your checkbox
<div id="mydiv" class="row">
<%= render 'tags/advanced', :object => #annotation %>
</div>
Using Jquery
$(function() {
$('#mycheckbox').click(function(event){
event.preventDefault();
$('div#mydiv').toggle();
});
});
Add your CSS file div.hidden { display: none; }
Using pure js
Add in some js file for example application.js or other you want
$(function() {
var mycheckbox = document.getElementById('mycheckbox');
var mydiv = document.getElementById('mydiv');
var toggleDiv = function(){
if(mycheckbox.checked) {
mydiv.style['display'] = 'block';
} else {
mydiv.style['display'] = 'none';
}
}
mycheckbox.onclick = toggleDiv;
toggleDiv();
});

rails nested form for many to many not working as expected

I have 3 models,
the relationship between those models is many to many relationship.
unit model
has_many :unitrentperiods, inverse_of: :unit
has_many :rentperiods, :through => :unitrentperiods
accepts_nested_attributes_for :unitrentperiods
unitrentperiod model
belongs_to :unit
belongs_to :rentperiod
rentperiod model
has_many :unitrentperiods, inverse_of: :rentperiod
has_many :units, :through => :unitrentperiods
and i have a unit form, where i can create a new unit or edit an unit.
i want to make my unit form not just able to manage my unit but able to manage my "unitrentperiod" as well, so lets say i have 2 rent periods like
"6 months"
"12 months"
i want my unit form to display those rent periods also with price input, so when i save my unit form, i also save 2 unitrentperiod based on rent periods in database, i did this in my view:
<style>
.divCB {
margin-right: 10px
}
</style>
<%= form_for(#unit) do |f| %>
<% if #unit.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#unit.errors.count, "error") %> prohibited this unit from being saved:</h2>
<ul>
<% #unit.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
<div class="page-header">
<div class="row">
<div class="col-md-7">
<h1 class="h2 page-title">Manage Unit</h1>
<div class="text-muted page-desc"></div>
</div>
<div class="col-md-5 charts">
<div class="row">
</div>
</div>
</div>
</div>
<!-- Breadcrumb -->
<ol class="breadcrumb">
<li class="active">Manage Unit</li>
<!-- Breadcrumb Menu-->
</ol>
<div class="container-fluid">
<div class="animated fadeIn">
<!--/.row-->
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<strong>Unit Form</strong>
</div>
<%= form_for #unit, html: {class: "form-horizontal"} do |f| %>
<div class="card-block">
<div class="form-group row">
<%= f.label 'Apartement', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= collection_select(:unit, :apt_id, #apts, :id, :apt_name, {:prompt => "Please select an Apartement"}, {:id => 'apartements_select', :class => "select", :style => "width:240px"}) %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Tower', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= collection_select(:unit, :tower_id, #towers, :id, :tower_name, {:prompt => "Select a Tower"}, {:id => 'towers_select', :class => "select", :style => "width:240px"}) %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit number', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitno, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit size', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitsize, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit view', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :unitview, options_for_select(#unit_views.collect { |s| [s[0].humanize, s[0]] }, selected: #unit.unitview), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Room Type', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= collection_select(:unit, :room_type_id, #roomtypes, :id, :room_type_desc, {:prompt => false}, class: "form-control") %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit floor', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitfloor, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit description', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitdesc, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Total bed', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :totalbedroom, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Furnish', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :furnish, options_for_select(#furnish_types.collect { |s| [s[0].humanize, s[0]] }, selected: #unit.furnish), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'For sell', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :isforsell, options_for_select( [ ['No', 0], ['Yes', 1] ], selected: #unit.isforsell), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Sell price', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :sellprice, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'For rent', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :isforrent, options_for_select( [ ['No', 0], ['Yes', 1] ], selected: #unit.isforrent), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Rent price', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<table>
<% #rentperiods.each do |rentperiod| -%>
<tr>
<td><%= rentperiod.rentmonth %> Month</td>
<td>
<%= fields_for(#unitrentperiods) do |elem| %>
<%= elem.text_field :rentprice, class: 'col-md-4 form-control' %> IDR
<%= elem.hidden_field :rentperiod_id, :value => rentperiod.id %>
<% end %>
</td>
</tr>
<% end -%>
</table>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit status', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :unitstatus, options_for_select(#unit_statuses.collect { |s| [s[0].humanize, s[0]] }, selected: #unit.unitstatus), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Photo Template', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :template_id, options_for_select(#templates.collect { |s| [s[:type], s[:id]] }, selected: #unit.template_id), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Floor plan', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<div id="divFloorplan">
</div>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit plan', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<div id="divUnitplan">
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-dot-circle-o"></i> Submit</button>
</div>
<% end %>
</div>
</div>
<!--/col-->
</div>
<!--/.row-->
</div>
</div>
<script>
$(document).ready(function()
{
$('.select').select2();
$('#apartements_select').change(function() {
$.ajax({
url: "/units/gettowers",
data: {
apt_id : $('#apartements_select').val()
},
dataType: "script"
});
$.ajax({
url: "/units/getfloorplans",
data: {
apt_id : $('#apartements_select').val()
},
dataType: "script"
});
$.ajax({
url: "/units/getunitplans",
data: {
apt_id : $('#apartements_select').val()
},
dataType: "script"
});
});
});
</script>
controller
class UnitsController < ApplicationController
before_action :set_unit, only: [:show, :edit, :update, :destroy]
# GET /units
# GET /units.json
def index
#units = Unit.paginate(page: params[:page])
end
# GET /units/1
# GET /units/1.json
def show
end
def gettowers
# updates towers based on apartement selection
tower = Tower.where(:apt_id => params[:apt_id])
# map to name and id for use in our options_for_select
#towers = tower.map{|a| [a.tower_name, a.id]}.insert(0, "Select a Tower")
end
def getfloorplans
floorplans = Floorplan.where(:apt_id => params[:apt_id]).select("id, floorplanphoto")
#imgs = floorplans
end
def getunitplans
unitplans = Unitplan.where(:apt_id => params[:apt_id]).select("id, unitplanphoto")
#imgs = unitplans
end
def preparedata
#master tables
#apts = Apt.all
#towers = Tower.all
#roomtypes = RoomType.all
#rentperiods = Rentperiod.all
array = []
Template.all.each do |t|
hash = { :type => t.room_type.room_type_desc, :id => t.id }
array.push(hash)
end
#templates = array
#enum from model
#unit_views = Unit.unitviews
#furnish_types = Unit.furnishes
#unit_statuses = Unit.unitstatuses
end
# GET /units/new
def new
#unit = Unit.new
#unitrentperiods = #unit.unitrentperiods.build
preparedata
# #rentperiods.each do |r|
# #unit.unitrentperiods.build
# end
end
# GET /units/1/edit
def edit
##unitrentperiods = #unit.unitrentperiods
#unitrentperiods = #unit.unitrentperiods.build
preparedata
end
# POST /units
# POST /units.json
def create
#unit = Unit.new(unit_params)
respond_to do |format|
if #unit.save
format.html { redirect_to #unit, notice: 'Unit was successfully created.' }
format.json { render :show, status: :created, location: #unit }
else
format.html { render :new }
format.json { render json: #unit.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /units/1
# PATCH/PUT /units/1.json
def update
respond_to do |format|
if #unit.update(unit_params)
format.html { redirect_to #unit, notice: 'Unit was successfully updated.' }
format.json { render :show, status: :ok, location: #unit }
else
format.html { render :edit }
format.json { render json: #unit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /units/1
# DELETE /units/1.json
def destroy
#unit.destroy
respond_to do |format|
format.html { redirect_to units_url, notice: 'Unit was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_unit
#unit = Unit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def unit_params
params.require(:unit).permit(:apt_id, :tower_id, :unitno, :unitsize, :unitview,
:room_type_id, :unitfloor,
:unitdesc, :isforsell, :sellprice, :isforrent, :furnish, :totalbedroom,
:unitstatus, :template_id, :floorplan_id, :unitplan_id,
unitrentperiods_attributes: [:rentprice, :rentperiod_id])
end
end
somehow it's not working. Did i miss something? need some guide to make it work.
At first you don't accept nested attributes, add
accept_nested_attributes_for :unitrentperiods
right below has_many :unitrentperiods
Also, you might need to configure strong parameters to allow that nested attributes parameter (or attr_accessible if you are on Rails 3).
Gotcha here: you have to allow unitrentperiods_attributes, not just unitrentperiods.

Bootstrap modal nested model form view messed up

Here is what happens
Here is my views code
Form
<%= simple_form_for([:supplier, #fuel_price],remote: true, :html => {:class => 'form-vertical' }) do |f| %>
<%= f.simple_fields_for :fuel_products do |fuel_products_form| %>
<div class="field">
<%= render partial: 'fuel_products_fields', locals: {f: fuel_products_form} %>
</div>
<% end %>
<%= link_to_add_fields "Add more", f, :fuel_products %>
<div class="modal-footer">
<%= f.button :submit, "Update", class: "btn btn-success"%>
</div>
<%end%>
Fuel Products Field Partial
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<%= f.hidden_field :_destroy %>
<ul class="list-unstyled">
<li><%= link_to '#', class: "btn btn-danger btn-xs remove_fields" do %>
Remove
<%end%></li>
</ul>
Javascript code
ready = ->
$('#FuelmodelBody').on 'click', '.remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('.field').hide()
event.preventDefault()
$('#FuelmodelBody').on 'click', '.add_fields', (event) ->
console.log('It is really happening ....')
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
$(document).ready(ready)
$(document).on('page:load', ready)
Views Modal code
<div class="modal inmodal" id="newFuelPrice" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content animated bounceInRight">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<i class="fa fa-usd modal-icon" style="color:#1ab394"></i>
<h4 class="modal-title">Add New Fuel Price</h4>
<span class="font-bold">Your contacts will get text message of your latest price</span>
</br>
<span class="font-bold">As (your fuel price + the formula).</span>
</div>
<div class="modal-body" id="FuelmodelBody">
<!-- form -->
</div>
</div>
</div>
</div>
I also have a helper function for link_to_add_fields
def link_to_add_fields(name, f, association)
# creates a new instance of the 'has_many' object
new_object = f.object.send(association).klass.new
# new_object = f.object.association.klass.new
# f.object.association.klass # => Document
# new_object = f.object.documents.build Document(user_id: f.object.id)
# gets the object id
id = new_object.object_id
# creates a new form for the association
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s + '_fields', f: builder)
end
link_to(name, '#', class: 'add_fields', data: { id: id, fields: fields.delete("\n") })
end
Moving around code since morning and still could not figure out how to fix those buttons by aligning side by add, the add more will also be button in blue, i was just playing around and converted it to link. I am Using simple Form
If you want to align your "Remove" buttons with their corresponding Fuel type and Price fields, then you need to put them all in a single Bootstrap row like so:
<div class="row">
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<div class="col-md-3">
<%= f.hidden_field :_destroy %>
<ul class="list-unstyled">
<li><%= link_to '#', class: "btn btn-danger btn-xs remove_fields" do %>
Remove
<%end%></li>
</ul>
</div>
</div>
Also, you probably don't need that extra ul tag. It does not carry any additional meaning, since you have just one button in there. It is not a list.
<div class="row">
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<div class="col-md-3">
<%= f.hidden_field :_destroy %>
<%= link_to 'Remove', '#', class: "btn btn-danger btn-xs remove_fields" %>
</div>
</div>

Rails 4 Two Forms, Separate Models On One Page

I have two models for communication, Post and Reply. For some reason I can't figure out, the #reply form is unresponsive. Submit does not work, and the file upload field does not work, but only on the #reply form, the #prosect and #post forms work perfectly.
<h1><strong><%= #group.name %></strong></h1>
<p><em><%= #group.description %></em><p>
<p>Operated by: <% #owner = User.find(#group.owner_id) %><%= #owner.name %></p>
<% if current_user.group_id == nil %>
<%= form_for(#prospect) do |f| %>
<%= f.hidden_field :group_id, value: #group.id %>
<%= f.submit "Join Group", class: "btn btn-primary" %>
<% end %>
<% end %>
<% if current_user.group_id == #group.id %>
<%= form_for(#post) do |f| %>
<%= f.hidden_field :group_id, value: #group.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<div class="form-group col-xs-12">
<div class="row">
<div class="col-xs-11">
<%= f.text_area :content, class: 'form-control', placeholder: "What ails you?" %>
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-success" onclick="addpic(0);">Add a pic</button>
</div>
</div>
</div>
<div class="form-group hidden col-xs-12" id="picture0">
<%= f.file_field :image, class: 'form-control' %>
</div>
<div class="form-group col-xs-12 actions">
<%= f.submit %>
</div>
<% end %>
<% #group.posts.each do |p| %>
<div class="post">
<div class="post-meta">
<%= p.user.name %> <em><%= p.created_at.strftime('%a, %b %e, %Y %r') %></em>
</div>
<%= p.content %>
<% if p.image %>
<img src="data:image/png;base64,<%= p.image %>" class="img-responsive"/>
<% end %>
<% p.replies.each do |r| %>
<%= r.user.name %><br/><%= r.content %>
<% if r.image %>
<img src="data:image/png;base64,<%= r.image %>" class="img-responsive"/>
<% end %>
<% end %>
<a class="reply">Reply</a>
<div class="reply hidden">
<%= form_for(#reply) do |ff| %>
<%= ff.hidden_field :post_id, value: p.id %>
<%= ff.hidden_field :user_id, value: current_user.id %>
<div class="form-group col-xs-12">
<div class="row">
<div class="col-xs-11">
<%= ff.text_area :content, class: 'form-control', placeholder: "Write a reply..." %>
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-success" onclick="addpic(<%= p.id %>);">Add a pic</button>
</div>
</div>
</div>
<div class="form-group hidden col-xs-12" id="picture<%= p.id %>">
<%= ff.file_field :image, class: 'form-control' %>
</div>
<div class="form-group col-xs-12 actions">
<%= ff.submit %>
</div>
<% end %>
</div>
</div>
<% end %>
<% end %>
<script>
var addpic = function(id) {
var el = '#picture' + id;
$(el).removeClass('hidden');
}
$('.reply').on('click', function(e){
e.preventDefault();
$(this).next().removeClass('hidden');
});
</script>
Figured out the issue, JS was preventing the default for all clicks inside a .reply div. Had reply used in the wrapper for the reply form, so it was preventing the default. Got it working now by changing the class of the reply link to 'addreply' and the js listener to 'addreply' as well.

Resources