I am creating a posting application in Ruby.
I'm using Rails.

I want to use the errors method and validation to display an error message when posting fails or editing fails.

I want to display an error message when posting fails or editing fails due to validation, but I am in trouble because no error is displayed.

There are currently no red screen errors.

There are two problems that are occurring.

(1) Although validation should have been applied so that empty posts cannot be made, new empty posts are completed and I return to the home. Is conditional branching not working (even though the database data is not increasing and the number of posts is not increasing)?

(2) On the posted edit screen, error characters are not displayed even if the specified number of characters is exceeded and an update is made. (It will return to the state before editing due to the limitation of the number of characters in validation)

Corresponding source code

tweets_contoroller.rb ↓

class TweetsController</pre>
<pre><code>Source code

tweets.rb (models) ↓

class Tweet</pre>
<p>new_html.erb ↓</p>
<pre><code>  <% @ tweet.errors.full_messages.each do | message |%>          <% = message%>      <% end%><% = form_with (model: @tweet, local: true) do | form |%>Post     <% = render partial: "form", locals: {form: form}%><% end%><% = render partial: "footer"%>

edit.html.erb ↓

  <% = form_with (model: @tweet, local: true) do | form |%>     Edit     <% @ tweet.errors.full_messages.each do | message |%>              <% = message%>          <% end%>  <% = render partial: "form", locals: {form: form}%><% end%><% = render partial: "footer"%>

For the time being routes.rb ↓

Rails.application.routes.draw do
  devise_for: users
  devise_scope: user do
    post'users/guest_sign_in', to:'users/sessions # new_guest'
  root to:'tweets # index'
  resources: tweets do
    resources: comments, only :: create
    collection do
  resources: users, only :: show
What I tried

① Is there a mistake in the description of the flash message? Or, I tried multiple times to see if it was useless because the description in each action was not written in render, but it has no effect. I don't even know because there is no red screen.

② I try to save the error statement data on the rails console, but the return value does not show true.
What I did on the rails console (in order)
tweet = Tweet.new
false in tweet.save
tweet.content = "arbitrary string"
Tweet.save and false again

Since false is displayed, even if I suspect that something is wrong, I have no idea.

I hypothesized that it wasn't possible because it was an instance variable, changed the code, and tried it, but I couldn't find a solution. I would like to borrow the knowledge of my seniors.

  • Answer # 1

    validation is working.
    The error display shows about @tweet,

    in createif Tweet.create (tweet_params)
    In updatetweet = Tweet.find (params [: id])

    Since neither is @tweet, no error is displayed.

    @tweet = Tweet.new (tweet_params)
    if @ tweet.save
    @tweet = Tweet.find (params [: id])
    if @ tweet.save

    Please comb