event-calendar rails 4 doesn't work - javascript

I'm tryong to use event_calendar gem and i follow this 3 guides
http://andolasoft.wordpress.com/2013/01/09/how-to-implement-event-calendar-in-rails-app/
https://github.com/elevation/event_calendar#static-files
https://github.com/elevation/event_calendar/wiki/Quickstart-from-scratch
I think that my problem is about the including of the stylesheet and of the javascript.
I can't understand the step of including because each guides say something different.
Heres the error
TypeError in Calendar#index
Showing /home/user/work/aptana_1/spree_calendar/app/views/calendar/index.html.erb where >line #6 raised:
no implicit conversion of nil into Hash
Extracted source (around line #6):
<h1>Calendar</h1>
<%= event_calendar %>
>Rails.root: /home/user/work/aptana_1/spree_calendar
>
>Application Trace | Framework Trace | Full Trace
>app/helpers/calendar_helper.rb:24:in `event_calendar'
>app/views/calendar/index.html.erb:6:in >`_app_views_calendar_index_html_erb___345437905_81942130'
>Request
>
>Parameters:
>
>{"year"=>"2014",
> "month"=>"07"}
and this is the calendar_helper file
module CalendarHelper
def month_link(month_date)
link_to(I18n.localize(month_date, :format => "%B"), {:month => month_date.month, :year => month_date.year})
end
# custom options for this calendar
def event_calendar_opts
puts("EVENTCALENDAROP")
{
:year => #year,
:month => #month,
:event_strips => #event_strips,
:month_name_text => I18n.localize(#shown_month, :format => "%B %Y"),
:previous_month_text => "<< " + month_link(#shown_month.prev_month),
:next_month_text => month_link(#shown_month.next_month) + " >>"
}
puts"###{#year}"
puts"###{:year.to_s}"
end
def event_calendar
# args is an argument hash containing :event, :day, and :options
puts "EVENT_CALENDAR"
calendar event_calendar_opts do |args|
event = args[:event]
%(#{h(event.name)})
end
end
end
and here is the view
<!-- Probably move the stylesheet to you layout. Also make sure you include the javascript. -->
<%= stylesheet_link_tag "event_calendar" %>
<h1>Calendar</h1>
<%= event_calendar %>
and here is the application layout
<!DOCTYPE html>
<html>
<head>
<title>SpreeCalendar</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true ,'event_calendar'%>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>

the problem is within this method:
def event_calendar_opts
puts("EVENTCALENDAROP")
{
:year => #year,
:month => #month,
:event_strips => #event_strips,
:month_name_text => I18n.localize(#shown_month, :format => "%B %Y"),
:previous_month_text => "<< " + month_link(#shown_month.prev_month),
:next_month_text => month_link(#shown_month.next_month) + " >>"
}
puts "###{#year}"
puts "###{:year.to_s}"
end
I believe the two calls to puts at the end are debugging output, but they change the return value of #event_calendar_opts. You should remove the puts or move them to the top:
def event_calendar_opts
puts("EVENTCALENDAROP")
puts "###{#year}"
puts "###{:year.to_s}"
{
:year => #year,
:month => #month,
:event_strips => #event_strips,
:month_name_text => I18n.localize(#shown_month, :format => "%B %Y"),
:previous_month_text => "<< " + month_link(#shown_month.prev_month),
:next_month_text => month_link(#shown_month.next_month) + " >>"
}
end

Related

Error in Rails 5 rendering form in partial: " undefined method `simple_fields_for' for "#<SimpleForm::FormBuilder "

This is a continuation from here:
Passing a form as a local to a ajax rendered partial in Rails 5
I've looked all over and can't find a solution that works.
Relevant Controller (profits_controller.rb):
def new_tabs
#market = Market.order('mjsnumber').all.first
#profit = Profit.new
profit_types_markets_products
end
def fetch_market
#market = Market.where(:id => params[:market_id]).first
#form = params["form"]
respond_to do |format|
format.js { render layout: false}
end
end
Relevant View (new_tabs.html.erb):
<%= simple_form_for #profit, :remote => true do |form| %>
<% #markets.each_with_index do |market, i| %>
<%= link_to market.nick, fetch_market_path(:market_id => market.id, :form => form, profit: #profit), :remote=>'true', :id => 'navBtn' + market.id.to_s, :class => 'd-flex flex-grow-1 align-content-center text-center nav-item nav-link ' + active(i).to_s + profit_nav_font_color(market.color).to_s, "data-toggle" => "pill", "roll" => "tab", "style" => "background-color: " + market.color.to_s + ";", remote: true %>
<% end %>
<%= render :partial => 'edit_partial_form', locals: { market: #market, form: form, profit: #profit } %>
Relevant Partial (_edit_partial_form.html.erb):
<%= market.namae %>
<%= form.simple_fields_for :figures, :defaults => { :input_html => { :class => "floatTextBox" }}, remote: true do |figures_form| %>
<%= figures_form.input "[test]" %>
<% end %>
Relevant JS (fetch_market.js.erb):
$("#edit_partial_form").html("<%= escape_javascript(render partial: 'edit_partial_form', locals: { market: #market, form: #form, profit: #profit } ) %>");
Routes:
resources :profits do
resources :markets
resources :products
collection do
get 'new_by_product_type'
get 'new_tabs'
end
end
get "/fetch_market" => 'profits#fetch_market', as: 'fetch_market'
patch 'profits/:id/autosave', as: :autosave_profit, to: 'profits#autosave'
It renders the partial fine, and the links appear to contain the FormBuilder information. When I click the link and add a "puts params" to the controller, it shows the params there. But then gives me an error when loading the partial in console:
ActionView::Template::Error (undefined local variable or method `form' for #<#<Class:0x00007fdbd6453648>:0x00007fdbd68db5f8>
Did you mean? fork):
1: $("#edit_partial_form").html("<%= escape_javascript(render partial: 'edit_partial_form', locals: { market: #market, form: form, profit: #profit } ) %>");
Full Error from local environment when I click on the link for the second market in the list of generated markets:
Processing by ProfitsController#fetch_market as JS
Parameters: {"form"=>"#<SimpleForm::FormBuilder:0x00007fed50dba9f8>", "market_id"=>"2"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /home/mudl/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Market Load (0.3ms) SELECT "markets".* FROM "markets" WHERE "markets"."id" = $1 ORDER BY "markets"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/profits_controller.rb:75
Rendering profits/fetch_market.js.erb
Rendered profits/_edit_partial_form.html.erb (6.5ms)
Rendered profits/fetch_market.js.erb (7.5ms)
Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.6ms)
ActionView::Template::Error (undefined method `simple_fields_for' for "#<SimpleForm::FormBuilder:0x00007fed50dba9f8>":String):
11: MJS 番後: <%= market.mjsnumber %>
12: </span>
13: </div>
14: <%= form.simple_fields_for :figures, :defaults => { :input_html => { :class => "floatTextBox" }}, remote: true do |figures_form| %>
15: <%= figures_form.input "[test]" %>
16: <% end %>
17:
app/views/profits/_edit_partial_form.html.erb:14:in `_app_views_profits__edit_partial_form_html_erb__249336059151108365_70328546926840'
app/views/profits/fetch_market.js.erb:1:in `_app_views_profits_fetch_market_js_erb___2023159631102932010_70328546933900'
app/controllers/profits_controller.rb:78:in `block (2 levels) in fetch_market'
app/controllers/profits_controller.rb:77:in `fetch_market'
(undefined method `simple_fields_for' for "#<SimpleForm::FormBuilder
I am removing the simple_form tag because after a simple test changing the simple_form to a regular form, the error persists with whatever method I call after field_for in the partial.
Thanks in advance.
EDIT:
So I ended up giving up and just using the #profit variable to make a new form in the partial (not ideal, but I'll hack it). Still curious if this is even possible, though, so I'll leave the question open..
So first of all you need to close your form block, then if you couldn`t pass form to partial then try to do this without partial
<%= simple_form_for #profit, remote: true do |form| %>
<% #markets.each_with_index do |market, i| %>
<%= link_to market.nick, fetch_market_path(market_id: market.id, form: form, profit: #profit), id: 'navBtn' + market.id.to_s, class: 'd-flex flex-grow-1 align-content-center text-center nav-item nav-link ' + active(i).to_s + profit_nav_font_color(market.color).to_s, "data-toggle" => "pill", "roll" => "tab", "style" => "background-color: " + market.color.to_s + ";", remote: true %>
<%= market.name %>
<% end %>
<%= form.simple_fields_for :figures, :defaults => { :input_html => { :class => "floatTextBox" }}, remote: true do |figures_form| %>
<%= figures_form.input "[test]" %>
<% end %>
<% end %>
And better to used something_key: "value" instead of this syntax :something_key => "value"

Rails: Paperclip gem no longer works after switching computers

I switched from a Sony computer to an Asus, downloaded the same programs as I had on the Sony and cloned my Rails 4.0.10 project from Bitbucket. Everything else behaves the same, but Paperclip has inexplicably stopped working, though the two development environments should be exactly the same. I used Paperclip to add an avatar to my User model, but now User.create fails when I attach an avatar. I tried uninstalling and reinstalling it, but that made no difference. Does anyone with Paperclip experience know what might have happened?
Gemfile:
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
# ...
models/user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :large => "500x500", :medium => "300x300>", :thumb => "50x50!" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
# ...
controllers/users.rb
class UsersController < ApplicationController
def create
# ...
if #user.save
render :action => "crop"
else
flash[:notice] = "Failed"
redirect_to new_user_path
end
end
private
def user_params
params.require(:user).permit(:name, :avatar)
end
views/users/new.html.erb
<%= form_for #user, :url => users_path, :html => { :multipart => true } do |f| %>
<%= f.text_field :name, :placeholder => "Name" %>
<br>
<%= f.label :display_picture %>
<%= f.file_field :avatar %>
<br>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
views/users/crop.html.erb
<% content_for(:head) do %>
<%= stylesheet_link_tag "jquery.Jcrop" %>
<%= javascript_include_tag "jquery.Jcrop.min" %>
<script type="text/javascript" charset="utf-8">
ready = $(function() {
$("#cropbox").Jcrop();
});
</script>
<% end %>
$(document).ready(ready);
$(document).on('page:load', ready);
<%= image_tag #user.avatar.url(:large), :id => "cropbox" %>
Make sure imagemagick is installed on your machine. This isn't something 'bundle install' will cover.
Follow the instructions here
http://www.imagemagick.org/script/install-source.php
Or use homebrew or other package managers

Rails controller and javascript

So I have a form that adds codes to an appointment in rails. There are 2 types of codes, regular codes that get added straight to the apt and wildcard codes that return a list of codes that contain the query (ex. 800* returns anything with 800 in it).
What I need to do is have any regular codes get added to the appointment and return the appointment page if no wildcard codes are selected. If wildcard codes are selected, I need to render a partial that toggleslides out with the search results. If both wildcard and regular codes are selected I need the toggleslide but also have the regular codes updated on the codes table on the page.
The problem I'm having is when just regular codes are selected, the toggleslide is still being called and populated with the appointment page in it.
I have the form that does this set to remote and in a js file I have the ajax response do the toggleslide and populate it with the response. Is there a way to override the ajax response in the controller and just redirect to the appointment page if no wildcard codes are selected?
Or is there a better way to do all this? I tried using a js template but keep getting a 406 error. Or I fix the 406 error by removing any respond_to calls but the toggleslide doesn't get called.
In my js file in my assets.
$(document).ready(function() {
$("#custom_codes_form_1").on("ajax:success", function(e, data, status, xhr) {
$('#my_lists_modal_1').hide();
$('#search_code_results').html(data);
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#search_code_results').show('blind', {direction: "left"});
});
return false;
});
In my controller. #all_codes is the list of codes returned on a search query.
This is in the add_code_list_codes method:
if !#all_codes.nil?
render :partial => "/encounter/code_search_results", :content_type => 'text/html', :locals => {:codes => #codes, :apt => #apt, :all_codes => #all_codes, :diagnoses_search => true, :type => #type}
else
respond_to do |format|
format.html {redirect_to encounter_show_path(:id => #apt.id)}
end
end
Here's the form.
<%= form_tag({:controller => :encounter, :action => :add_code_list_codes, :id => #apt.id}, :class => 'form-horizontal', :id => "custom_codes_form_#{cl.id}", :remote => true ) do %>
<% CodeType.by_name.all.each do |ct| %>
<h4 class="small_bottom_margin"><%= ct.display_name %></h4>
<div class="control-group">
<% cl.code_list_items.all_by_code_type(ct.id).each do |cc| %>
<%= label_tag :codes, :class => 'checkbox' do %>
<% if cc.code %>
<%= check_box_tag 'codes[]', cc.id %> <strong><%= cc.code.code %></strong> - <%= cc.name_override ? cc.name_override : cc.code.name %>
<% else %>
<%= check_box_tag 'codes[]', cc.id %> <strong><%= cc.search_criteria %></strong> - Wildcard
<% end %>
<% end %>
<% end %>
</div>
<% end %>
<%= submit_tag("Add Selected Charges", :class => 'btn', 'data-disable-with' => "Adding Charges") %>
<% end %>

JS script is not working on create and delete actions

I am implementing 'acts_as_commentable_with_threading' and the js script is not working with the delete or create actions. I have to reload the page for my comments to be added and removed from the page.
I have placed my 'comments.js.coffee' file in the 'app/assets/javascript' directory. It looks like the following:
#comments.js.coffee
jQuery ->
# Create a comment
$(".comment-form")
.on "ajax:beforeSend", (evt, xhr, settings) ->
$(this).find('textarea')
.addClass('uneditable-input')
.attr('disabled', 'disabled');
.on "ajax:success", (evt, data, status, xhr) ->
$(this).find('textarea')
.removeClass('uneditable-input')
.removeAttr('disabled', 'disabled')
.val('');
$(xhr.responseText).hide().insertAfter($(this)).show('slow')
# Delete a comment
$(document)
.on "ajax:beforeSend", ".comment", ->
$(this).fadeTo('fast', 0.5)
.on "ajax:success", ".comment", ->
$(this).hide('fast')
.on "ajax:error", ".comment", ->
$(this).fadeTo('fast', 1)
#In Comments controller file I wrote:
def create
#comment_hash = params[:comment]
#obj = #comment_hash[:commentable_type].constantize.find(#comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
#comment = Comment.build_from(#obj, current_user, #comment_hash[:body])
if #comment.save
render :partial => "comments/comment", :locals => { :comment => #comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
def destroy
#comment = Comment.find(params[:id])
if #comment.destroy
render :json => #comment, :status => :ok
else
render :js => "alert('error deleting comment');"
end
end
end
# In Catalogs controller the code is:
def find_user
#member = Member.find(params[:id])
end
def show_user
find_user
#comments = #member.comment_threads.order('created_at desc')
#new_comment = Comment.build_from(#member, #member.id, "","")
respond_to do |format|
format.html # show.html.erb
format.json { render json: #member }
end
end
#In Comments model, the code include:
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
attr_accessible :commentable, :body,:member_id,:sender
validates :body, :presence => true,:length=>{:maximum=>1000}
validates :member, :presence => true,:length=>{:maximum=>200}
validates :sender,:presence=> true,:length=>{:maximum=>200}
belongs_to :commentable, :polymorphic => true
# NOTE: Comments belong to a user
belongs_to :member
def self.build_from(obj, member_id, comment,sender)
new \
:commentable => obj,
:member_id => member_id,
:body => comment,
:sender => sender
end
...................
# In Member model, I add the following lines:
acts_as_commentable
has_many :comments, :dependent=>:destroy
# In the view file 'show_user', I wrote:
<div class='comments'>
<%= render :partial => 'comments/form', :locals => { :comment => #new_comment }%>
<%= render :partial => 'comments/comment', :collection => #comments, :as => :comment %>
</div>
#Partial 'comments/comment' contains:
<div class='comment'>
<hr>
<b><%= comment.sender %> </b>
<small><%= comment.updated_at%></small> <%= link_to "×",comment_path(comment), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment from # {comment.sender}?", :disable_with => "×", :class => 'close'%>
<p><%= comment.body.html_safe%></p>
</div>
# In _form.html.erb file:
<div class='comment-form'>
<%= simple_form_for comment, :remote => true do |f|%>
<small>Leave your comment </small><br/>
<%= f.input :sender,:as => :hidden,:input_html => { :rows => "2", :value =>"# {current_user.login}"}, :label => false %>
<%= f.input :body, :input_html => { :rows => "2", :class =>"tinymce"}, :label => false %>
<%= f.input :commentable_id, :as => :hidden, :value => comment.commentable_id %>
<%= f.input :commentable_type, :as => :hidden, :value => comment.commentable_type %>
<%= f.button :submit, :class => "btn btn-primary", :disable_with => "Submitting…" %>
<%end%>
</div>
In config/routes file:
resources :comments, :only => [:create, :destroy]
#Here is the output I get if I click on to 'create comments' button:
I hope someone will help me. Thank you.
I **deleted** the jquery.min.js file from my app/assets/javascripts directory
and in my gem file I added the line : gem 'jquery-rails'
and used 'bundle install' to use the gem in my app.
The application.js file has three lines:
//= require jquery
//= require jquery_ujs
//= require_tree .
And in my layout.html.erb file I have the line:
<%= javascript_include_tag 'application' %>
I know it is pretty obvious. Sometimes you need to get things wrong
to understand the obvious better. Now I can add and remove
messages using AJAX and js files.

Rails jQuery pageless problem

Here's the deal,
I'm trying to solve why pageless (infinite scroll) isn't rendering. Using this jQuery Plugin
I have my index page of products with pagination by will_paginate:
def index
#products = Product.find(:all, :order => "position DESC").paginate(:per_page => 3, :page => params[:page])
if request.xhr?
render :partial => 'shared/products', :object => #products
end
end
My index page is calling the products partial
<%= render "shared/products", :object => #products %>
Which in turn is passing the collection onto a singular product partial
<%= render 'shared/product', :collection => #products %>
And calling pageless on it if javascript is enabled (otherwise will_paginate steps in.
<%= pageless(#products.total_pages, products_path) %>
-----------
def pageless(total_pages, url=nil, container=nil)
opts = {
:totalPages => total_pages,
:url => url,
:loaderMsg => 'Loading more results'
}
container && opts[:container] ||= container
javascript_tag("$('#results').pageless(#{opts.to_json});")
end
And finally here is the code for the singular product partial
<% #products.each do |product| %>
<li id="product_<%= product.id %>">
<%= link_to product_image(product), product %>
</li>
<% end %>
Now when I load the page, there are no js errors, I can see the pages getting loaded in my server logs and yet nothing is rendering. Everything seems to work perfectly and yet it doesn't render. Any help would be appreciated.
-Scott
The products weren't being appended to the html. Turned out to be a class/id issue in the html. For anyone trying to debug something similar, see Jonathan Tran's comment on the question.
i changed few things to make it work for rails3:
index.html.erb
<%= render :partial => 'blah/fooPartial' , :locals => { :foo => #foo} %>
<%= will_paginate(#foo) %>
<%= pageless(#foo.total_pages, #path ) %>
My FooController:
def index
#foo = Foo.paginate(:page => params[:page], :per_page => 10).
order("created_at DESC")
respond_to do |format|
format.js
format.html # index.html.erb
end
end
My index.js.erb
$('.row').last().after("<%= escape_javascript( render :partial => 'blah/fooPartial' , :locals => { :foo => #foo}).html_safe %>");
Also Changed the ajax datatype at the very end from 'html' to 'script' in jquery.pageless.js as suggested by ahuang
Hope this helps!!!cheers!!!

Categories

Resources