说我的嵌入式红宝石是零或空的

时间:2013-12-13 05:09:58

标签: ruby-on-rails ruby eruby

编辑页面:嵌入式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

1 个答案:

答案 0 :(得分:0)

edit操作不可见,因为它是private并且未执行。因此@postnil。将其移至private来电

上方
def edit
  @post = Post.find(params[:id])
end

private

def post_params
  params.require(:post).permit(:title, :content)
end