编辑页面:嵌入式ruby的第一行给了我麻烦
编辑帖子
<%= form_for @post do |f|%> (this line is the problem)
<p>
<%= f.label :title %>
<%= f.text_field :title%>
</p>
<p>
<%= f.label :content %>
<%= f.text_area :content%>
</p>
<p>
<%= f.submit "Update post"%>
</p>
<% end %>
看看edit post
,这是问题吗?好像我在做
everthimg对,我只是想不出来。
后置控制器
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new(params[:id])
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to posts_path, :notice => "your post was saved"
else
render "new"
end
end
private
def post_params
params.require(:post).permit(:title, :content)
end
def edit
@post = Post.find(params[:id])
end
答案 0 :(得分:0)
edit
操作不可见,因为它是private
并且未执行。因此@post
是nil
。将其移至private
来电
def edit
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :content)
end