I am building an app with ruby on rails. The version of rails and ruby are as listed above.
My objective is to implement JavaScript/ Jquery into my mindmap index views so that they can dynamically add information into my page without having to reload the entire page. So their will be a link to be clicked on which will will display the mindmap form, on submission of the form, the form should disappear and the new data should be appended into the specified div views. --So far I have been able to display the mindmap form once I click on the new link but the form doesn't disappear on the submission of the form. I have been able to allow the user to enter and submit the information but the information only shows after i do a full page reload.
I have tried a number but nothing seems to help.
Someone Please help. If you live in Chicago, I owe you a beer :).
Below are the controller, js and views file.
class MindmapsController < ApplicationController
before_action :find_capsule, only: [:new, :index, :create, :show, :update, :destroy]
before_action :find_mindmap, only: [:show, :edit, :index, :update, :destroy]
before_action :authenticate_author!
def index
@mindmaps = Mindmap.all.order('created_at DESC')
end
def new
@mindmap = Mindmap.new
end
def create
@mindmap = Mindmap.new(mindmap_params)
if @mindmap.save
respond_to do |format|
format.html {redirect_to mindmaps_path , notice:"You have successfully created a Mindmap"}
format.js
end
else
render 'new'
end
end
def show
end
def edit
end
def update
if @mindmap.update(mindmap_params)
redirect_to @mindmap , notice:" You have successfully updated Your Mindmap"
else
render 'edit'
end
end
def destroy
@mindmap.destroy
redirect_to mindmaps_path
end
def find_mindmap
@mindmap = Mindmap.find_by(params[:id])
end
def find_capsule
@capsule = current_author.capsules.find_by(params[:id])
end
def mindmap_params
params.require(:mindmap).permit(:src, :src_purpose, :capsule_id, :profile_id)
end
end
JavaScript files are located under the mind map views folder as FYI
new.js.erb
$("a#new_mindmap_link").hide().after("<%= j render('form')%>");
create.js.erb
$("form#new_mindmap").remove();
$("a#new_mindmap_link").show();
$("div#mindmap").append("<%= j render(@mindmaps) %>");
Mindmap index page
Mindmaps#index
<%= link_to "Create Mindmap", new_mindmap_path, id:"new_mindmap_link", remote: true %>
<% @mindmaps.each do |mindmap| %>
<div class="mindmap">
<div>Source: <%= mindmap.src %></div>
<div class="meta">
<%= link_to time_ago_in_words(mindmap.created_at) + " ago" , mindmap %>
<span class="admin"><%= link_to "Edit", edit_mindmap_path(mindmap) %>
<%= link_to "Delete", mindmap , method: :delete, data: { confirm: "Are you sure you want to delete this Mindmap" } %>
</span>
</div>
</div>
<% end %>
Mindmap Form
<div class="col-md-4 mindmap-container">
<%= simple_form_for @mindmap, remote: true do |f| %>
<%= f.input :src, as: :url, class:'form-control', label: false, placeholder:"Add a URL" %>
<%= f.input :src_purpose, as: :text, class: 'form-control', label: false, placeholder: "Add a description to your URL"%>
<%= f.input :capsule_id, label: false, collection: Capsule.all, default: 'Select a capsule', label_method: :title %>
<%= f.input :profile_id, label: false, collection: Profile.all, default: 'Select a Profile ID',label_method: :profile_name %>
<%= f.button :submit, class:'btn btn-primary' %>
<% end %>
</div>