因此,当我在创建新博客时单击“提交”时,所有人都可以在我的博客表单中的屏幕截图中看到参数已发送但实际上从未插入到MySQL命令中。除了Timestamp之外,您可以在后台看到该帖子是空的。
现在可以说,我的编辑博客页面使用了相同的表单。我的表单是在新博客页面和编辑博客页面中部分呈现的。因此,如果我编辑同一个博客页面,该页面是在没有输入编辑的情况下创建的,那么参数实际上会经过,然后博客文章将显示编辑中的所有信息。
class PostsController < ApplicationController
before_action :find_post, only: [:edit, :update, :show, :delete]
# Index action to render all posts
def index
@posts = Post.all
end
# New action for creating post
def new
@post = Post.new
end
# Create action saves the post into database
def create
@post = Post.new
if @post.save(post_params)
flash[:notice] = "Successfully created post!"
redirect_to post_path(@post)
else
flash[:alert] = "Error creating new post!"
render :new
end
end
# Edit action retrives the post and renders the edit page
def edit
end
# Update action updates the post with the new information
def update
if @post.update_attributes(post_params)
flash[:notice] = "Successfully updated post!"
redirect_to post_path(@post)
else
flash[:alert] = "Error updating post!"
render :edit
end
end
# The show action renders the individual post after retrieving the the id
def show
end
# The destroy action removes the post permanently from the database
def destroy
if @post.destroy
flash[:notice] = "Successfully deleted post!"
redirect_to posts_path
else
flash[:alert] = "Error updating post!"
end
end
private
def post_params
params.require(:post).permit(:strain, :straintype, :style, :light, :temp, :breeder, :private, :notes, :nutrients)
end
def find_post
@post = Post.find(params[:id])
end
end
我只是不明白为什么编辑会正确保存但没有初始提交。
答案 0 :(得分:-1)
我想也许应该是.create(post_params)而不是.new
http://guides.rubyonrails.org/active_record_basics.html#create
Rails: Difference between create and new methods in ActiveRecord?