'Rendering file in create action is not working in rails in windows 10

In my users views new.html.erb

<% provide(:title, 'SignUp') %>

<%= form_with(model: @user, local: true) do |f| %>

<%= f.label :name, "Name" %> 
<%= f.text_field :name %>
<br>
<%= f.label :email, "Email" %>
<%= f.email_field :email %>
<br>

<%= f.label :password, "Password" %>
<%= f.password_field :password %>
<br>

<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<br>

<%= f.submit "Create Account", class: "btn btn-outline-success" %>

<% end %>

In my user controller users_controller.rb looks like:-

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(user_params)
    if @user.invalid?
      puts "Hi"
      flash[:success] = "Invalid Data"
      render 'new'
    else
      flash[:success] = "Welcome to sample app"
      @user.save
      redirect_to @user
    end
  end

  private
    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end

This is my errors file /views/shared/_errors.html.erb

<% if @user.errors.any? %>
    <div class="alert alert-danger">
        The form contains <%= pluralize(@user.errors.count, "error") %>
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
        <li> <%= msg %> </li>
    <% end %>
    </ul>
<% end %>

render 'new' is not working in create action. I include the errors.html.erb file in new file so it can display the error, but it is not working.



Solution 1:[1]

Try form_for instead of form_with.

<%= form_for(@user) do |f| %>

<%= f.label :name, "Name" %> 
<%= f.text_field :name %>
<br>
<%= f.label :email, "Email" %>
<%= f.email_field :email %>
<br>

<%= f.label :password, "Password" %>
<%= f.password_field :password %>
<br>

<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<br>

<%= f.submit "Create Account", class: "btn btn-outline-success" %>

<% end %>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohit