Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No template found for UsersController#create, rendering head :no_content

OK, previously I had a problem with a no template error from users#create, now it complete 200 OK however does not redirect at all. Below is my edited users_controller.rb

I have a Signup, Login, Logout rails application with users as a resource. I am trying to save the first user in the database so I can then login but this error is server output when I try to "users#new" and "users#create" the full error is below, then my users_controller.rb and views/users -> new.html.erb

No template found for UsersController#create, rendering head :no_content
Completed 204 No Content in 35ms (ActiveRecord: 0.5ms)

users_controller.rb

def new
  @user = User.new
end

def create
  @user = User.new(user_params)
  if (@user = User.find_by_email(params[:email]))
    flash[:success] = "User already exists."
    if @user.save
      session[:user_id] = user.id
      flash[:success] = "New User created."
      redirect_to '/layouts/application'
    else
      render 'new'
    end
  end
end

new.html.erb

<h1>Sign Up</h1>
<%= form_with(model: @user) do |f| %>
<p> Username:</br> <%= f.text_field :username %> </p>
<p> Email:</br> <%= f.text_field :email %> </p>
<p> Password:</br> <%= f.password_field :password%></p>

<%= f.submit "Signup" %>
<% end %>
<% if @user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in @user.errors.full_messages %>
  <li>* <%= message_error %></li>
<% end %>
</ul>
<% end %>
</div>

Do I have to have another html.erb file? And how can I tell what that has to be? Sorry for the obvious question, newb here.

like image 581
nephilim Avatar asked Mar 27 '18 02:03

nephilim


People also ask

Why won't my template show up in the list?

This prevents ALL existing templates from being usable (thanks Microsoft!) the solution is to create a copy of the template you need (usually Web Server) and make sure in its properties that it is usable by 2003 and above, then make sure to "issue" the new template and it should show up in the list if that was the problem. This is misleading.

How to avoid if else condition in a template?

To avoid this you must redirect it somewhere or render a template which you have done in the next if and else but it's not executing. The condition is not letting it redirect to anywhere.

Why are there no certificate templates available for the Web Enrollment pages?

No certificate templates could be found. You do not have permission to request a certificate from this CA, or an error occurred while accessing the Active Directory. This behavior occurs if the Web enrollment pages are in an Active Directory domain on an Enterprise CA server.

How to avoid'IF ELSE'block when rendering a template?

To avoid this you must redirect it somewhere or render a template which you have done in the next if and else but it's not executing. The condition is not letting it redirect to anywhere. Try moving the if block out like this.


1 Answers

As per your code if the User is not present it will not enter in the if block. Rails end up trying to find create.html as the current action is create.

To avoid this you must redirect it somewhere or render a template which you have done in the next if and else but it's not executing.

The condition is not letting it redirect to anywhere. Try moving the if block out like this.

def create
  @user = User.new(user_params)
  if User.exists?(email: params[:email]) # I think this should be `user_params[:email]` instead of `params[:email]`
    flash[:error] = "User already exists." 
    redirect_to 'whereever/you/want/to/redirect' and return
  end
  if @user.save
    session[:user_id] = user.id
    flash[:success] = "New User created."
    redirect_to '/layouts/application'
  else
    render 'new'
  end
end
like image 161
Deepak Mahakale Avatar answered Sep 26 '22 06:09

Deepak Mahakale