2014-11-06 46 views
0

我一直在这个问题上停留了一段时间,并且在解决方案后搜索了解决方案,到目前为止,我遇到的问题都无法解决。rails param丢失或者值为空:post

我为我的应用程序创建了一个新的对象的导轨脚手架,我设置了表格,并在运行服务器时查看Post对象。

,当我去查看时,我的服务器运行后对象的问题,它的内容不显示,我不能更新这个错误被抛出的对象:数据库长相

param is missing or the value is empty: post 

Posts table像这样:

posts table

正如你可以看到,现场content有我想要显示我的网页上,当我去查看后说的数据。

这是出现相反:

what i see when running the server

,你可以看到,它不会显示content字段的内容。

这是我posts_controller.rb

class PostsController < ApplicationController 
    # you may want to udpate these posts? typo's outdated info? 
    before_action :set_post, only: [:show, :edit, :update, :destroy] 

    # GET /posts 
    # GET /posts.json 
    def index 
    @posts = Post.all 
    end 

    # GET /posts/1 
    # GET /posts/1.json 
    def show 
    end 

    # GET /posts/new 
    def new 
    @post = Post.new 
    end 

    # GET /posts/1/edit 
    def edit 
    end 

    # POST /posts 
    # POST /posts.json 
    def create 
    @post = Post.new(post_params) 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @post } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /posts/1 
    # PATCH/PUT /posts/1.json 
    def update 


    respond_to do |format| 
     if @post.update(post_params) 
     format.html { redirect_to @post, notice: 'Post was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.json 
    def destroy 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to posts_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_post 
     @post = Post.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def post_params 
     params.require(:post).permit(:content) 
    end 
end 

我认为这将让我看到了content

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

,我想这是什么定义before_action :set_post, only: [:show, :edit, :update, :destroy]:post因为

def set_post 
    @post = Post.find(params[:id]) 
end 

这是什么 显示错误页面上轨:

{"utf8"=>"✓", 
"_method"=>"patch", 
"authenticity_token"=>"tHFQAWv08aUoi7ndZsVC1aCNJkYP6hE8w+89NTTpBjc=", 
"commit"=>"Update Post", 
"id"=>"2"} 

我真的很困惑,为什么发生这种情况,进出口新的轨道,从而毫无疑问我在想念着一些基本的东西。

谢谢, 克里斯。

+0

你可以把你的html代码,因为那是我认为的问题是 – 2014-11-06 00:50:51

+0

@OmarMowafi现在没有必要!你提到这让我记得我需要更新html,所以谢谢! – 2014-11-06 01:07:37

+3

不客气。我认为你应该关闭这个问题,或者用你失去的东西来更新它。 – 2014-11-06 01:12:28

回答

0

您的请求缺少post PARAM:

{"utf8"=>"✓", 
"_method"=>"patch", 
"authenticity_token"=>"tHFQAWv08aUoi7ndZsVC1aCNJkYP6hE8w+89NTTpBjc=", 
"commit"=>"Update Post", 
"id"=>"2"} 

这很可能是由于形式的错误在你app/views/posts/edit.html.erb视图。表格应该看起来像这样:

<%= form_for @post, url: {action: "edit"} do |f| %> 
    <%= f.text_area :content %> 
<%= end %> 

您可以在rails guides中阅读更多关于表格的信息。

相关问题