Rails 6 registraion modal with devise - ruby-on-rails

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.

Related

rails 6 error messages not working with sign in/sign up modal

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.

Rails Radio button is only allowing me to select only first option and not the other

What would be the cause of me only being able to select the first radio button, but not the second. Here is what my form looks like.
`
<%= form_for [#item, Calendar.new] do |f| %>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :start_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :end_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
</div>
<div class="form-group">
<div class="btn-group" data-toggle="buttons-radio">
<%= f.collection_radio_buttons :status, Calendar.statuses, :first, :first, checked: Calendar.statuses.first do |b|
b.radio_button + b.label {b.text.split("_").join("").capitalize}
end %>
</div>
</div>
<div class="row new-pricing">
<div class="col-md-10">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">$</span>
<%= f.text_field :item_price, class: "form-control", value: #item.item_price, required: true %>
</div>
</div>
</div>
<div class="col-md-2">
<p style="margin-top: 10px"> Per Day </p>
</div>
</div>
<div class="no-pricing hide">
</div>
<div class="form-group">
<%= f.button "Save", type: :submit, class: "btn btn-success" %>
</div>
<% end %>`
Here is what the html looks like when I click on the "Not available" button. The checked always remains on the first value input button.
The html in the inspection window.
How can I go about fixing this. Sorry if this was an easy question.

Rails: Hide form field when value is popuated

I have this form. When the object has a value for gender, I want to change the gender field to a hidden field, so the user does not need to see it. What is the best way to do this?
<%= form_for( object ,:html => {:class => "form-horizontal"}) do |f| %>
<div class="form-group row">
<%= f.label(:first_name, :class => "control-label col-xs-4 input-lg") %>
<div class="form-inline col-xs-8">
<div class="">
<%= f.text_field( :first_name,{class: "form-control input-lg"}) %>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label(:last_name, :class => "control-label col-xs-4 input-lg") %>
<div class="form-inline col-xs-8">
<div class="">
<%= f.text_field( :last_name,{class: "form-control input-lg"}) %>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
<div class="col-xs-8">
<%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
</div>
</div>
<div class="col-xs-8 col-xs-offset-4">
<%= f.submit object.submit_button_name, {class: 'btn btn-lg'} %>
</div>
<% end %>
You could simply use an if statement to render different field types based on whether gender is set or not:
<% if object.gender %>
<%= f.hidden_field(:gender)
<% else %>
<div class="form-group row">
<%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
<div class="col-xs-8">
<%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
</div>
</div>
<% end %>
Instead of hidding it, it's better to not rendering it. Try it like this:
<% unless object.gender != nil %>
<div class="form-group row">
<%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
<div class="col-xs-8">
<%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
</div>
</div>
<% end %>

Rails hit new method on save of simpleform

So i have this method
def create
#newevent = Event.new(create_params)
#newevent.save!
flash[:success] = "Event Created"
redirect_to "/events"
end
And this form
<% provide(:title, "Edit user") %>
<h1>Editing event:
<%= #newevent.id %></h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= simple_form_for #newevent do |f| %>
<div class="form-group">
<%= f.label :eventname %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventname, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.input :event_type, :collection => ['Concert','Festival','Sports','Theatre'] %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventdesc %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventdesc, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventshortdesc %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventshortdesc, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :pagetitle %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :pagetitle, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :metatag %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :metatag, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventvenuename %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventvenuename, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.input :time, type: "time", :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.input :date, type: "date", :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventimage %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventimage, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.check_box :eventready %>
<%= f.label :eventready, "Is event ready for SEO?" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.check_box :eventcomplete %>
<%= f.label :eventcomplete, "Is event ready for Public?" %>
</div>
</div>
</div>
<%= f.submit "Save changes", class: "btn btn-info" %>
<%= link_to "Delete", event_path(#newevent), :method => :delete, class: "btn btn-danger" %>
<% end %>
</div>
</div>
I'm currently populating this form with the edit method here
def edit
#newevent = Master.find(params[:id])
end
How can i go about making this pull in information from the Master table to auto populate the table but then save to the Event table?
Sam
What about a simple update method?
def update
#newevent = Event.find(params[:id])
if #newevent.update_attributes(params[:newevent])
redirect_to #user
else
render action: "edit"
end
end

Add params to URL on render rails

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');

Resources