How to edit the bootstrap.js file of gem twitter-bootstrap-rails - ruby-on-rails

I know this is definitely wrong, but I cannot seem to get sub menu links to work on mobile devices, and other posts suggest edditing the bootstrap.min.js file.
However, since I am using the gem "twitter-bootstrap-rails" I do not know where the file is and even if it would work to change it.
Ideas?
This is my code, omitted some parts.
<%= nav_bar :fixed => :top, :brand => image_tag('logo.png'), :responsive => true do %>
<% if user_signed_in? %>
<%= menu_group :pull => :right do %>
<%= drop_down "Scan" do %>
<%= menu_item "Android", 'http://sasfad' %>
<%= menu_item "iPhone", 'zxing://asfasdf' %>
<% end %>
<% end %>
<% end %>

You can't and you shouldn't edit those files. But you can fork the gem, edit the bootstrap.min.js file and use your forked gem in your Gemfile.
My recommendation is not to use the twitter-bootstrap-rails gem as the only thing it does is adding for you the assets to the asset pipeline.

Related

Rails Simple Calendar Gem Error

I am trying to get simple_calendar gem to work in my rails application. but keep getting an "undefined method `has_key?' for nil:NilClass"
Running Rails 5.0.0
Gemfile -> gem "simple_calendar", "~> 2.0"
Ran "bundle install" command, restarted application
Screenshot of error --> http://imgur.com/a/umAQH
Pastebin of controller and view page -> http://pastebin.com/8WX4dZNs
Thanks in advance!
As per the documentation, the calendar helper takes a parameter which is a Hash. In your code snippet you've passed in a collection of events (#events), not a Hash of options.
If you want to pass the events in, the documentation shows how to do it.
<%= month_calendar events: #meetings do |date, meetings| %>
<%= date %>
<% meetings.each do |meeting| %>
<div>
<%= meeting.name %>
</div>
<% end %>
<% end %>
Source: https://github.com/excid3/simple_calendar#rendering-events
So in your case, you'll want to pass your #events collection in as a key value pair like so: <%= calendar events: #events do %>.
I think you need to add the start_time alias definition inside the model you use for your events, here I have this inside my app/models/evento.rb
def start_time
self.fecha
end
Here 'fecha' is the start date for my event. Then you have to manipulate your events as Carlos Ramirez explains, so you have to have something like this inside your 'calendar.html.erb':
<h3>Eventos (<%= #eventos.count %>)</h3>
<%= month_calendar events: #eventos do |date, eventos| %>
<%= date.day %>
<% eventos.each do |evento| %>
<div>
<%= link_to evento.title, evento %>
</div>
<% end %>
<% end %>
That should render the link to your event.
Copied files from https://github.com/excid3/simple_calendar/tree/master/lib directory into my applications lib directory
edited my calendar.html.erb file
<%= month_calendar do |date| %>
<%= date %>
<% end %>
Restarted server and got a calendar displaying

client side validation not loading

I am following the rails cast 263 on client side valuation. I have the code in place, though it is not validating the errors live for me. The errors only show after submitting the form.
application.html.erb:
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag :defaults, "rails.validations" %>
<%= csrf_meta_tags %>
client_side_validations.rb:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
else
%{<div class="field_with_errors">#{html_tag}</div>}.html_safe
end
end
form.html.erb:
<%= form_for #user, :validate => true do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
</div>
application.js:
//= require jquery
//= require jquery.ui.all
//= require jquery_ujs
//= require jquery.magnific-popup.js
//= require jquery.purr
//= require rails.validations
//= require best_in_place
//= require_tree .
Original Suggestion: Link to JS in application.js.
Rather than linking to the rails.validations.js from application.html.erb:
<%= javascript_include_tag :defaults, "rails.validations" %>
You may want to do this in your app/assets/javascripts/application.js file:
//= require rails.validations
That's how I connect into additional JavaScript files.
I don't know much about Rails 2, but I think that there was a shift in Rails 3 so that now application.html.erb is used to link to application.js and then all other JS files are linked to from there. It looks like :default might not work how it used to. RailsCasts are super helpful but some of the episodes that were created a few years ago have code samples that have changed in recent versions of Rails.
Edit 1: Make sure rails.validations.js was installed into app/assets/javascripts.
In the RailsCast the rails g client_side_validations:install command puts rails.validations.js into the project. Apparently this doesn't always work and you can call rails g client_side_validations:copy_assets to get it dropped into app/assets/javascripts (See: Can't include js file for client slide validation).
Edit 2: In Rails 4, fix compatibility bugs.
It looks like client_side_validations doesn't work in Rails 4 and the gem is no longer maintained (See: https://github.com/bcardarella/client_side_validations).
In Rails 3, in action_view\helpers\form_helper.rb, the apply_form_for_options! method is:
def apply_form_for_options!(object_or_array, options)
In Rails 4, it is:
def apply_form_for_options!(record, object, options)
If you look at the client_side_validations code, its version is:
def apply_form_for_options!(object_or_array, options)
super
options[:html][:validate] = true if options[:validate]
end
That matches the Rails 3 definition. We only get two arguments but then we call super which blows up because Rails 4 expects three arguments.
If you are feeling brave, you could fork the client_side_validations gem and work through these Rails-4-compatibility issues. But I would suspect that there might be quite a few pieces to update since this gem had to be so tightly integrated with the FormHelper.
Sorry that doesn't immediately solve your problem. Good luck!

Paperclip displaying multiple 'missing image' tags on edit page

So I have an object that has multiple photos (through use of a model called asset). I'm using paperclip to upload the photos and it's working fine. On my show page I call
<li class="grid_3">Photos:<br /></li>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
and the photos display perfectly. when I click on the edit page:
<%= f.simple_fields_for :assets do |asset| %>
<%= render :partial => 'asset_fields', :locals => { :f => asset } %>
<% end %>
the partial is rendered:
<div class='nested-fields'>
<%= f.file_field :photo %>
<% unless f.object.new_record? %>
<%= f.file_field :photo %>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
<% end %>
</div>
at this point the photos each show up 3 times along with an extra image tag with a 'missing' photo placeholder. I would say I've tried different things but I just don't see what is wrong with the code. Any ideas?
Assuming simple_fields_for behaves the same as fields_for... the call to f.simple_fields_for :assets is a loop already. It loops over f.object.assets and renders the contents of the block once for each item found using the asset form generator (it may be a little less confusing to call that block variable asset_form, by the way). So since you're then rendering a partial that also renders all of the assets for the #address that's why you get each image 3 times.

Kaminari pagination layout is broken

I have a trouble with kaminari pagination and bootstrap styles.
Pagination works fine, but styles are broken.
Is there any way to fix it? or is there any other style sheets for kaminari pagination?
here is a screenshot http://imageshack.us/photo/my-images/59/kaminari.png/
Browser Google Chrome
= paginate #activities, :theme => 'twitter-bootstrap'
You can also run
rails g kaminari:views bootstrap
This might be due to missing i18n translations -- they create extra tags which messes up bootstrap pagination.
checkout https://github.com/gabetax/twitter-bootstrap-kaminari-views
I've had the same problem and after some experimentation I ended up with this in my gem file;
gem "kaminari", "~> 0.13.0"
gem "kaminari-bootstrap", "~> 0.1.2"
The page links are working the way I think they are supposed to, although I dont like the way they look.
I get an error message if I use Kaminari 0.14.0, so back it off to 0.13.0.
Also...
I noticed it messes things up if I generate Kaminari views. It is the most stable to use the views that come with the gem, if you can live with them.
If you're using Bootstrap 3 you'll need to do the following. In the console:
rails g kaminari:views bootstrap
Then, replace the contents of app/views/kaminari/_pagination.html.erb with the following:
<%= paginator.render do %>
<ul class="pagination">
<%= first_page_tag unless current_page.first? %>
<%= prev_page_tag unless current_page.first? %>
<% each_page do |page| -%>
<% if page.left_outer? || page.right_outer? || page.inside_window? %>
<%= page_tag page %>
<% elsif !page.was_truncated? %>
<%= gap_tag %>
<% end %>
<% end %>
<%= next_page_tag unless current_page.last? %>
<%= last_page_tag unless current_page.last? %>
</ul>
<% end %>
Replace app/views/_first_page.html.erb with the following (note the change to the class of li and from link_to_unless to link_to):
<li <%= "class=disabled" if current_page.first? %>>
<%= link_to raw(t 'views.pagination.first'), url, :remote => remote %>
</li>
Also replace the contents of _prev_page, _last_page, and _next_page in a similar fashion (you'll just need to change "current_page.first?" to "current_page.last?"

acts_as_list - Not dragging!

I followed several tutorial to make this work, but I can't get draggable items.
Here's what i did:
added gem 'acts_as_list' in my GemFile and did a bundle install
added <%= javascript_include_tag :defaults %> to my application.html.erb
added an :integer column named position to my facilities;
added acts_as_list in my facility model;
added the sort method in my facility model:
def sort
#facilities = Facility.all
#facilities.each do |f|
f.position = params['f'].index(f.id.to_s) + 1
f.save
end
end
added this in my view:
<ul id="facilities">
<% for facility in Facility.all %>
<% content_tag_for :li, facility do %>
<span class="handle">[drag]</span>
<%= link_to h(facility.name), facility %>
<% end %>
<% end %>
</ul>
<%= sortable_element("facilities", :url => sort_facilities_path, :handle => "handle") %>
added this to the routes:
resources :facilities do
collection do
post :sort
end
end
But in the end i have only a normal list, not draggable!
What am i missing?
Thanks!
As far as I remember does acts_as_list only deal with lists in Rails' models which means the server side of things. In order to make a list element draggable you'll have to include a client side library or plugin such as jQuery UI. The gem itself doesn't (or at least didn't use to) include the functionality you're describing.
This library or solution will then
make your elements draggable and
provide you with a callback to update the order in the DB (usually using an AJAX call)

Resources