Rails best in place gem - ruby-on-rails-3

I followed Ryan Bates' railscast on the best in place gem but cannot get it to work properly. The default value I set before_create in the user.rb model is showing up on the page but I cannot click on it to edit; it's just showing as a static element. Although, Firebug is showing that the best_in_place javascript is being loaded in the view <head>.
application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require best_in_place
//= require_tree .
users.js.coffee
jQuery ->
$('.best_in_place').best_in_place()
user.rb
attr_accessible :email, :name, :password, :password_confirmation, :avatar, :goal
...
before_create :default_values
...
private
def default_values
self.goal ||= "Write Current Goal Here"
end
users_controller.rb
respond_to :html, :json
...
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html {
flash[:success] = "Profile updated"
sign_in #user
redirect_to #user
}
format.json { respond_with_bip(#user) }
else
format.html { render 'edit' }
format.json { respond_with_bip(#user) }
end
end
end
users/show.html.erb
...
<%= best_in_place #user, :goal %>

In your layouts/application.html.erb .. instead of javascript_include_tag(:all) do
javascript_include_tag(:application)
Also, make sure that users.js.coffee lives in app/assets/javascript
Edit
Ensure that you have the gem specified in the Gemfile and bundle installed
gem "best_in_place"
Try this, like Said Kaldybaev suggested
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require_tree .
//= require best_in_place
then instead of coffee do this at the top of the page:
$(document).ready(function() {
$('.best_in_place').best_in_place();
})
If that doesn't work, remove //= require best_in_place and //= require jquery.purr then run
rails g best_in_place:setup

me also had the same problem, solved by moving the require best_in_place to the end of require list
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require_tree .
//= require best_in_place

This works for me:
Change
$(document).ready(function() {
/* Activating Best In Place */
jQuery(".best_in_place").best_in_place();
});
To
$(document).ready(function() {

Related

Rails Devise Signout not working

I am having issues logging out of my Rails app via Devise. I have looked at several different posts about this and have been struggling for a couple days now.
couldnt-find-user-with-id-sign-out
couldnt-find-user-with-id-sign-out
are some examples that have not worked so far. I have a feeling it is javascript related but I am not sure.
routes.rb:
Rails.application.routes.draw do
resources :events
devise_for :users
resources :users
resources :articles
resources :athletes
get 'welcome/index'
get 'athletes/index'
get 'users/login'
root 'welcome#index'
end
application.html.rb
<% if user_signed_in? %>
<%= link_to 'Logout', destroy_user_session_path, :method => :delete %>
application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_bootstrap
//= require_tree .
Any advise or direction would be appreciated I am not sure where to go from here.
thanks,
I resolved this be modifying ./config/initializers/devise.rb and changing
config.sign_out_via = :get

Using Parsley With Rails

I am trying to add parsley.js to a rails project to do client side form validation and can't seem to get it working.
I am using the gem for parsley for the rails asset pipline - https://github.com/mekishizufu/parsley-rails
gem "parsley-rails"
I went ahead and required it in my application.js file
//= require jquery
//= require parsley
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .
And then I added the following to the form I wish to validate on
<%= form_for :user, :html => {:"data-validate" => 'parsley'} do |user| %>
Which generates a data attribute in the html for the form
data-validate="parsley"
Then to check for the presence of the address I have:
<%= label_tag :current_address %>
<%= text_field_tag :address, nil, :data => {:"required" => 'true'} %>
Which renders a text field with a attribute for the text field
data-required="true"
Yet when I click submit no validation occurs and a post request is made. What else do I need to do in order to get parsley working on a rails 3 form? Much appreciated!
The gem uses Parsley-js version 1.2.3, as 2.0.0 is still a release candidate. Here are the docs for that version: http://parsleyjs.github.io/Parsley-1.x/documentation.html
The attributes are a bit different - the form_for tag should look like this:
<%= form_for :user, :html => {'parsley-validate' => ''} do |user| %>
and the text field:
<%= text_field_tag :address, 'parsley-required' => '' %>
Hope this helps.
Update
The 'parsley-rails' gem is now using v2.0.
The current version is now 2.0.5 and the docs can be found here http://parsleyjs.org
Using the rails gem you need to now add data-parsley-validate to the form and directives like required to the inputs.
Inside a rails form I had luck with:
<%= form_for #post, :html => {"data-parsley-validate" => true} do |f| %>
<%= f.text_field :title, :placeholder => "Title", :required => true %>
Hope this helps someone until parsley-rails gets some better links or basic usage docs in the readme. Might be a nice PR...
I got parsley to work in our Rails application by initializing parsley with
$('#form-id').parsley({ 'data-parsley-focus': 'first' });
the 'data-parsley-focus' is an option for where parsley will send the cursor if validations fail.
I included the gem as you did in gemfile, but my application.js loads parsley after the ujs adapter
//= require jquery
//= require jquery_ujs
//= require parsley

Error 500: We're sorry... due to javascript_include_tag

I've followed all the advice I could find on this issue, and yet when I run my app on the server I get a Error 500: We're sorry... error.
The production.log file tells me that this line is the offender:
<%= javascript_include_tag "application" %>
My application.js file looks like this:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require jquery.turbolinks
//= require fancybox
//= require bootstrap
//= require bootstrap-wysihtml5
//= require_tree .
(And removing that last line didn't help.) Since it's asset-related, the relevant parts of my production.rb file are:
config.serve_static_assets = true
config.assets.compress = false
config.assets.compile = true
config.assets.digest = true
Any help will help.
I don't know if this'll help anyone, but I needed these lines in my gemfile:
group :assets do
gem 'coffee-rails'
end

Making rails3-jquery-autocomplete work with these Post and Tag models?

I have a Post and a Tag model (the later as an attribute called :name).
They are associated with has_and_belongs_to_many.
views/posts/_form.html.erb:
<%= f.label :tags %>
<%= autocomplete_field_tag :tags, params[:tags], autocomplete_tags_name_posts_path %>
routes.rb:
resources :posts do
get :autocomplete_tags_name, :on => :collection
end
posts_controller.rb:
class PostsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
autocomplete :tags, :name
Nothing happens and there are no errors.
I'm a bit confused about whether using :tags or :tags_name. As shown in the documentation.
(they both produce errors about undefined methods and variables)
application.js:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require autocomplete-rails
Any suggestions to fix this?
Maybe this example will help you, works with act_as_taggable but it's the same idea.
How to add tagging with autocomplete to an existing model in Rails?

Rails 3: scaffold destroy action redirect to show action

i created a simple scaffold, the problem is that when i try to delete a record click destroy rails redirect to the show action, this is the code of the link generated automatically when i generate the scaffold:
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>
what's wrong?
You need to have rails.js included in your application layout, as this link require JS to use the correct HTTP method. Also make sure you have the csrf_meta_tag in your layout.
Check if your application.js has //= require jquery_ujs
Add in application.js this code
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .