I'm currently trying to render a partial with errors messages if when a form submission fails to create a tenant instance. Im using locals as described in the Rails documentation to pass variables to a partial.
controllers/tenants_controller.rb
def new
#unit = Unit.find(params[:unit_id])
#tenant = Tenant.new
end
def create
#unit = Unit.find(params[:unit_id])
#tenant = #unit.tenants.new(tenant_params)
if #tenant.save
redirect_to #tenant
else
Rails.logger.error('Unable to save Tenant')
render 'new'
end
end
In here im redirecting to the new template if the #tenant fails to be created. This should be adding errors to the #tenant
tenants/new.html.erb
<div class="py-6">
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
<h1 class="text-2xl font-semibold text-gray-900">
Let's add the tenant's information
</h1>
</div>
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
<!-- Replace with your content -->
<%= form_for [#unit, #tenant], html: { class: "space-y-8 divide-gray-200" } do |f| %>
<%= render 'errors', locals: { tenant: f.object } %>
<div class="space-y-8 divide-y divide-gray-200 sm:space-y-5">
<div class="pt-8 space-y-6 sm:pt-10 sm:space-y-5">
<div class="space-y-6 sm:space-y-5">
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
<%= f.label :first_name, 'First name' %>
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= f.text_field :first_name, value: "Albert", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
<%= f.label :last_name, 'Last Name' %>
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= f.text_field :last_name, value: "Terry", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
<%= f.label :last_name, 'Last Name' %>
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= f.text_field :last_name, value: "Terry", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
<%= f.label :email, 'Email' %>
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= f.text_field :email, value: "example#gmail.com", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
<%= f.label :phone_number, 'Phone' %>
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= f.text_field :phone_number, value: "(000)000-0000", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
<%= f.label :lease_start_date, 'Lease start date' %>
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= f.text_field :lease_start_date, value: "", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
<%= f.label :lease_end_date, 'Lease end date' %>
</label>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= f.text_field :lease_end_date, value: "", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<div class="sm:border-t sm:border-gray-200 sm:pt-5">
<div>
<%= f.label :notes, 'Add your notes & documents', class: "block text-sm font-medium text-gray-700" %>
<div class="mt-1">
<%= f.rich_text_area :notes, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="pt-5">
<div class="flex justify-end">
<%= link_to 'Cancel', unit_path(#unit), class: "bg-white py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
<%= f.submit "Save", class: "ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
</div>
</div>
<% end %>
<!-- /End replace -->
</div>
Here im passing tenant variable via the locals however it doesn't seem to be passing the variable to the partial errors in the line <%= render 'errors', locals: { tenant: f.object } %>
When the errors partial is rendered an error is raised.
tenants/_errors.html.erb
<% if tenant.errors.any? %>
<div class="rounded-md bg-red-50 p-4">
<div class="flex">
<div class="flex-shrink-0">
<!-- Heroicon name: mini/x-circle -->
<svg class="h-5 w-5 text-red-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-red-800">
There were <%= tenant.errors.count %> errors with your submission
</h3>
<div class="mt-2 text-sm text-red-700">
<ul role="list" class="list-disc space-y-1 pl-5">
<% tenant.errors.full_messages.each do |error_message| %>
<li><%= error_message %></li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
<% end %>
Any idea why
undefined local variable or method `tenant' for #<ActionView::Base
is being raised?
You are calling render incorrectly.
You have two options. You can call it like this:
<%= render partial: 'errors', locals: { tenant: f.object } %>
or like this:
<%= render 'errors', { tenant: f.object } %>
Related
I am using devise to handle registration with username/email and facebook. Everything is working except error messages. If I try to login from the modal and enter the wrong email or password, it doesn't do anything, but I can't figoure out how to get the error messages to appear in the modal form.
If I close the modal after an error and reopen it there are no errors and I'm not sure how to refresh the modal. If it is successfulit logs me in and takes me to my profile page, but if it is not succesful it just sits there. I'm not sure how to open it without clicking a link.
If I submit the registragion form with no or missing info it will go back to the regular sign up page and display the errors but I can't figure out how to hvae it reload the modal and display them there.
Here is the _login.html.erb model:
<div class="modal fade" id="login" tabindex="-1" aria-labelledby="login" aria-hidden="true">
<div class="modal-dialog modal-lg border-0">
<div class="modal-content border-0">
<div class="modal-content-head">
<h5 class="modal-title" id="login">Member Login</h5>
<button type="button" class="close text-right" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-sm-12 col-md-4">
<div class="row border-0">
<div class="col-12 modal-content-subhead">
Login
</div>
</div>
<div class="row">
<div class="col modal-content">
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :remote => true) do |f| %>
<%= render "shared/error_messages", resource: resource %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_email.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.email_field :email, class: "form-control shadow rounded", placeholder: "Email", :autofocus => true %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.password_field :password, class: "form-control shadow rounded", placeholder: "Password" %>
</div>
</div>
<% if devise_mapping.rememberable? -%>
<div>
<%= f.check_box :remember_me %> <%= f.label :remember_me, class: "form_small_text" %>
</div>
<% end -%>
<div class="form-group row text-center">
<div class="col-12">
<%= f.submit "Sign in", :class => 'btn reg-submit-btn' %>
</div>
</div>
<div class="modal-footer">
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to t(".forgot_your_password"), new_password_path(resource_name), class: "form_small_text" %><br />
<% end -%>
</div>
<% end %>
</div>
</div>
</div>
<div class="col-sm-12 col-md-1 text-center p-0">
<%= image_tag("layout/login/login_divider.gif") %>
</div>
<div class="col-sm-12 col-md-7">
<div class="row border-0">
<div class="col-12 modal-content-subhead">
Sign-Up
</div>
</div>
<div class="row">
<div class="col modal-content">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), id: "form", :remote => true) do |f| %>
<%= render "shared/error_messages", resource: resource %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_name_37_33.gif") %>
</div>
<div class="col-md-5 col-sm-12">
<%= f.text_field :first_name, class: "form-control shadow rounded", placeholder: "First Name" %>
</div>
<div class="col-md-5 col-sm-12">
<%= f.text_field :last_name, class: "form-control shadow rounded", placeholder: "Last Name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_email.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.email_field :email, class: "form-control shadow rounded", placeholder: "Email", :autofocus => true %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<div class="reg_tooltip" data-tooltip="
The club username will be used to easily share your club page on this website.
For example if your club username is my_club the url for your club page on this
site will be www.themathouse.com/clubs/my_club.
">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-question-diamond" fill="white" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M6.95.435c.58-.58 1.52-.58 2.1 0l6.515 6.516c.58.58.58 1.519 0 2.098L9.05 15.565c-.58.58-1.519.58-2.098 0L.435 9.05a1.482 1.482 0 0 1 0-2.098L6.95.435zm1.4.7a.495.495 0 0 0-.7 0L1.134 7.65a.495.495 0 0 0 0 .7l6.516 6.516a.495.495 0 0 0 .7 0l6.516-6.516a.495.495 0 0 0 0-.7L8.35 1.134z"/>
<path d="M5.255 5.786a.237.237 0 0 0 .241.247h.825c.138 0 .248-.113.266-.25.09-.656.54-1.134 1.342-1.134.686 0 1.314.343 1.314 1.168 0 .635-.374.927-.965 1.371-.673.489-1.206 1.06-1.168 1.987l.003.217a.25.25 0 0 0 .25.246h.811a.25.25 0 0 0 .25-.25v-.105c0-.718.273-.927 1.01-1.486.609-.463 1.244-.977 1.244-2.056 0-1.511-1.276-2.241-2.673-2.241-1.267 0-2.655.59-2.75 2.286zm1.557 5.763c0 .533.425.927 1.01.927.609 0 1.028-.394 1.028-.927 0-.552-.42-.94-1.029-.94-.584 0-1.009.388-1.009.94z"/>
</svg>
</div>
<%= image_tag("layout/login/icon_reg_name_37_33.gif") %>
</div>
<div class="form-check col-md-10 col-sm-12">
<%= f.text_field :username, class: "form-control shadow rounded", placeholder: "Username", id: "username" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.password_field :password, class: "form-control shadow rounded", placeholder: "Password" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.password_field :password_confirmation, class: "form-control shadow rounded", placeholder: "Confirm Password" %>
</div>
</div>
<div class="form-group row text-center">
<div class="col-12">
<%= f.submit "Sign up", :class => 'btn reg-submit-btn' %></div></p>
</div>
</div>
<% end %>
</div>
</div>
</div>
<div class="row">
<div class="col-11 text-center">
<%=link_to( image_tag("layout/facebook_login_200_42.gif"), user_facebook_omniauth_authorize_path) %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
This is the applicatin_helpers.rb file:
module ApplicationHelper
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
end
Both forms in the modal work. I can sign up with email/password or facebok and I can create an account and log in but I can't get the errors to show up in the modal.
I'm new to rails and I am trying to set modal for user login/registration. I am able to get the modal to work but I canot figure out how to get the devise registraion to work in the modal. I even tried createing a separate users/_new.html.erb file with my own registration form but when I try to submit from the modal I get an error no route matches post/. I tried change the devise/registration/new.html.erb form to _new.html.erb but when I try to load the page I get an error:
ActionView::Template::Error (undefined local variable or method `resource' for
#<#Class:0x000000000ebf9b50:0x000000000ec0de70>
Did you mean? rescue):
The above error happens when I render the standard devise/registrations/new.html.erb form. I converted it to a partial and move it to the shared folder.
<h1><%= t('.sign_up') %></h1>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, autocomplete: 'email', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, autocomplete: 'current-password', class: 'form-control' %>
<% if #minimum_password_length %>
<small class="form-text text-muted"><%= t('devise.shared.minimum_password_length', count: #minimum_password_length) %></small>
<% end %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: 'current-password', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit t('.sign_up'), class: 'btn btn-primary' %>
</div>
<% end %>
<%= render 'devise/shared/links' %>
The other way I tried it was posted the entire form in the modal on the nav page, but when I click submit, it goes to localhost.3000 and I get an error that no routes match post "/".
That form is, sorry, it's long, I have two forms in the modal:
<!-- Modal -->
<div class="modal fade" id="login" tabindex="-1" aria-labelledby="login" aria-hidden="true">
<div class="modal-dialog modal-lg border-0">
<div class="modal-content border-0">
<div class="modal-content-head">
<h5 class="modal-title" id="login">Member Login</h5>
<button type="button" class="close text-right" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-sm-12 col-md-5">
<div class="row border-0">
<div class="col-12 modal-content-subhead">
Login
</div>
</div>
<div class="row">
<div class="col-12 modal-content">
<%= form_with(model: #user, class: "shadow p-3 mb-3 rounded text-light", local: true) do |f| %>
<div class="form-group row">
<div class="col-md-1 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_email.gif") %>
</div>
<div class="col-md-11 col-sm-12">
<%= f.email_field :email, class: "form-control shadow rounded", placeholder: "Email" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-1 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-11 col-sm-12">
<%= f.password_field :password, class: "form-control shadow rounded", placeholder: "Enter your password" %>
</div>
</div>
<div class="form-group row justify-content-center">
<div class="col-12">
<%= f.submit "Log in", class: "btn btn-outline-light btn-lg" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
<div class="col-1 text-center">
<%= image_tag("layout/login/login_divider.gif") %>
</div>
<div class="col-sm-12 col-md-5">
<div class="row border-0">
<div class="col-12 modal-content-subhead">
Join
</div>
</div>
<div class="row">
<div class="col-12 modal-content">
<%= form_with(model: #user, class: "shadow p-3 mb-3 rounded text-light", local: true) do |f| %>
<div class="form-group row">
<div class="col-md-1 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_name_37_33.gif") %>
</div>
<div class="col-md-11 col-sm-12">
<%= f.text_field :first_name, class: "form-control shadow rounded", placeholder: "First Name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-1 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_name_37_33.gif") %>
</div>
<div class="col-md-11 col-sm-12">
<%= f.text_field :last_name, class: "form-control shadow rounded", placeholder: "Last Name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-1 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_email.gif") %>
</div>
<div class="col-md-11 col-sm-12">
<%= f.email_field :email, class: "form-control shadow rounded", placeholder: "Email" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-1 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-11 col-sm-12">
<%= f.password_field :password, class: "form-control shadow rounded", placeholder: "Enter your password" %>
</div>
</div>
<div class="form-group row justify-content-center">
<div class="col-12">
<%= f.submit "Log in", class: "btn btn-outline-light btn-lg" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-11 text-center">
<%=link_to( image_tag("layout/facebook_login_200_42.gif"), user_facebook_omniauth_authorize_path) %>
</div>
</div>
</div>
</div>
</div>
</div>
---
Here is my routes.rb
~~~code
Rails.application.routes.draw do
devise_for :users, controllers: {omniauth_callbacks: 'omniauth'}
root 'pages#index'
get 'about', to: 'pages#about'
resources :users, param: :username
resources :clubs, param: :club_username
end
I'm happy to create my own or use the devise with some modifications but I just can't figure out how to do either.
Any help would be gretly appreciated.
Thanks,
Scott
I did some more searching and was able to get it to work. I uninstalled devise and tried it but couldn't get it to work so I reinstalled devise and omniauth and find an explanation to incorporating the devise forms into my app.
I installed the devise bootstrap views then in the layouts folder I created a partial named _login_html.erb and copied the login form and the sign upforms side-by-side:
<div class="modal fade" id="login" tabindex="-1" aria-labelledby="login" aria-hidden="true">
<div class="modal-dialog modal-lg border-0">
<div class="modal-content border-0">
<div class="modal-content-head">
<h5 class="modal-title" id="login">Member Login</h5>
<button type="button" class="close text-right" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-sm-12 col-md-4">
<div class="row border-0">
<div class="col-12 modal-content-subhead">
Login
</div>
</div>
<div class="row">
<div class="col modal-content">
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :remote => true) do |f| %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_email.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.email_field :email, class: "form-control shadow rounded", placeholder: "Email", :autofocus => true, class: "form-control shadow rounded", placeholder: "Email" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.password_field :password, class: "form-control shadow rounded", placeholder: "Password" %>
</div>
</div>
<% if devise_mapping.rememberable? -%>
<div>
<%= f.check_box :remember_me %> <%= f.label :remember_me, class: "form_small_text" %>
</div>
<% end -%>
<div class="form-group row text-center">
<div class="col-12">
<%= f.submit "Sign in", :class => 'btn reg-submit-btn' %>
</div>
</div>
<div class="modal-footer">
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to t(".forgot_your_password"), new_password_path(resource_name), class: "form_small_text" %><br />
<% end -%>
</div>
<% end %>
</div>
</div>
</div>
<div class="col-sm-12 col-md-1 text-center p-0">
<%= image_tag("layout/login/login_divider.gif") %>
</div>
<div class="col-sm-12 col-md-7">
<div class="row border-0">
<div class="col-12 modal-content-subhead">
Sign-Up
</div>
</div>
<div class="row">
<div class="col modal-content">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), id: "form", :remote => true) do |f| %>
<%= devise_error_messages! %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_name_37_33.gif") %>
</div>
<div class="col-md-5 col-sm-12">
<%= f.text_field :first_name, class: "form-control shadow rounded", placeholder: "First Name" %>
</div>
<div class="col-md-5 col-sm-12">
<%= f.text_field :last_name, class: "form-control shadow rounded", placeholder: "Last Name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_email.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.email_field :email, class: "form-control shadow rounded", placeholder: "Email", :autofocus => true %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<div class="reg_tooltip" data-tooltip="
The club username will be used to easily share your club page on this website.
For example if your club username is my_club the url for your club page on this
site will be www.themathouse.com/clubs/my_club.
">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-question-diamond" fill="white" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M6.95.435c.58-.58 1.52-.58 2.1 0l6.515 6.516c.58.58.58 1.519 0 2.098L9.05 15.565c-.58.58-1.519.58-2.098 0L.435 9.05a1.482 1.482 0 0 1 0-2.098L6.95.435zm1.4.7a.495.495 0 0 0-.7 0L1.134 7.65a.495.495 0 0 0 0 .7l6.516 6.516a.495.495 0 0 0 .7 0l6.516-6.516a.495.495 0 0 0 0-.7L8.35 1.134z"/>
<path d="M5.255 5.786a.237.237 0 0 0 .241.247h.825c.138 0 .248-.113.266-.25.09-.656.54-1.134 1.342-1.134.686 0 1.314.343 1.314 1.168 0 .635-.374.927-.965 1.371-.673.489-1.206 1.06-1.168 1.987l.003.217a.25.25 0 0 0 .25.246h.811a.25.25 0 0 0 .25-.25v-.105c0-.718.273-.927 1.01-1.486.609-.463 1.244-.977 1.244-2.056 0-1.511-1.276-2.241-2.673-2.241-1.267 0-2.655.59-2.75 2.286zm1.557 5.763c0 .533.425.927 1.01.927.609 0 1.028-.394 1.028-.927 0-.552-.42-.94-1.029-.94-.584 0-1.009.388-1.009.94z"/>
</svg>
</div>
<%= image_tag("layout/login/icon_reg_name_37_33.gif") %>
</div>
<div class="form-check col-md-10 col-sm-12">
<%= f.text_field :username, class: "form-control shadow rounded", placeholder: "Username", id: "username" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.password_field :password, class: "form-control shadow rounded", placeholder: "Password" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label p-2">
<%= image_tag("layout/login/icon_reg_locked.gif") %>
</div>
<div class="col-md-10 col-sm-12">
<%= f.password_field :password_confirmation, class: "form-control shadow rounded", placeholder: "Confirm Password" %>
</div>
</div>
<div class="form-group row text-center">
<div class="col-12">
<%= f.submit "Sign up", :class => 'btn reg-submit-btn' %></div></p>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
To make it work I added the following code to the helpers_application.
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
I think that's it.
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 %>
I am using loop in nested forms in rails and i wanted to pass object id through render partial form
app/model/contract_booking_attached_trips.rb
class ContractBookingAttachedTrip < ApplicationRecord
has_many :contract_attached_trip_sheets
accepts_nested_attributes_for :contract_attached_trip_sheets ,allow_destroy: true
belongs_to :booking_history
belongs_to :duty_type
belongs_to :vehicle_make
belongs_to :vehicle_category
end
app/model/contract_attached_trip_sheet.rb
class ContractAttachedTripSheet < ApplicationRecord
belongs_to :contract_booking_attached_trip
end
app/controller/contract_booking_attached_trips_controller
def edit
#contract_booking_attached_trip = ContractBookingAttachedTrip.find(params[:id])
booking_history_id = #contract_booking_attached_trip.booking_history_id
#booking_history = BookingHistory.find(booking_history_id)
#booking_trips = #booking_history.transport_plans
#trip_count = #booking_history.transport_plans.count
#contract_booking_attached_trip.contract_attached_trip_sheets.build
end
app/views/ContractBookingAttachedTrips/_form.html.erb
<div class="container-fluid">
<%= form_for(#contract_booking_attached_trip, :html => {class: "form-horizontal",id: "contract_trip_sheet_validate"}) do |f| %>
<div class="card">
<div class="card-header">
<h5 class="panel-title float-left" style="padding-top: 7.5px;">Duty Slip</h5
</div> <!-- /.card header -->
<div class="card-body">
<div class="card">
<div class="card-body" style="margin-bottom: -50px;">
<div class="row">
<div class="col-md-6 col-lg-6 mb-4 mb-lg-4">
<div class="table-responsive ">
<!--Table-->
<table id="dataTable" class="table table-borderless table-sm" cellspacing="0">
<!--Table body-->
<tbody>
<tr>
<th>Customer</th>
<th><b><%= User.find(#contract_booking_attached_trip.booking_history.try(:user_id)).try(:username) %></b></th>
</tr>
</tbody>
<!--Table body-->
</table>
<!--Table-->
</div> <!-- table -->
</div>
<div class="col-md-6 col-lg-6 mb-4 mb-lg-4">
<div class="table-responsive ">
<!--Table-->
</div> <!-- table -->
</div>
</div><!-- row1 -->
</div> <!-- inner card body -->
</div> <!-- inner card -->
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h6 class="float-left">Trip Sheet</h6>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-11">
<% #booking_history.transport_plans.each do |transport_plan| %>
<%= f.fields_for :contract_attached_trip_sheets ,transport_plan do |builder| %>
<h3><%= transport_plan.id %> </h3>
<%= render 'contract_attached_trip_sheet_fields', f: builder, transport_plan: transport_plan %>
<% end %>
<% end %>
</div>
<div class="col-md-1">
<%= link_to_add_association "+", f, :contract_attached_trip_sheets, class: "fa fa-user fa-lg" %>
</div>
</div>
</div> <!-- /.inner card body -->
</div> <!-- /.inner card -->
</div> <!-- /. col 12 -->
</div> <!-- /.inner card row -->
</div>
</div>
<div class="card-footer ">
<div class="btn-group float-right">
<%= f.submit "submit", class:"btn btn-primary btn-sm button", id:"generateInvoice" %>
</div>
</div>
</div><!-- /.card -->
<% end %>
</div> <!-- /.container fluid -->
app/views/contract_booking_attached_trips/_contract_attached_trip_sheet_fields.html.erb
<fieldset>
<div class="nested-fields">
<div class="row">
<%= transport_plan %>
<div class="col-md-2">
<label class="label" for="">Initial Odo Reading<span class="star"></span></label>
<div class="md-form form-group">
<%= f.text_field :initial_odo_reading, class: "form-control" %>
</div>
</div>
<div class="col-md-2">
<label class="label" for=""> Trip Start Date<span class="star"></span></label>
<div class="md-form form-group">
<%= f.text_field :trip_start_date_time, class: "form-control"%>
</div>
</div>
<div class="col-md-2">
<label class="label" for=""> Final Odo Reading<span class="star"></span></label>
<div class="md-form form-group">
<%= f.text_field :final_odo_reading, class: "form-control" %>
</div>
</div>
<div class="col-md-2">
<label class="label" for=""> Trip Stop Date<span class="star"></span></label>
<div class="md-form form-group">
<%= f.text_field :trip_stop_date_time, class: "mobile form-control" %>
</div>
</div>
<div class="col-md-2">
<label class="label" for=""> Toll Cost<span class="star"></span></label>
<div class="md-form form-group">
<%= f.text_field :toll_cost, class: "form-control", placeholder:"" %>
</div>
</div>
<div class="col-md-2">
<label class="label" for=""> Permit Cost<span class="star"></span></label>
<div class="md-form form-group">
<%= f.text_field :permit_cost, class: "form-control", placeholder:"" %>
</div>
</div>
<div class="col-md-2">
<label class="label" for="">Parking Cost<span class="star"></span></label>
<div class="md-form form-group">
<%= f.text_field :parking_cost, class: "form-control", placeholder:"" %>
</div>
</div>
<%= f.hidden_field :transport_plan_id, value: transport_plan %>
<div class="col-md-1">
<br/>
<%= link_to_remove_association " " ,f, class:"fa fa-trash ", style: "color:red; font-size:20px; top:14px; " %>
</div>
</div>
<br />
</div>
</fieldset>
i am tryinh to pass transport_plan_id in hidden field but am gettting the following error in
please tell me how to pass the id through nested render partial
In your _form you write
<%= render 'contract_attached_trip_sheet_fields', :f => builder,{ :transport_plan =>transport_plan} %>
and that should be
<%= render 'contract_attached_trip_sheet_fields', :f => builder, :transport_plan =>transport_plan %>
There are two ways to pass data to partials, either you write:
render partial: 'partial_name', locals: {f: builder, a: 123}
or the simple form is just
render 'partial_name', f: builder, transport_plan: transport_plan
Is it possible to add params to url while rendering view on controller?
Here I'm trying to add tab=2 on the url.So that my new url looks like localhost:3000/organizations/new?tab=2
I tried the following code
render 'organizations/new', tab: 2
but it neither change the URL nor add the params on URL it just render the view.
I also tried render 'organizations/new?tab=2' It throws ActionView::MissingTemplate.
EDIT 1
Here is my organization/new.html.erb code
<!-- START PAGE CONTAINER -->
<div class="page-container page-navigation-top-fixed" id="organization-new">
<!-- PAGE CONTENT -->
<%= render 'shared/navigation' %>
<div class="page-content">
<%= render 'shared/header' %>
<!-- PAGE CONTENT WRAPPER -->
<div class="page-content-wrap">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default tab-panel">
<div class="panel-body">
<div class="tab-container" id="tabs">
<ul class="nav nav-tabs" role="tablist">
<li class="active">
<a href="#tab-1" data-toggle="tab">
<%= t('admins.create_organization') %>
</a>
</li>
<li class="disabled">
<a href="#tab-2" data-toggle="tab" class="disabled">
<%= t('organization.add_profile') %>
</a>
</li>
</ul>
<div class="org tab-content">
<div class="tab-pane fade in active" id="tab-1">
<h3></h3>
<%= render 'organizations/add_edit_org_form' %>
</div>
<div class="tab-pane fade" id="tab-2">
<h3></h3>
<%= render 'organizations/add_business_profile_tab' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- END PAGE CONTENT WRAPPER -->
<%= render 'shared/footer' %>
</div>
<!-- END PAGE CONTENT -->
</div>
<!-- END PAGE CONTAINER -->
here is my 'organizations/add_edit_org_form' partial which is renders on first tab
<%= form_for (#organization), html: {class: 'form-horizontal organization-form', id: 'add_edit_organization'} do |f| %>
<div class="form-group">
<%= f.label(:name, t('organization.label_name'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.text_field(:name, class: 'form-control', organization_id: #organization.id) %>
<span class="org-error-block text-danger text-danger" id="organization_name_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:abbreviation, t('organization.label_abbr'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.text_field(:abbreviation, class: 'form-control', organization_id: #organization.id) %>
<span class="org-error-block text-danger text-danger" id="organization_abbreviation_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:organization_type, t('organization.label_type'), class: 'col-md-3
col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.select(:organization_type, options_for_organization_type, {}, class: 'form-control select') %>
<span class="org-error-block text-danger text-danger" id="organization_type_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:primary_currency, t('organization.label_primary_currency'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.select(:primary_currency, options_for_primary_currency, {}, class: 'form-control select') %>
<span class="org-error-block text-danger text-danger" id="organization_primary_currency_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:status, t('organization.label_status'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.select(:status, options_for_status, {}, class: 'form-control select') %>
<span class="org-error-block text-danger text-danger" id="organization_status_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:country_id, t('organization.label_country'), class: 'col-md-3 col-xs-12
control-label') %>
<div class="col-md-4 col-xs-12 select-country-options">
<%= f.select(:country_id, options_for_countries, {}, class: 'form-control select') %>
<span class="org-error-block text-danger text-danger" id="organization_country_id_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:payment_method_id, t('organization.label_payment_method'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.select(:payment_method_id, options_for_payment_methods, {}, class: 'form-control select') %>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-primary ajax-spinner pull-right">
<%= t('organization.label_submit') %>
<span class="fa fa-floppy-o fa-right"></span>
</button>
<i class="fa fa-spinner fa-pulse fa-2x pull-right"></i>
</div>
<% end %>
and here is my another partials 'organizations/add_business_profile_tab' which is renders on second tab
<%= form_for (#business_profile), html: {class: 'form-horizontal', id: 'add_business_profile', multipart: true} do |f| %>
<div class="form-group">
<label class="col-md-3 col-xs-12 control-label"><%= t('business_profile.upload_your_logo') %></label>
<div class="col-md-4 col-xs-12 ">
<div class="image-frame" id="upload_logo_div">
<%= image_tag 'upload.jpg', class: 'image-frame', id: 'logo_image' %>
</div>
<%= f.file_field(:logo, id: 'logo_upload_btn') %>
<span class="org-error-block text-danger text-danger" id="logo_upload_btn_error"></span>
<div id="date_picker_container"></div>
</div>
</div>
<div class="form-group">
<%= f.hidden_field(:organization_id, value: #organization.id, id: 'organization_id') %>
<%= f.label(:email, t('business_profile.email'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.text_field(:email, class: 'form-control', organization_id: #organization_id) %>
<span class="org-error-block text-danger text-danger" id="business_profile_email_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:address, t('business_profile.address'), class: 'col-md-3
col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.text_field(:address, class: 'form-control') %>
<span class="org-error-block text-danger text-danger" id="business_profile_address_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:phone, t('business_profile.phone'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.text_field(:phone, class: 'form-control') %>
<span class="org-error-block text-danger text-danger" id="business_profile_phone_error"></span>
</div>
</div>
<div class="form-group">
<%= f.label(:contact_person_name, t('business_profile.contact_person_name'), class: 'col-md-3 col-xs-12 control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.text_field(:contact_person_name, class: 'form-control') %>
<span class="org-error-block text-danger text-danger" id="business_profile_contact_person_name_error"></span>
</div>
</div>
<div class="form-group ">
<%= f.label(:registration_date, t('business_profile.registration_date'), class: 'col-md-3 col-xs-12
control-label') %>
<div class="col-md-4 col-xs-12">
<%= f.text_field(:registration_date, class: 'form-control', autocomplete: 'off', value: #business_profile.registration_date.nil? ? '' : #business_profile.registration_date.strftime('%m/%d/%Y')) %>
<span class="org-error-block text-danger text-danger" id="business_profile_registration_date_error"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-4 col-xs-12">
<%= f.hidden_field(:organization_id) %>
<input type="hidden" value=<%= Date.today.strftime('%Y-%m-%d') %> name="today_date" id="today_date">
<span class="org-error-block text-danger text-danger" id="business_profile_organization_id_error"></span>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-primary pull-right">
<%= t('business_profile.label_submit') %>
<span class="fa fa-floppy-o fa-right"></span>
</button>
</div>
NOTE
I want to be stay on second tab after the form submission on both success and failure cases.
try this one.
render 'organizations/new', :locals => {tab:2}
Another solution:
On new organizations link pass the tab like
link_to 'New Organization', new_organization_path(tab: 2)
This will exactly same as you want
localhost:3000/organizations/new?tab=2
Normally when render method is executed in controller, it parses the targeted view template in your case organizations/new. It has nothing to do with changing the HTTP request URL and params.
instead you do like this
form_for (#organization, url: organization_path(#organization, tab: 2) )
This will make the params tab=2 in the address bar after the completion of the request.
Moreover, it looks like the term tab=2 is not fixed, it might change at run time so you may use jQuery to modify the action attribute of the form like
$('#target_form').attr('action', old_value + '?tab=2');