I'll be straight and say that I am fairly new to the concepts relating to website design and development. Right now, I am having an issue that I cannot seem to find a solution to.
I am trying to extend onto mhartl's Ruby on Rails tutorial. I created my own sample app, but with an addition. On the sign up form, I have two radio buttons to determine the account type, which can either be a service provider or a property holder.
Some background on my models. I have a Base User controller and model. My property holder and service provider models inherit from the Base User model using Single Table Inheritance.
If I provide all the correct details and submit a valid signup, all is good, it redirects me to a show page.
The problem arises when I signup incorrectly but have one of the radio buttons for account type checked off. The first time i click submit, the error labels appear and the url changes from /signup to /base_users. If I click submit again, I get an error which says "param is missing or the value is empty: base_user"
Here is some of my code.
Judging from the debug messages I am receiving in the terminal, I have a feeling it has something to do with my application submitting a post request to /service_providers rather than /base_users, but I am unsure of how to fix this, or if it is even the root problem. Can you help me clear up any misunderstandings I have and lead me in the right direction? it would be much appreciated.
add/views/base_users/new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@base_user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<label>
<%= f.radio_button :type, "PropertyHolder", label: "test", class: 'form-control' %>Property Holder
</label>
<label>
<%= f.radio_button :type, "ServiceProvider", label: "test", class: 'form-control' %>Service Provider
</label>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
app/controllers/base_users_controller.rb
class BaseUsersController < ApplicationController
def index
@base_users = BaseUser.where(:type => params[:type])
end
def show
@base_user = BaseUser.find(params[:id])
end
def new
@base_user = BaseUser.new
@base_user.type = params[:type]
end
def create
@base_user = BaseUser.new(user_params)
if @base_user.valid?
if @base_user.type == "PropertyHolder"
@property_holder = @base_user
if @property_holder.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @property_holder
else
render 'new'
end
elsif @base_user.type == "ServiceProvider"
@service_provider = @base_user
if @service_provider.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @service_provider
else
render 'new'
end
else
render 'new'
end
else
render 'new'
end
end
private
def user_params
params.require(:base_user).permit(:name, :email, :password,
:password_confirmation, :type)
end
end
The important parts of my routes file relating to signup is the following:
get 'signup' => 'base_users#new'
resources :base_users
resources :property_holders, :controller => 'base_users', :type => "PropertyHolder"
resources :service_providers, :controller => 'base_users', :type => "ServiceProvider"
Aucun commentaire:
Enregistrer un commentaire