Ask A Question

Notifications

You’re not receiving notifications from this thread.

Validation error not showing up in browser

kartik362 asked in Rails

articles controller

class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
end

def index
    @articles = Article.all
end

def new
    @article = Article.new
end

def create
 @article = Article.new(params.require(:article).permit(:title, :description))
 if @article.save
   redirect_to @article
 else
   render 'new'
 end
end

end

new.html.erb

Create a new article

<% if @article.errors.any? %>

The following errors prevented the article from being saved



    <% @article.errors.full_messages.each do |msg| %>
  • <%= msg %>

  • <% end %>

<% end %>

<%= form_with scope: :article, url: articles_path, local: true do |f|%>

<p>
    <%= f.label :title%><br>
    <%= f.text_field :title%>
</p>

<p>
    <%= f.label :description%><br>
    <%= f.text_area :description%>
</p>

<p>
    <%= f.submit%>
</p>

<% end %>

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.