Rails rest in peace gem - how to use in each loop? - javascript

I am using the Rest in place gem: https://github.com/janv/rest_in_place.
How to use it correctly inside of an each loop? More specifically, for a belongs_to relationship ?
For Example, User has_many :fee_agreements and FeeAgreement belongs_to :user
How to utilize the edit in place feature to update the fee_agreement "loan_amount" here?
<tbody>
<% #user.fee_agreements.each do |fa| %>
<tr>
<td><%= fa.user_id %></td>
<td><%= fa.loan_id %></td>
<td id="<%= dom_id(fa) %>"><span class="rest-in-place" data-formtype="input" data-attribute="loan_amount"><%= fa.loan_amount %></span></td>
#above is the line in question
</tr>
<% end %>
</tbody>
Console output:
Started PUT "/users/51" for 127.0.0.1 at 2014-03-19 00:19:29 -0400
Processing by UsersController#update as JSON
Parameters: {"authenticity_token"=>"xxx=", "fee_agreement"=>{"loan_amount"=>"998988"}, "id"=>"51"}
I do have a separate controller for FeeAgreements which "builds" for the user..
def create
#fee_agreement = current_user.fee_agreements.build(:loan_id => params[:loan_id])
if #fee_agreement.save
redirect_to fee_agreements_path
else
redirect_to index_path
end
end

try this out:
instead of rest_in_place use Best in Place gem
after some initial setup as described in screencast you can use it by
<tbody>
<% #user.fee_agreements.each do |fa| %>
<tr>
<td><%= fa.user_id %></td>
<td><%= fa.loan_id %></td>
<td><%= beast_in_place fa.loan_amount %></span></td>
#above is the line in question
</tr>
<% end %>
</tbody>

Related

Automatic Serial Number Generator Using EJS Loop

I want to Generate Automatic Serial number for my Registered Admins
<tbody>
<% Admins.forEach(function (Admin){ %>
<tr>
<td>//S.No Here Starts from 1 //</td>
<td><%= Admin.First_Name %></td>
<td><%= Admin.Last_Name %></td>
<td><%= Admin.User_Name %></td>
<td><%= Admin.Email %></td>
<td><%= Admin.Password %></td>
<td><%= Admin.Contact %></td>
</tr>
<% }) %>
forEach passes the index as the second argument. It's zero-based so you'll need to add 1:
<% Admins.forEach(function (Admin, index) { %>
<tr>
<td><%= index + 1 %></td>

gem 'gon' - name error in Quotes#index, uninitialized constant ActionView::CompiledTemplates::Gon, Rails 5

I am trying to use the gem 'gon' but run into the above error, stating;
namer error in Quotes#index
Showing /Users/jamesbkemp/Code/QuoteEngine/app/views/layouts/application.html.erb where line #5 raised:
uninitialized constant ActionView::CompiledTemplates::Gon
Any idea what the issue may be and or any suggestoins on alternative gems to make passing controller variables into javascript easier?
Thanks
application.html.erb, note line 5;
<!DOCTYPE html>
<html>
<head>
<title>QuoteEngine</title>
<%= Gon::Base.render_data %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= render "layouts/navbar" %>
<% flash.each do |key, value| %>
<div class="alert alert-info alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
</body>
</html>
quotes_controller.rb
class QuotesController < ApplicationController
before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]
# before_action :owners_only, only: [ :show, :edit, :index, :update, :destroy ]
def new
#quote = Quote.new
end
def create
# #quote = Quote.new(quote_params)
#quote = current_user.quotes.new(quote_params)
if #quote.save
redirect_to quote_url(#quote), notice: 'Quote request created'
else
render :new
end
end
def show
# #quote = Quote.find(params[:id])
#quote = current_user.quotes.find(params[:id])
gon.gon_quote_id = #quote.id
end
def index
#quotes = current_user.quotes.all
end
def edit
#quote = current_user.quotes.find(params[:id])
end
def update
#quote = current_user.quotes.find(params[:id])
if #quote.update_attributes(quote_params)
redirect_to quote_path(#quote)
else
render :edit
end
end
def destroy
#quote = current_user.quotes.find(params[:id])
#quote.destroy
redirect_to quotes_path
end
private
def quote_params
params.require(:quote).permit(:gla, :prev_cover, :co_name, :co_number, :postcode, :industry, :lives_overseas,
:scheme_start_date, :payment_frequency, :commission_level)
end
end
_quote.rb partial;
<% #quote = local_assigns[:quote] %>
<section id="quotes">
<div class="container">
<div class="row">
<div class="col-md-4 text-center">
<div class="panel panel-success panel-quote link-panel">
<div class="panel-heading">
<strong>GLA</strong>
</div>
<div class="panel-body text-center">
<p><strong>Quote ID; <%= #quote.id %></strong></p>
</div>
<table class="table table-bordered">
<tr>
<td>Company name</td>
<td><%= #quote.co_name %></td>
</tr>
<tr>
<td>Company number</td>
<td><%= #quote.co_number %></td>
</tr>
<tr>
<td>Office postcode</td>
<td><%= #quote.postcode %></td>
</tr>
<tr>
<td>Industry</td>
<td><%= #quote.industry %></td>
</tr>
<tr>
<td>Previous cover</td>
<td><%= #quote.prev_cover %></td>
</tr>
<tr>
<td>Lives overseas</td>
<td><%= #quote.lives_overseas %></td>
</tr>
<tr>
<td>Scheme start date</td>
<td><%= #quote.scheme_start_date %></td>
</tr>
<tr>
<td>Payment frequency</td>
<td><%= #quote.payment_frequency %></td>
</tr>
<tr>
<td>Commission level</td>
<td><%= #quote.commission_level %></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</section>
index.html.erb
<% #quotes.each do |quote| %>
<%= render :partial => "quote", locals: {quote: quote} %>
<% end %>
I would like to help with your case since I'm also using gon in rails 5 and it's working
after run bundle install,
open app/views/layouts/application.html.erb
and put under tag body <%= Gon::Base.render_data %>
do some test with open your your controller and create gon data (for example below I gave you with 10 users data
gon.users = User.limit(10)
open your coffee and test with log
alert gon.users
and don't forget to restart your rails server command

How to apply a color to <td> tag in .ejs file in nodeJS

I want to know how to apply a color in .ejs file. Here is the code.
<% resultList.forEach(function(item, index){ %>
<tr>
<td><%= item.function %></td>
<td><%= item.value %></td>
<td><%= item.content %></td>
</tr>
<% }); %>
if(item.value == false) //I want to apply red color
How can I do that? Thank you.
Would something like this work?
<% resultList.forEach(function(item, index){ %>
<tr>
<td><%= item.function %></td>
<% if (item.value === false) { %>
<td style='color:red;'><%= item.value %></td>
<% else %>
<td><%= item.value %></td>
<% } %>
<td><%= item.content %></td>
</tr>
<% }); %>
Note personally I think this is ugly. I would try to keep more code out of template side and would also use a CSS class for the coloring, but it was easier to illustrate this way.

Iterate over an array by index when click on submit button Ruby on Rails

I have an array of Pet instances and each of them have a name, type, sex and size, etc... I'd like to iterate through this collection of Pet instances and display the each one by one.
This is what I have so far:
<% #pets.each do |pet| %>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Sex</th>
<th>Age</th>
<th>breed</th>
<th>Size</th>
<th>Picture</th>
<th>Description</th>
<th>Shelter_id</th>
<th>Shelter</th>
<th>Phone</th>
<th>Email</th>
<th>City</th>
<th>Zip</th>
<th>Like</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= pet.name %></td>
<td><%= pet.species %></td>
<td><%= pet.sex %></td>
<td><%= pet.age %></td>
<td><% pet.breeds.each do |breed| %>
<li><%= breed.name %></li>
<%end%>
</td>
<td><%= pet.size %></td>
<td><%= image_tag pet.picture %></td>
<td><%= pet.description %></td>
<td><%= pet.shelter_id %></td>
<% shelter = Shelter.find(pet.shelter_id)%>
<td><%= shelter.name %></td>
<td><%= shelter.phone %></td>
<td><%= shelter.email %></td>
<td><%= shelter.city %></td>
<td><%= shelter.zip %></td>
<td><%= form_for pet, :url => { :controller => "favorite_pets", :action => "create" }, :html => {:method => :post} do |f| %>
<%= f.text_field :id %>
<%= f.submit %>
<% end %>
</td>
</tr>
</tbody>
</table>
<%end%>
So now, I need two thinks.
1st
When a user clicks the submit button (<%= f.submit %>) to send a POST request i do not want the page to reload nor to redirected me to another page. I looked around and it seems like :remote => true included in the form_for would do the trick but i'd need more help with that.
2nd
I'd like to control the iterating through #pets so that only the first one is displayed pet[0] and every time I click on the <%= f.submit %> button not only does it not reload the page but it displays the next index in the array pet[1]
I think I need to set up a sort of counter which would increment the index of the pet array every time a user click on the <%= f.submit %> button.
Any guiding thoughts?
If I were to do this I would use some javascript. So your submit button turns into more of an "Add Button" it doesn't actually submit anything. It would just add that object (in your case the pet, along with its info) to a javascript array as well as display the new object (again the pet along with the info) on the window. Then create another button to actually submit all the pets. (The javascript array)

Rails jQuery sortable unable to save

I'm trying to make my table sortable (drag and drop) using jQuery, the problem I'm having is saving the new sorted table and then publishing it to other users or when the browser is refreshed.
I'm using:
gem 'acts_as_list'
Rails 3.2.17
ruby 1.9.3p484
Is there a better way of doing this?
my sort and show method on my controller
def sort
#episodes = current_user.episodes_for(#show).each do |episode|
episode.position = params['episode'].index(episode.id.to_s) + 1
episode.save
end
render :nothing =>true
end
def show
#episodes = current_user.episodes_for(#show)
#episodes.order('episode.position ASC')
#episode = #episodes.where(id: params[:id]).first
end
My Javascript
<script>
$(window).load(function() {
$("#sortthis").sortable({
axis: 'y',
placeholder: 'ui-state-highlight',
cursor: 'move',
dropOnempty: false,
scroll: true,
opacity: 0.4,
update: function(event, ui){
var itm_arr = $("#sortthis").sortable('toArray');
var pobj = {episodes: itm_arr};
$.post("/episodes/sort", pobj);
}
});
});
</script>
The table I wanna sort in views:
<tbody id='eps'>
<tr>
<th>Title</th>
<th>#</th>
<th>Season</th>
<th>Download?</th>
<th>Shared?</th>
<th>Length</th>
<th>Status</th>
<th></th>
</tr>
<% for episode in #episodes %>
<tr>
<td id="eps_<%= episode.id %>", width="20%"><%=h episode.title %></td>
<td id="eps_<%= episode.id %>"><%=h episode.number %></td>
<td id="eps_<%= episode.id %>"><%=h episode.season %></td>
<td id="eps_<%= episode.id %>"><%=h episode.is_downloadable %></td>
<td id="eps_<%= episode.id %>"><%=h episode.shared_with_dev %></td>
<td id="eps_<%= episode.id %>"><%=h episode.length %></td>
<td>
<% if episode.video %>
<%= link_to 'Replace Video', new_show_episode_video_path(episode.show, episode) %>
<% else %>
<%= link_to 'Upload Video', new_show_episode_video_path(episode.show, episode) %>
<% end %>
</td>
<td>
<%= link_to "View", [episode.show, episode] %>
<%= link_to "Edit", edit_show_episode_path(episode.show, episode) if permitted_to? :update, episode %>
<%= link_to "Delete", show_episode_path(episode.show, episode), :confirm => 'Are you sure?', :method => :delete if permitted_to? :delete, episode %>
</td>
</tr>
<% end %>
any help will be greatly appreciated!
I figured it out, made the following changes:
Views:
<tr id="<%= episode.id %>">
and new controller
def sort
Episode.all.each do |e|
if position = params[:episodes].index(e.id.to_s)
e.update_attribute(:position, position + 1) unless e.position == position + 1
end
end
render :nothing => true, :status => 200
end
finally added this to my model:
acts_as_list
default_scope order('position')
let me know if anyone's having similar issues.

Categories

Resources