我试图避免为@post 更新一个空名称。我是 RoR 的初学者,我不明白为什么在终端我得到了@post.invalid? => true
,但在我看来edit.html.erb
@post.invalid? => false
post_controller.rb
class PostsController < ApplicationController
before_action :set_post, :only => [:edit, :show, :update, :destroy]
def index
@posts = Post.all
respond_to do |format|
format.html
format.json { render json: @posts }
end
end
def show
respond_to do |format|
format.html
format.json { render json: @post }
end
end
def edit
end
def update
if @post.update(post_params)
redirect_to posts_path, success: "Post updated"
else
puts @post.invalid? # write true
render 'edit'
end
end
def new
@post = Post.new
end
def create
post = Post.create(post_params)
redirect_to post_path(post.id), success: "Post created"
end
def destroy
@post.destroy
redirect_to posts_path, success: "Post deleted"
end
private
def post_params
params.require(:post).permit(:name, :content)
end
def set_post
@post = Post.find(params[:id])
end
end
post.rb
class Post < ApplicationRecord
validates :name, presence: true
def as_json(options = nil)
super(only: [:name, :id, :created_at] )
end
end
编辑.html.erb
<h1>Editer l´article</h1>
<%= @post.invalid? %> <!-- write false -->
<% if @post.invalid? %> <!-- return false -->
<div class="alert alert-danger">
<% @post.errors.full_messages.each do |message| %>
<%= message %>
<% end %>
</div>
<% end %>
<%= form_for @post do |f| %>
<div class="form-group">
<label>Titre de l´article</label>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<label>Contenu de l´article</label>
<%= f.text_area :content, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Modifier l´article", class: 'btn btn-primary' %>
</div>
<% end %>
我很困惑,有人有想法吗?