I'm new to Rails and am trying to modify an app that was written by another team. I have a common problem of needing to retain form fields after a failed validation. In our app, the user submits basic info like name, email, and an invitation code. I validate the invitation code in the controller. If it fails, it currently does a redirect_to :back but that loses the info that the user filled in and clears the form. From other pages that I've researched, it seems the proper thing to do is a render :action => :new. However, when I try that I get errors.
This attempt...
format.html { render :action => "new" }
all give the same result of...
Template is missing
Missing template after_step/new, application/new with...
The way the previous developers set it up, Step 1 of the form is at "users/registrations/new", but the following steps are at "after_step/step_two", etc. So, upon form submission, it is going to "after_step_controller.rb" where I am doing my validation. Obviously, I see that it is trying to render the "new" action on that controller instead of the "registrations_controller.rb".
However, when I try to force it with...
format.html { render :template => "registrations_controller/new" }
format.html { render :action => "new", :controller => "registrations" }
...I still get the missing template error. Therefore, I tried other things I found suggested online...
format.html { render :template => "users/registrations/new" }
format.html { render :file => "users/registrations/new.html.erb" }
These didn't work either as they gave this error:
undefined method `model_name' for NilClass:Class
I then researched this and most said that the "new" function wasn't creating the new user. However, the code is there so I'm not sure what is going on.
Can anyone point me in the right direction with this? It's obviously not a show-stopper, but for a better user experience I'd like the form fields to retain their values upon failed validation.
Here's some info...
users/registrations/new.html.erb
<%= form_for(@user, :url => step_two_path) do |f| %>
<div class="signup_step_mid_content">
<div class="step_content_block">
<h3> First, tell us a little bit about yourself:</h3>
<div class="signup_filed_small mrgn_r_20">
<label>First Name</label>
<%= f.text_field :first_name, :title => "First Name", :placeholder => "First Name" %>
</div>
registrations_controller.rb
def new
@user = User.new
@invite_code = params[:invite_code]
end
after_step_controller.rb
def step_two
@user = User.find_by_email(params[:user][:email])
respond_to do |format|
@tester = Prefinery::Tester.new(:beta_id => 1234)
@tester.email = params[:user][:email]
@tester.invitation_code = params[:user][:invitation_code]
@tester.status = 'active'
if [email protected]
#format.html{redirect_to :back }
#format.html { render :template => "registrations_controller/new" }
#format.html { render :action => "new", :controller => "registrations" }
#format.html { render :action => "new" }
#format.html { render :template => "users/registrations/new" }
#format.html { render :file => "users/registrations/new.html.erb" }
Let me know if any further info is needed.
You're right about this step:
format.html { render :template => "users/registrations/new" }
The problem is that you're not setting an instance for @user in the "after_step_controller", if you want to do the render, you should have to set an instance for @user, and it should contains the attributes that were submitted in the form, so you could show validations errors.
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With