Simple form in rails doesn't display validation error messages - ruby-on-rails

I have this simple_form in my app but it doesn't display any validation error messages:
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<%= f.input :first_name, label: "Prénom" %>
<%= f.input :last_name, label: "Nom" %>
<%= f.input :email, label:"email" %>
<%= f.input :telephone, label: "telephone"%>
<p><%= f.label :birthdate, 'Date de naissance' %></p>
<%= f.date_select :birthdate, {:include_blank => true, :default => nil, :use_month_names => ['Janv.','Fevr.', 'Mars', 'Avr.', 'Mai', 'Juin', 'Juil.', 'Août','Sept.', 'Oct.', 'Nov.', 'Déc.'], :order => [:day, :month, :year], :start_year => 1910, :end_year => 1995} %>
<%= f.input :genre, label: "Sexe" %>
<%= f.input :ranking, label:"Classement" %>
<%= f.input :licence_number, label: "numéro de licence"%>
<p>
<%= f.label :Photo%>
<%= f.file_field :picture %>
</p>
<p>
<%= f.label :licence %>
<%= f.file_field :licencepicture %>
</p>
<p>
<%= f.label :certificat %>
<%= f.file_field :certifmedpicture %>
</p>
<div id="validation"><%= f.submit %></div>
<% end %>
Here are my validations in my user model
validates :first_name, presence: { strict: true }, on: :update
validates :last_name, presence: { strict: true }, on: :update
and you can have a look at my simple_form.en.yml:
en:
simple_form:
"yes": 'Yes'
"no": 'No'
required:
text: 'required'
mark: '*'
# You can uncomment the line below if you need to overwrite the whole required html.
# When using html, text and mark won't be used.
# html: '<abbr title="required">*</abbr>'
error_notification:
default_message: "Certains champs posent problèmes:"
My update method:
def update
#user.update(user_params)
#user.create_mangopay_natural_user_and_wallet
redirect_to user_path(current_user)
end
I don't get why the error notifications don't display as I have the proper f.error_notification in my view

As revealed in your comments, your controller doesn't check for a successful save and just blindly redirects to the user page. I didn't see any native functionality from simple_form that does this for you. You need to check for this successful save and re-render the edit form on failure so it can show those errors in f.error_notification:
def update
if #user.update(user_params)
#user.create_mangopay_natural_user_and_wallet
redirect_to user_path(current_user)
else
render 'edit'
end
end

Related

Why am I getting undefined variable for my params? Fields_for multiple model params

I am getting an undefined variable error with my app, claiming that my params is an undefined variable. It is suggesting that I use participant_path instead?
I am working on an app that has multiple models. There is a form that is using fields_for to fill in data for the a model which is hanging off the primary model.
When testing it I am using the primary model’s controller to process the form submission and defining the params to include both models’ attributes.
Is this the correct way to use fields_for?
I will add my code later, as I am posting this from my phone.
UPDATE with code:
Here is my form:
<%= form_for #participant, url: {action: "create"} do |f| %>
<%= f.label :first_name, 'First:' %>
<%= f.text_field :first_name %>
<%= f.label :last_name, 'Last:' %>
<%= f.text_field :last_name %>
<%= f.label :gender, 'Sex:' %>
<%= f.select :gender, ['Male', 'Female'] %>
<br>
<br>
<%= f.label :email, 'Email:' %>
<%= f.text_field :email %>
<%= f.label :phone, 'Phone:' %>
<%= f.text_field :phone %>
<%= f.label :street_name, 'Street Number & Name:' %>
<%= f.text_field :street_name %>
<%= f.label :city, 'City:' %>
<%= f.text_field :city %>
<%= f.label :state, 'State:' %>
<%= f.text_field :state %>
<%= f.label :zip, 'Zip:' %>
<%= f.text_field :zip %>
<%= f.hidden_field :role, :value => 'Reader' %>
<%= f.fields_for #participant.student_details do |student_detail_field| %>
<%= f.label :nationality, 'Nationality:' %>
<%= student_detail_field.text_field :nationality %>
<%= f.label :religion, 'Religion:' %>
<%= student_detail_field.text_field :religion %>
<%= f.label :birthdate, 'Birthdate:' %>
<%= student_detail_field.date_field :birthdate %>
<%= f.label :need_ride, 'Do you need help getting to the building?' %>
<%= student_detail_field.select :need_ride, ['Yes','No'] %>
<br>
<br>
<%= f.label :has_spouse, 'Marital Status:' %>
<%= student_detail_field.select :has_spouse, ['married', 'single'] %>
<br>
<br>
<%= f.label :spouse_name, "If married, spouse's name:" %>
<%= student_detail_field.text_field :spouse_name %>
<%= f.label :english_level, 'English Level:' %>
<%= student_detail_field.select :english_level, ['Low', 'Medium Low', 'Medium', 'Medium High', 'High'] %>
<%= f.label :expectations, 'What are your expectations for the program?:' %>
<%= student_detail_field.text_area :expectations %>
<%= f.label :length_of_stay, 'About how long will you be in Nashville?:' %>
<%= student_detail_field.select :length_of_stay, ['Less than 1 Year', '1 Year', '1-2 Years', '2-3 Years', '3+ Years'] %>
<%= f.label :exact_length, 'Please tell us exactly how long you plan to be in town:' %>
<%= student_detail_field.text_area :exact_length %>
<% end %>
<br>
<br>
<%= f.submit 'Register' %>
<% end %>
Here is the relevant code in my controller:
def new
#participant= Participant.new
end
def create
#participant = Participant.create(participant_params)
#participant.save
if #participant.save
flash[:success] = "Successfully Registered!"
#email notifes admin of new registration
NotifyMailer.notify_email(#participant).deliver_now
redirect_to '/signup'
end
def participant_params
params.require(:participant).permit(:first_name, :last_name, :gender, :email, :birthdate, :phone, :street_name, :city, :state, :zip, :nationality, :religion, :need_ride,
:has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id, :matched, :returned_home)
end
When I test this on the local server, I get this error:
undefined local variable or method `participant_params' Did you mean?
participant_path participant_url participants_path
Thanks in advance!

how to solve undefined method `model_name' for 1:Fixnum error ( ruby on rails)

on click on submit it should go to update_settings method of dashboard_controller. on click i gets this error.How to solve this
Any help is appreciatable
undefined methodmodel_name' for 1:Fixnum`
`
i have my routes like this
namespace :generic_users do
root to: 'dashboard#show'
get 'settings', to: 'dashboard#settings'
patch "settings" => "dashboard#update_settings", :as => "update_settings"
end
my dashboardcontroller is like this
class GenericUsers::DashboardController<ApplicationController
before_filter :authenticate_user!
def settings
#title='Generic Users Profile Management Page'
#generic_user = current_user.specific.id
end
def update_settings
#generic_user = GenericUsers.find(current_user.specific.id)
if #generic_user.update_attributes(sign_up_params)
redirect_to generic_users_root_path, flash: {notice: "Your details have been updated"}
else
render "new"
end
end
private
def sign_up_
end
private
def sign_up_params
params.require(:generic_user).permit(:username,:mobile,:firstname,:lastname, :email,:image,:referred_by,:address)
end
end
in views/dashboard folder, view file called,
settings.html.erb
<%= simple_form_for(#generic_user, :url => generic_users_update_settings_path) do |f| %>
<div class="form-inputs">
<%= f.input :username, required: true, autofocus: true %>
<%= f.input :firstname, required: true, autofocus: true %>
<%= f.input :lastname, required: true, autofocus: true %>
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :mobile, required: true, autofocus: true %>
<%= f.input :image, required: true, autofocus: true %>
<%= f.input :referred_by, required: true, autofocus: true %>
<%= f.label :address, "Address" %><br>
<%= f.text_area :address, required: true, autofocus: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
also have a model generic_user.rb
Please give me a solution to this error.
i have no idea
undefined method `model_name' for 1:Fixnum
You need to change the #generic_user in settings method as below
def settings
#title='Generic Users Profile Management Page'
#generic_user = GenericUsers.find(current_user.specific.id)
end
The first argument in the simple_form_for should be a record.

Rails 4 - Acts as Taggable on gem - set up errors

I am trying to make an app with Rails 4.
I am trying to use gem 'acts-as-taggable-on', '~> 3.4'
I found this tutorial showing how to set it up:
https://www.reddit.com/r/rails/comments/2chtgw/tagging_in_rails_4/
I have an articles controller, which has included tag_list in the strong params:
def article_params
params[:article].permit(:user_id, :body, :title, :image, :tag_list,
comment_attributes: [:opinion])
end
I have an articles form, with:
<%= simple_form_for(#article) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, autofocus: true %>
<%= f.input :body, :label => "Post", :input_html => {:rows => 10} %>
<%= f.input :image, :label => "Add an image" %>
<%= f.input :tag_list, :label => "Add tags" %>
</div>
<div class="form-actions">
<%= f.button :submit, "Submit & Publish", :class => 'formsubmit' %>
</div>
<% end %>
I have the table in my schema and have completed all the steps prior to the view integration in the reddit post.
When I try to check the form is working, I get this error:
undefined method `tag_list' for #<Article:0x007fad26cd9f30>
Can anyone see what's going wrong?

2 Views 1 Form in RoR

I am having trouble articulating the exact issue I am facing, but I will try with a brief descriptions and code.
I am trying to add a feature to a simple existing app that allows the user to crop an uploaded image for an avatar. I do the file selection on the same view that allows the user to update their password and other various account options. The user submits that form which then renders the view for the cropping feature. The issue is that from the crop view, the submission fails because it fails validation of parameters from the previous form. Basically I would like the form to all be submitted at the same time but from two different views.
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :avatar,
:crop_x, :crop_y, :crop_w, :crop_h
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
mount_uploader :avatar, AvatarUploader
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_avatar
def crop_avatar
avatar.recreate_versions! if crop_x.present?
end
end
I have tried several different things to remedy this. I am sure I am missing a fundamental concept. Any ideas?
users_controller.rb
def update
#user = User.find(params[:id])
if #user.update_attributes(params[:user])
if params[:user][:avatar].present?
render 'crop'
else
sign_in #user
redirect_to #user, notice: "Successfully updated user."
end
else
render 'edit'
end
end
edit.html.erb
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for #user, :html => {:multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
crop.html.erb
<% provide(:title, 'Crop Avatar') %>
<h1>Crop Avatar</h1>
<div class="row">
<div class="span6 offset3">
<%= image_tag #user.avatar_url(:large), id: "cropbox" %>
<h4>Preview</h4>
<div style="width:100px; height:100px; overflow:hidden">
<%= image_tag #user.avatar.url(:large), :id => "preview" %>
</div>
<%= form_for #user do |f| %>
<% %w[x y w h].each do |attribute| %>
<%= f.hidden_field "crop_#{attribute}" %>
<% end %>
<div class="actions">
<%= f.submit "Crop" %>
</div>
<% end %>
</div>
</div>
You can't have 2 views for one action. On a second thought why do you need it anyways, i mean you are rendering crop only when params[:user][:avatar] is present and that will be called only when you'll submit your edit.html.erb template. I think what you can do is have another method in controller with name crop and there update user's avatar with the dimention's specified.

Formtastic :destroy action as a button

I want to add delete button in actions. but all it does is js goback. I tried <%= f.actions %> didn't show the delete button. below is my effort to add it manually.
<% if can? :update, #parking_branch %>
<%= semantic_form_for #parking_branch do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :parking_company_id, :as => :select, :collection => Hash[ParkingCompany.all.map {|c| [c.company_name,c.id]}], :required => true %>
<%= f.input :branch_name, :required => true %>
<%= f.input :email, :required => true %>
<%= f.input :telephone, :required => false %>
<%= f.input :latitude, :hint =>"Automatically filled based on address" %>
<%= f.input :longitude, :hint =>"Automatically filled based on address" %>
<%= f.input :airport, :required => true %>
<%= f.input :address1 %>
<%= f.input :address2, :required => false %>
<%= f.input :address3, :required => false %>
<%= f.input :city %>
<%= f.input :county %>
<%= f.input :postcode %>
<%= f.input :country, :as => :country, :priority_countries => ["United Kingdom"], :required => true %>
<% end %>
<br />
<%= f.actions do %>
<%= f.action :submit, :button_html => {:class => 'btn-primary', :disable_with => 'Please Wait...' } %>
<%= f.action :cancel, :button_html => {:class => 'btn-danger', :disable_with => 'Please Wait...', :method => :delete } %>
<% end %>
<% end %>
<% else %>
<br />
<h1> You are not authorised to do this! <h1>
<% end %>
I have destroy action in my controller too

Resources