Using Parsley With Rails - ruby-on-rails-3

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

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

Navbar dropdown not working

I have the code below, but the dropdown is not working, any ideas?
Do I need to add "$('.dropdown-toggle').dropdown();" to my javascript file?
.navbar.navbar-fixed-top
.navbar-inner
.container
%button.btn.btn-navbar{"data-target" => ".nav-collapse", "data-toggle" => "collapse", :type => "button"}
%span.icon-bar
%span.icon-bar
%span.icon-bar
= link_to I18n.t(:site_title), root_url, :class => "brand"
.nav-collapse
%ul.nav
%li= link_to I18n.t(:nav_thelist)
%li= link_to I18n.t(:nav_themap)
%li= link_to I18n.t(:nav_the92)
%li.dropdown
%a.dropdown-toggle{"data-toggle" => "dropdown", "data-target" => "#", :href => "#"}
= I18n.t(:nav_admin)
%b.caret
%ul.dropdown-menu
%li= link_to I18n.t(:nav_leagues), leagues_url
%li= link_to I18n.t(:nav_clubs), clubs_url
%li= link_to I18n.t(:nav_visits), visits_url
Dropdowns in Twitter-Bootstrap require not only the appropriate attributes and CSS, but also the appropriate javascript plugins. Check here to make sure you have the right javascript plugins installed for dropdowns:
http://twitter.github.com/bootstrap/javascript.html
If you are using twitter-bootstrap-rails gem
it names the require path incorrectly in application.js
Change it to
//= require bootstrap

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, paperclip assigns id 0 when uploading image

iam using rails with formtastic for my admin backend. i want to be able to upload an image to my recordset, and i try to use paperclip to to that.
when i edit a recordset, the upload of the image works just fine. when i try to CREATE a NEW recordset, paperclip seems to assign the ID 0 for that image in my upload path!
#expected path for new image:
/public/logos/2342/some_image.png
#and thats what i get when i create my new record-set:
/public/logos/0/some_image.png
i tried to add attr_accessible to my model
attr_accessible :logo_file_name, :logo_content_type, :logo_file_size, :logo_updated_at
but that throws me an sql-error
Column 'logo_file_size' cannot be null
EDIT: solved the mysql error when i add attr_accessible. i just allowed the logo_file_size to be null. but the id=0 problem still exists...
my code:
MODEL:
has_attached_file :logo,
:url => "/:class/:attachment/:id/:basename.:extension",
:styles => { :original => ["150x150>", :png] }
VIEW:
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :logo, :as => :file %>
<%= f.input :link, :as => :url %>
<%= f.input :published, :published => 'Veröffentlicht' %>
<% end %>
CONTROLLER:
def create
Article.create(params[:article])
end
my datebase has these 4 colums in Article-Table:
logo_file_name
logo_content_type
logo_file_size
logo_updated_at
iam using rails 3.1.1, formtastic 1.2.4, paperclip 2.4.5
thanks a lot for your help!!!
i know its a bit late, but i found the problem and will share the answer for everyone with the same problem.
problem was mysql, upgraded to mysql2 gem, and everything worked as expected

Delete link doesn't work but button does

I have a problem very similar to this one: rails 3 - link_to to destroy not working
But delete/destroy links do not work; I simply get redirected to the show page of the object. When I make a button for delete, it all works fine. But I'd like to understand why. Does anyone know?
I seems to be related to some .js files I am using/calling.
<!-- This link doesn't work -->
<%= link_to('Delete', post, :confirm => 'Are you sure?', :method => :delete) %>-->
<!-- This button does work -->
<%= button_to "delete", post, :method=>:delete, :class=>:destroy, :confirm => 'Are you sure?' %>
Post Controller
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
UPDATE
After doing some further research it seem that everyone else having a similiar issue has included the following jquery library:
<script type="text/javascript" src="/javascripts/dragdrop.js"></script>
But I still don't know what the issue is...
LOG
Started GET "/posts/7" for 127.0.0.1 at Tue Jul 12 08:34:06 -0400 2011
Processing by PostsController#show as HTML
Parameters: {"id"=>"7"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 7 LIMIT 1
SQL (0.2ms) SELECT COUNT(*) FROM "comments" WHERE ("comments".post_id = 7)
Rendered posts/show.html.erb within layouts/application (116.5ms)
HTML generated
Delete
UPDATE: I've found that removing <script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script> fixes my problem. But I don't understand why. Is there a known conflict between jquery1.5.1 and rails 3.0.7?
Make sure you include these in your application layout:
<%= javascript_include_tag(:defaults) %>
<%= csrf_meta_tag %>
In my case adding
<%= javascript_include_tag(:defaults) %>
did not work. However explicitly defining java script files did the trick
<%= javascript_include_tag 'prototype' %>
<%= javascript_include_tag 'application' %>
Not sure yet why :defaults tag didn't work...
Rails will automatically load jquery for you if you have jquery-rails gem loaded via your Gemfile (this is the default rails configuration). It is then loaded via:
<%= javascript_include_tag(:defaults) %>
Have a look at app/assets/javascripts/application.js for the code that tells rails to add jquery via assets:
//= require jquery
//= require jquery_ujs
By trying to load another copy of jquery via this line:
<script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script>
You have caused a clash of jquery instances, the effect of which will be that neither will work.