Rails 4 - devise not displaying authentication errors - ruby-on-rails-4

I'm in the process of building a rails 4 app and I've run into a snag. When I go to the sign in page and enter the information correctly everything works
If I go to sign in but I don't input one of the required fields (name, email, etc) it will put up an error message saying 'Please review the problems below:' but the specific error messages, such as 'email can't be blank' do not show up.
The errors displayed properly before I added styling..
The code is here:
https://github.com/mikejames386/Black-Fret
I'm currently working off the devise branch.
This is the first time I've ever asked a question on stackoverflow so please let me know if there is any other info needed.
<div class="container">
<div id="form_container"><!-- start form_container -->
<h1>New Artist</h1>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<h2>Artist or Band Name</h2>
<div class="form_section">
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<br class="clear_both"/>
</div> <!-- end form_section -->
<h2>Primary Contact</h2>
<div class="form_section">
<div class="field">
<%= f.label :primary_first_name, 'First Name'%>
<%= f.text_field :primary_first_name %>
</div>
<div class="field">
<%= f.label :primary_last_name, 'Last Name'%>
<%= f.text_field :primary_last_name %>
</div>
<div class="field">
<%= f.label :email, 'Email'%>
<%= f.text_field :email %>
</div>
<br class="clear_both"/>
</div> <!-- end form_section -->
<h2>Password</h2>
<div class="form_section">
<div class="field"><%= f.label :password %>
<%= f.password_field :password %></div>
<div class="field"><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<br class="clear_both"/>
</div>
<h2>Members</h2>
<div class="form_section">
<p>member multi-add goes here</p>
</div> <!-- end form_section -->
<%= f.submit "Add Artist", :class =>'btn btn-custom' %>
<% end %>
</div> <!-- end form_container -->

Errors on Devise log-in page are added as flash messages.
In order to capture them, add following lines in app/views/layouts/application.html.erb
<% flash.each do |name, msg| %>
<%= content_tag :div, msg %>
<% end %>
As per Devise Documentation
Remember that Devise uses flash messages to let users know if sign in
was successful or failed. Devise expects your application to call
flash[:notice] and flash[:alert] as appropriate. Do not print the
entire flash hash, print only specific keys. In some circumstances,
Devise adds a :timedout key to the flash hash, which is not meant for
display. Remove this key from the hash if you intend to print the
entire hash.

I have Devise set up on rails 4 with my app and I actually have a Devise User model, a registration controller (which youdon't need) and a Devise views section. You may want to follow this tutorial https://github.com/plataformatec/devise, if you scroll down a bit you will see configuring views. The below will get put into your user model and the validatable is for validations on email and password which may be why the validations are not working properly just a thought. But I hope this tutorial gives you some guidance.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
After you install Devise and add it to your Gemfile, you need to run the generator:
rails generate devise:install
After you install Devise and add it to your Gemfile, you need to run the generator:
The generator will install an initializer which describes ALL Devise's configuration options and you MUST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator:
rails generate devise MODEL
Replace MODEL by the class name used for the applications users, it's frequently User but could also be Admin. This will create a model (if one does not exist) and configure it with default Devise modules. Next, you'll usually run rake db:migrate as the generator will have created a migration file (if your ORM supports them). This generator also configures your config/routes.rb file to point to the Devise controller.
Configuring views
We built Devise to help you quickly develop an application that uses authentication. However, we don't want to be in your way when you need to customize it.
Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after some time you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application:
rails generate devise:views

Related

Spree - how to add multiple products to cart

I have created a semi-custom Addon function for products. I created a new relationship table in the backend which connects one product to another product (through a checkbox and an ajax request in the backend).
What I am having trouble with is how to manipulate the populate_orders_path so that I can pass various variant/product id's to the cart at the same time.
Currently in my cart form partial I have this:
<div id="product-addons" class="col-md-6">
<h3 class="product-section-title">Add Ons</h3>
<%= image_tag('short-line-2.png', class: 'hundy') %>
<% addons = Addon.where(product_id: #product.id) %>
<% addons.each_with_index do |a, index| %>
<%= a.inspect %>
<% product_addon = Spree::Product.where(id: a.product_addon_id).first %>
<% addon_variant = Spree::Variant.where(product_id: a.product_addon_id).first %>
<% addon_image = Spree::Asset.where(viewable_id: addon_variant.id).first %>
<div class="col-lg-12">
<%= image_tag('/spree/products/' + addon_image.id.to_s + '/mini/' + product_addon.images[0].attachment_file_name) %>
<%= check_box_tag "products[#{a.id}][]", addon_variant.id, index == 0 %>
<span style="margin-left: 6px;"><%= product_addon.name %></span>
<br/>
</div>
<% end %>
</div>
But I am not sure what to do with
<%= hidden_field_tag "variant_id", #product.master.id %> in order for it to use multiple ID's. Any help would be much appreciated.
You can get above functionality by Spree promotion.
Configure the item added to cart rule and add another item to cart by action in promotion section.
it will work like this if Variant A is added to cart then Variant B will be added to cart automatically.

CKEditor cktext_area only shows up when refreshing page (Rails 4/Heroku)

My local version runs CKEditor fine.
Edit: Local version does the same thing.
It's runs on heroku as well, but when I first load my create blog post page, the cktext_area simply shows up as a normal text_area box. When I refresh the page, the ck_textarea shows up like it is supposed to.
I'm not sure why this is happening.
This is using rails 4.
Here's my application layout
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>
Here's my application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require turbolinks
//= require ckeditor/init
//= require_tree .
And my blog create form
<%= form_for #article do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title, :size=>'50' %>
</p>
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I know I'm late to the party, but may be it will help someone.
The problem is that ckeditor isn't initialized after visiting a turbolinks link. So the solution is to reinitialize ckeditor every time turbolinks loads pages.
This could be done using turbolinks callback turbolinks:load (page:load for old versions)
The second problem is that turbolinks:load fires every time even on document ready event. So I have to check somehow if ckeditor is initializes or I'll get The editor instance ... is already attached to the provided element.
The final code will be:
$(document).on('turbolinks:load', function() {
$('.ckeditor:visible').each(function() {
var id = $(this).attr('id');
if (!CKEDITOR.instances[id]) {
CKEDITOR.replace(id);
}});
});
OK. Well. This isn't a complete solution, but removing turbolinks solves this issue. I certainly don't need turbolinks for my project, but it would eventually be nice to find a solution that works including turbolinks.
removed //= require turbolinks from application.js
It would seem CKEditor doesn't play nice with turbolinks. Instead of disabling turbolinks, I simply added a data: { 'no-turbolink' => true } to any link that contained a ckeditor.
For example, in my header I had a link to create a new blog post:
<%= link_to "Create New Blog Post", new_article_path, data: { 'no-turbolink'=>true} %>
This solved it for me, without completely removing turbolinks.

How to integrate photo uploading code to member registration form

(I've removed working code part such as controller file and javascript to minimize question size as suggested in the chat. And I can add if required by anyone)
I followed this tutorial to upload image (worked successfully with some syntax modification). But when I integrated the code to member registration form, everything upto image preview is working, but photo is not uploading to the predefined directory.
Working part of code
uploadfile.html.erb
<h1>File Upload</h1>
<%= form_tag({:action => 'uploadFile'}, :multipart => true) do %>
<p><label for="upload_file">Select File</label> <%= file_field 'upload', 'datafile', :class => 'filen btn btn-danger'%></p>
<div class="upload-preview">
<img alt="your image"/>
</div>
<br><br />
<%= submit_tag "Upload" %>
<% end %>
Not working part of code (only uploading part. Works fine upto previewing image)
Now I integrate this code with my member reg form as below, by copy pasting form_tag contents without {} brackets in form_for to convert form_tag to form_for.
_form.html.erb
<div class="input-group">
<%= form_for(#member, :action => 'uploadFile', :multipart => true) do |f| %>
<p><label for="upload_file">Select File</label> <%= file_field 'upload', 'datafile', :class => 'filen btn btn-danger'%></p>
<div class="upload-preview">
<img alt="your image"/>
</div>
<div class="field">
<%= f.label :member_code %><br>
<%= f.text_field :member_code %>
</div>
So many fields and labels
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
members_controller.rb (Here, I copy paste the def uploadFile part from upload_controller.rb)
def index
#members = Member.all
end
def uploadFile
post = Member.save(params[:upload])
render :text => "File has been uploaded successfully"
end
def member_params
params.require(:member).permit(:member_code, :emp_code, :name, :father_name, :dob, :doe, :age, :caste, :religion, :residing_year, :nominee, :relation, :income_amt, :permanent, :off_add1, :off_add2, :off_add3, :mem_cancellation, :no_shares, :registration_date, :share_amt, :salary_head, :record_status, :sex, :mem_any_society, :occupation, :application_no, :dor, :kannada_name, :date_of_death, :date_of_inactive, :res_kan_add1, :res_kan_add2, :res_kan_add3, :kannada_nominee)
end
end
The preview part somehow working without creating new javascript file for member model.
From what I understand, When I press create member button, the file is not uploading to the predifined public/data directory. Even after I mention :action => 'uploadFile', in the view, why it is not working, when other things up-to preview image works fine. I think I have to add upload inside create action itself, but don't know what exactly to add.
Please help me.

Dropdown depending on another collection_select rubyonrails

Guys that is my code thing is I want to select a parent subject and upon selection I need to use that selected subject to list the topics within it in the parent field how can i do that?
<label>Parent Subject</label>
<div class="field">
<%= collection_select :topic, :subject_id, Subject.all, :id, :name %>
</div>
<label>Parent Topic</label>
<div class="field">
<%= collection_select :topic, :parent_id, ?????, :id, :name %>
</div>
Would this help? Rails dependent collection_select fields in form
If not, I am about to build something and might be happy to share it if it isn't to ugly!

Rails 4 edit form do not display upload photo

I have a mongo rails 4 app, with an embedded photo.
I have no problem in the new form, but in the edit form, if the user has not upload any photo, the form will not display a "add photo button".
Here is the form:
<%= f.fields_for :founder_profile_photos do |founder_photo_f| %>
<%= render partial: 'founder_profile_photo_fields', locals: { f: founder_photo_f } %>
<%= link_to_add_association raw('<i class="fi-plus"> add a founder photo</i>'), f, :founder_profile_photos %>
<% end %>
Here is the partial:
<div class="nested-fields">
<% if f.object.file.to_s.empty? %>
<%= f.file_field :file, label: "Upload a founder photo." %>
<% else %>
<%= image_tag f.object.file, class: 'small-10 medium-10 image-previewer' %>
<% end %>
</div>
Thank you so much for your help. I finally found what was wrong, I needed to build the field photo in the edit action of my controller, other the field does not exist and therefore do not appear in the view.
def edit
if #user.photos.empty?
#user.photos.build
end
end
photo is embedded into the user model (one to many relation) if you have a one to one relation you have to do:
if #user.photo.blank?
#user.photo = Photo.new
end
Thank you very much for all your help, still so much to learn.