I have two models for communication, Post and Reply. For some reason I can't figure out, the #reply form is unresponsive. Submit does not work, and the file upload field does not work, but only on the #reply form, the #prosect and #post forms work perfectly.
<h1><strong><%= #group.name %></strong></h1>
<p><em><%= #group.description %></em><p>
<p>Operated by: <% #owner = User.find(#group.owner_id) %><%= #owner.name %></p>
<% if current_user.group_id == nil %>
<%= form_for(#prospect) do |f| %>
<%= f.hidden_field :group_id, value: #group.id %>
<%= f.submit "Join Group", class: "btn btn-primary" %>
<% end %>
<% end %>
<% if current_user.group_id == #group.id %>
<%= form_for(#post) do |f| %>
<%= f.hidden_field :group_id, value: #group.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<div class="form-group col-xs-12">
<div class="row">
<div class="col-xs-11">
<%= f.text_area :content, class: 'form-control', placeholder: "What ails you?" %>
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-success" onclick="addpic(0);">Add a pic</button>
</div>
</div>
</div>
<div class="form-group hidden col-xs-12" id="picture0">
<%= f.file_field :image, class: 'form-control' %>
</div>
<div class="form-group col-xs-12 actions">
<%= f.submit %>
</div>
<% end %>
<% #group.posts.each do |p| %>
<div class="post">
<div class="post-meta">
<%= p.user.name %> <em><%= p.created_at.strftime('%a, %b %e, %Y %r') %></em>
</div>
<%= p.content %>
<% if p.image %>
<img src="data:image/png;base64,<%= p.image %>" class="img-responsive"/>
<% end %>
<% p.replies.each do |r| %>
<%= r.user.name %><br/><%= r.content %>
<% if r.image %>
<img src="data:image/png;base64,<%= r.image %>" class="img-responsive"/>
<% end %>
<% end %>
<a class="reply">Reply</a>
<div class="reply hidden">
<%= form_for(#reply) do |ff| %>
<%= ff.hidden_field :post_id, value: p.id %>
<%= ff.hidden_field :user_id, value: current_user.id %>
<div class="form-group col-xs-12">
<div class="row">
<div class="col-xs-11">
<%= ff.text_area :content, class: 'form-control', placeholder: "Write a reply..." %>
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-success" onclick="addpic(<%= p.id %>);">Add a pic</button>
</div>
</div>
</div>
<div class="form-group hidden col-xs-12" id="picture<%= p.id %>">
<%= ff.file_field :image, class: 'form-control' %>
</div>
<div class="form-group col-xs-12 actions">
<%= ff.submit %>
</div>
<% end %>
</div>
</div>
<% end %>
<% end %>
<script>
var addpic = function(id) {
var el = '#picture' + id;
$(el).removeClass('hidden');
}
$('.reply').on('click', function(e){
e.preventDefault();
$(this).next().removeClass('hidden');
});
</script>
Figured out the issue, JS was preventing the default for all clicks inside a .reply div. Had reply used in the wrapper for the reply form, so it was preventing the default. Got it working now by changing the class of the reply link to 'addreply' and the js listener to 'addreply' as well.
Related
I have been struggling to convert this to some sort of rails helper select. Can anyone help with this?
<%= form_with(model: scheduleevent, class: "contents", data: { controller: 'nested-form', nested_form_wrapper_selector_value: '.nested-form-wrapper' }) do |form| %>
... schedtimerange_form start ...
<%= select_tag "days[]",options_for_select(Date::DAYNAMES.zip((1..7))), id:"days[]", multiple: true, class: 'multis' %>
<% end %>
The select tag is within a nested attribute partial called schedtimerange_form that is called like this:
<template data-nested-form-target="template">
<%= form.fields_for :schedtimeranges, Schedtimerange.new, child_index: 'NEW_RECORD' do |schedtimerange| %>
<%= render "schedtimerange_form", form: schedtimerange %>
<% end %>
</template>
<%= form.fields_for :schedtimeranges do |schedtimerange| %>
<%= render "schedtimerange_form", form: schedtimerange %>
<% end %>
Schedtimerange_form:
<div data-new-record="<%= form.object.new_record? %>">
<div class="w-full">
<div data-controller="select">
<%= select_tag "days[]",options_for_select(Date::DAYNAMES.zip((1..7))), id:"days[]", multiple: true, class: 'multis' %> </div>
</div>
<div>
<div class="w-full mb-2">
<label class="form_label"><%= t("scheduling_events_new_lbl_starttime") %> </label>
<div class="pr-4"><%= form.text_field :start_time, data: { controller: "flatpickr", flatpickr_enable_time: true, flatpickr_no_calendar: true,
flatpickr_date_format: "h:i K" }, class: 'form_control', placeholder: "#{t("scheduling_events_new_lbl_starttime")}" %> </div>
</div>
<div class="w-full mb-4">
<label class="form_label"><%= t("scheduling_events_new_lbl_endtime") %> </label>
<div class="pr-4"><%= form.text_field :end_time, data: { controller: "flatpickr", flatpickr_enable_time: true, flatpickr_no_calendar: true, flatpickr_date_format: "h:i K" }, class: 'form_control', placeholder: "#{t("scheduling_events_new_lbl_endtime")}" %> </div>
</div>
<div class="w-full">
<button type="button" class="font-medium text-red-600 dark:text-red-500 hover:underline" data-action="nested-form#remove">Remove Times</button>
</div>
</div><%= form.hidden_field :_destroy %>
</div>
<%= select_tag "days[]",options_for_select(Date::DAYNAMES.zip((1..7))), id:"days[]" %>
Don't know why my app is throwing error on the registration file.Plz let me know the error.Thank you very much
Error
Showing /home/punisher/Desktop/billboard/app/views/register/signup.html.erb where line #36 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #36):
<h1 class="Login-here">Register Here</h1>
<%= form_with model: #owner,url:register_path, local: true do |form| %>
<% if #owner.errors.any? %>
<div class="alert alert-danger">
<% #owner.errors.full_messages.each do |message| %>
<div> <%= message %> </div>
Register_controller
class RegisterController < ApplicationController
def new
#owner=Owner.new
end
def create
# render plain: "Thanks for registering!"
# render plain: params[:user]
#owner=Owner.new(owner_params)
if #owner.save
session[:owner_id] = #owner.id
redirect_to login_path
else
flash[:error] ="User Not Registered"
render :signup
end
end
private
def owner_params()
params.permit(:email, :company_name, :address, :contact_no, :password, :password_confirmation)
end
end
signup.erb.html
<body style = "background-color:rgb(92, 179, 196)">
<div class="tnavbar">
<div class="tlogo">
<%= image_tag('1.png', :class => "logoimg") %>
<h3 class="title"> Out of Home</h3>
</div>
<div>
<%= link_to "login", login_path, class:"home-btn"%>
</div>
</div>
<div class="container-fluid" style="margin-top:50px;">
<div class="row loginpagerow">
<div class="col-1">
</div>
<div class="logincol col-5 imagebox">
<h1 class="headinglogin">Bringing you the best of the best.</h1>
<%= image_tag('loginpageimage.png', :class => "img") %>
</div>
<div class="col-5 logincol loginbox">
<h1 class="Login-here">Register Here</h1>
<%= form_with model: #owner,url:register_path, local: true do |form| %>
<% if #owner.errors.any? %>
<div class="alert alert-danger">
<% #owner.errors.full_messages.each do |message| %>
<div> <%= message %> </div>
<% end %>
</div>
<% end %>
<div class="row" style="margin-top:30px">
<div style="width:45%; height:35vh; margin-left:25px;">
<%= form.email_field :email ,class: "form-control login-email-field signup-fields", placeholder:"Email" %>
<%= form.text_field :company_name ,class: "form-control login-email-field signup-fields", placeholder:"Company Name" %>
<%= form.text_field :address ,class: "form-control login-email-field signup-fields", placeholder:"Address" %>
</div>
<div style="width:45%; height:35vh; margin-left:25px;">
<%= form.telephone_field :contact_no ,class: "form-control login-email-field signup-fields", placeholder:"Contact No" %>
<%= form.password_field :password ,class: "form-control login-email-field signup-fields", placeholder:"password" %>
<%= form.password_field :password_confirmation,class: "form-control login-email-field signup-fields", placeholder:"Confirm password" %>
</div>
<div class="mb-3 login-input">
<%= form.submit "Signup" ,class: "sign-btn btn btn-primary"%>
</div>
</div>
<% end %>
</div>
<div class="col-1">
</div>
</div>
</div>
</body>
You have a app/views/register/signup.html.erb view and Rails expects signup method in your registrations_controller and is looking for #owner
instance method but you don't have signup method that's why it's raising an error because #owner is nil.
def signup
#owner = Owner.new(owner_params)
...
end
Working on a form:
<%= form_for(#contact, html: {multipart: true}) do |f| %>
<div class="card">
<div class="card-header">
<h2 class="display-5 main-text-blue text-center font-weight-bold">Add New Contact</h2>
</div>
<div class="card-body">
<div class="errors">
<% if #contact.errors.any? %>
<div class="alert alert-danger">
<h5 class="text-center mb-n1 pb-1"><i class="fa fa-exclamation-triangle text-danger"></i> Please correct the following errors: </h5>
</div>
<% end %>
</div>
<div class="form-group row">
<div class="col-md-5 mx-auto">
<div class="wrap" id="avatar-container">
<div class="valign-middle">
<div class="form-group">
<% if #contact.new_record? %>
<%= image_tag "100x100.png", class: "img-responsve img-preview" %>
<% else %>
<%= image_tag #contact.avatar, class: "img-responsve img-preview" %>
<% end %>
<label for="file" class="sr-only">Choose Image</label>
<%= f.file_field :contact_avatar, id: "file" %>
</div>
</div>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label :name, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :name, class: "form-control #{'is-invalid' if has_error?(#contact, :name) }", id: "name", placeholder: "Name.." %>
<% if has_error?(#contact, :name) %>
<span class="text-danger">
<%= get_error(#contact, :name)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :email, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :email, class: "form-control #{'is-invalid' if has_error?(#contact, :email) }", id: "email", placeholder: "Email.." %>
<% if has_error?(#contact, :email) %>
<span class="text-danger">
<%= get_error(#contact, :email)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :mobile, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :mobile, class: "form-control #{'is-invalid' if has_error?(#contact, :mobile) }", id: "mobile", placeholder: "Mobile.." %>
<% if has_error?(#contact, :mobile) %>
<span class="text-danger">
<%= get_error(#contact, :mobile)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :phone, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :phone, class: "form-control #{'is-invalid' if has_error?(#contact, :phone) }", id: "phone", placeholder: "Phone.." %>
<% if has_error?(#contact, :phone) %>
<span class="text-danger">
<%= get_error(#contact, :phone)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :country, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :country, class: "form-control", id: "country", placeholder: "Country.." %>
</div>
</div>
<div class="form-group row">
<%= f.label :address, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :address, class: "form-control", id: "address", placeholder: "Address.." %>
</div>
</div>
<div class="form-row">
<%= f.label :location, class: "col-lg-2 col-form-label" %>
<div class="col-4">
<%= f.text_field :city, class: "form-control", id: "city", placeholder: "City.." %>
</div>
<div class="col-4">
<%= f.text_field :state, class: "form-control", id: "state", placeholder: "State.." %>
</div>
<div class="col-2">
<%= f.text_field :zip, class: "form-control", id: "zip", placeholder: "Zip.." %>
</div>
</div>
<div class="form-group row category-mt">
<%= f.label :category, class: "col-lg-2 col-form-label" %>
<div class="col-lg-5">
<%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "Select Category" }, id: "category_select", class: "form-control #{'is-invalid' if has_error?(#contact, :category) }" %>
<% if has_error?(#contact, :category) %>
<span class="text-danger">
<%= get_error(#contact, :category)%>
</span>
<% end %>
</div>
<div class="col-lg-3">
<a class="btn btn-outline-secondary add-category-button btn-block mt-1" href="#" id="add-category-btn">Add Category</a>
</div>
</div>
<div class="form-group row" id="add-new-category">
<label class="col-lg-2 col-form-label" for="location">New Category:</label>
<div class="col-lg-10">
<div class="input-group">
<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(#category, :name) %>" placeholder="Enter category name" type="text">
<% if has_error?(#category, :name) %>
<span class="text-danger">
<%= get_error(#category, :name)%>
</span>
<% end %>
<div class="input-group-append">
<button class="btn btn-outline-secondary category-btn" id="save-new-category-btn" type="button"><i class="fa fa-check"></i></button>
</div>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label :note, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_area :note, class: "form-control", id: "note", placeholder: "Note..", rows: "3" %>
</div>
</div>
</div>
<div class="card-footer">
<%= f.submit "Save", class: "btn btn-primary border-button mb-3 ml-3", id: "save-btn" %>
<a class="btn btn-outline-secondary border-button mt-n3" data-dismiss="modal" href="#" id="cancel-btn">Cancel</a>
</div>
</div>
<% end %>
As you can see here I place some rails validation codes which already exist on contacts model. For instance:
<%= f.text_field :name, class: "form-control #{'is-invalid' if has_error?(#contact, :name) }", id: "name", placeholder: "name.." %>
Which will add the class is-invalid if there are any errors. And this also:
<% if has_error?(#contact, :phone) %>
<span class="text-danger">
<%= get_error(#contact, :phone)%>
</span>
<% end %>
Which will simply print the error text at the bottom if there's an error. Here's the helper codes for that:
def has_error?(resource, field)
resource.errors.messages[field].present?
end
def get_error(resource, field)
msg = resource.errors.messages[field]
field.to_s.capitalize + " " + msg.join(' and ') + '.'
end
So, basically I am working on two tables here: Contact and Category. So far what am I verifying on my form are mostly contacts. I have this feature here where in user can add NEW CATEGORY right on the spot via ajax which is a foreign key on the contacts table which can also be seen on the code above.
Now, on my understanding, I can also add a validation on models. So I tried to put some validation on my category:
validates :name, uniqueness: true
I was hoping that this will validate the uniqueness of the new category being output, meaning if the text (category name) already exist on the database it must prohibit it and throw an error. So what I did is I tried the following code to display the error:
<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(#category, :name) %>" placeholder="Enter category name" type="text">
<% if has_error?(#category, :name) %>
<span class="text-danger">
<%= get_error(#category, :name)%>
</span>
<% end %>
And so I thought it will work however it just throw an error saying
undefined method errors' for nil:NilClass which is I thought it should work since I am using #category but then I realize it's a form for #contact.
Is there a better way to make this work and display the error on the add new category field? I am really stuck on this. I hope someone can help me.
You can get categories errors through f object
<% f.object.categories.each do |category| %>
<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(category, :name) %>" placeholder="Enter category name" type="text">
<% if has_error?(category, :name) %>
<span class="text-danger">
<%= get_error(category, :name)%>
</span>
<% end %>
<% end %>
am new to stack overflow..hoping support from all f u
i have employee form with personal details,contact details,salary details etc,arranged,but they are different controllers.i want to get all data in one form in employee view.employee contacts and other forms are not getting saved...help out...
`
<%= form_for(#employee, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |f| %>
<div class="form-group row">
<%= f.label :first_name, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= f.text_field :first_name, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :first_name) %></span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= f.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
<div role="tabpanel" class="tab-pane in" id="nav-tabs-0-2">
<%= form_for(#employee.contact_components, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |e| %>
<div class="form-group row">
<%= e.label "P O Box", class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_field :po_box, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :po_box) %></span>
</div>
</div>
<div class="form-group row">
<%= e.label :permanent_address, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_area :permanent_address, class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= e.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
</div>`
What you want is a nested form. It allows you to update different model attributes from a single form. That doc link will walk you through each change you need to make to the model, controller and views.
Im worked on a project that create events.But I click on a "create event" button with empty fields partial "_error_messages" don't render list with a errors.The page the page is loaded again.Validates seems to be working, in rails console I tried to create and bash show me an error "can't be blank".
This is my _event_form.html.erb
<%= form_for event, html: { class: "event-form" } do |f| %>
<%= render 'shared/error_messages', event: event %>
<% unless current_user %>
<div class='row'>
<div class='col-md-4'>
<div class="form-group col-sm-12">
<%= label :email, "Введите Ваш email:" %>
<%= text_field_tag :email, "",class: "form-control" %>
<small>Он нужен для того, что бы Вы могли позже отредактировать добавленное Вами событие.</small>
</div>
</div>
</div>
<% end %>
<div class="row">
<div class="col-md-8">
<div class="form-group col-sm-12">
<%= f.label "Название" %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group col-sm-4">
<%= f.label "Изображение" %>
<%= image_tag event.image, class: "img-responsive" if event.image.present? %>
<%= f.file_field :image %>
</div>
<div class="form-group col-sm-4">
<%= f.label "Город" %><br />
<%= select_tag :city_id, options_from_collection_for_select(City.all, :id, :name), name: "event[city_id]", placeholder: "Город" %>
<%#= f.file_field :image %>
</div>
<div class="form-group col-sm-12">
<%= f.label "Описание" %>
<%= f.text_area :description, class: "form-control", rows: 10 %>
</div>
<div class="form-group col-sm-12">
<%= f.label "Адрес" %>
<%= f.text_field :address, class: "form-control", id: :address %>
<div id='map'></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group col-sm-6">
<%= f.label "Цена от" %>
<input name="event[price][min_price]" value="<%= event.price.try(:min_price) %>" class="form-control" type="number" min="0" />
<%= f.label "Цена до" %>
<input name="event[price][max_price]" value="<%= event.price.try(:max_price) %>" class="form-control" type="number" />
<%= f.label "Тип цены" %>
<br />
<%= select_tag :price_types, options_from_collection_for_select(PriceType.all, :id, :name), name: "event[price][price_type_id]", placeholder: "Выберите тип цены" %>
</div>
<div class="form-group col-sm-6">
<%= f.label "Время проведения" %>
<%= f.text_field :time, class: "form-control", type: :time %>
</div>
<div class="form-group col-sm-6">
<%= f.label "Тип отдыха" %>
<br />
<%= select_tag :types, options_from_collection_for_select(Type.all, :id, :name, selected: event.types.pluck(:id)), name: "event[types_ids][]", placeholder: "Тип отдыха" %>
</div>
<div class="form-group col-sm-12">
<%= f.label "Дата проведения" %>
<div id="mdp"></div>
<input type='hidden' name="event[days]" class="form-control" id="mdp_alt" />
</div>
</div>
</div>
<div class="form-group text-right">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
<script>
$(document).ready(function(){
var dates = <%= event.days.pluck(:date).map{ |d| d.to_s.to_datetime.to_i * 1000 } %>;
if(dates.length){
var settings = {
altField: '#mdp_alt',
dateFormat: "yy-mm-dd",
addDates: dates
}
}else{
var settings = {
altField: '#mdp_alt',
dateFormat: "yy-mm-dd"
}
}
$('#mdp').multiDatesPicker(settings);
})
</script>
this is my _error_messages.erb
<% if event.errors.any? %>
<div id="errorExplanation">
<h2> В форме обнаружено <%= pluralize(event.errors.count, "ошибка", "ошибки") %>:</h2>
<ul>
<% event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
what's wrong?