hey trying figure out why when trying create article, database still creating article though :title , :description coming in "". meaning in front end can hit create button , eventhough each field empty in "", article still created , thats dont want happen. if :title param or :description param == "" want render error not create article doing.
new.hmtl.erb
<style> body { background-color: white !important;; } </style> <h1>create article</h1> <% if @article.errors.any? %> <h2>the following errors have kept article being created</h2> <ul> <% @article.errors.full_messages.each |msg| %> <li><%= msg %></li> <%end %> </ul> <%end %> <%= form_for @article |f| %> <p> <%=f.label :title%> <%=f.text_field :title%> </p> <p> <%=f.label :description%> <br/> <%= f.text_area :description%> </p> <p><%= f.submit %></p> <% end %>
articlescontroller.rb
class articlescontroller < applicationcontroller def new @article = article.new end def create @article = article.new(article_params) if @article.save flash[:notice] = "article created" redirect_to article_path(@article) else render 'new' end end def show @article = article.find(params[:id]) end private def article_params params.require(:article).permit(:title, :description) end end
are validating these fields presence in model? code expects. otherwise you'll need add other validation, perhaps in form well.
validates :description, presence: true validates :title, presence: true
http://guides.rubyonrails.org/active_record_validations.html
Comments
Post a Comment