0

我正在使用rails 4.0.8。我給一個名爲'Things'的模型添加了一個註釋部分,但是當我按下submit comment按鈕時,我一直收到同樣的錯誤「param丟失或者值爲空:thing」。它說這個錯誤在Things#Controller中。我究竟做錯了什麼?錯誤:param丟失或值爲空:東西

更新:我從窗體中刪除了url路徑,但是新的錯誤返回「找不到沒有ID的東西」。該錯誤位於註釋#控制器中。

查看THING/SHOW

<div id= "thing"> 
    <h1> 
    <%= @thing.name %> 
    </h1> 
    <br> 
    <div id= "commentsection"> 
    Comments 
    <div id= "comments"> 
     <br> 
     <% @thing.comments.each do |c| %> 
     <%= c.username %> 
     <br> 
     <%= c.text %> 
     <% end %> 
     <%= form_for @comment, :url => thing_path do |f| %> 

     <%= f.label :username %> 
     <%= f.text_field :username %> 

     <%= f.label :comment %> 
     <%= f.text_field :text %> 

     <%= f.submit "Enter", class: "btn btn-small btn-primary" %> 
     <% end %> 
    </div> 
    </div> 
</div> 

THINGS控制器

class ThingsController < ApplicationController 

    def show 
    @thing = Thing.find(params[:id]) 
    @thing.comments.build 
    @comment = Comment.new 
    end 

    def index 
    end 

    def new 
    @thing = Thing.new 
    @things = Thing.all 
    end 

    def create 
    @thing = Thing.new(thing_params) 
    if @thing.save 
     redirect_to @thing 
    else 
     render 'new' 
    end 
    end 

    private 
    def thing_params 
     params.require(:thing).permit(:name, :avatar) 
    end 

end 

評論控制器(我把周圍的線星號錯誤所在)

class CommentsController < ApplicationController 

    def show 
    @comment = Comment.find(params[:id]) 
    end 

    def new 
    @comment = Comment.new 
    @comments = Comment.all 
    end 

    def create 
    ****@thing = Thing.find(params[:thing_id])**** 
    @comment = @thing.comments.create(comment_params) 
    redirect_to thing_path(@thing) 
    end 
    end 

    private 

    def comment_params 
     params.require(:comment).permit(:user, :text, :upvotes, :downvotes, :thing_id) 
    end 

end 

ROUTES

Website::Application.routes.draw do 

    get "comments/new" 
    get "comments/show" 
    get "things/new" 
    root 'home_page#home' 
    get "all/things/new" => 'things#new' 
    get "all/allthings" 
    resources :things 
    resources :good_comments 
    get "things/show" 
    get "things/results" 

end 

回答

0

您正在發佈@comment表格至post '/things' path

<%= form_for @comment, :url => thing_path do |f| %> 

這應該只是<%= form_for @comment do %>(Rails是一個足夠聰明的comments_path堵塞),或者如果你覺得更加明確的(即使它不是必需的)

<%= form_for @comment, url: :comments_path do %> 

另要注意雖然,如果您希望Comment被捆綁到特定Thing那麼在您的模型應該是

Class Thing 
    has_many :comments 
end 

Class Comment 
    belongs_to :thing 
end 

然後確保在你的數據庫commentthing_id foreign_key field,然後您的評論的形式實際上應該看起來像

<%= form_for @thing, @comment do %> <% end %>

+0

謝謝!我刪除了毫無意義的url路徑,但是現在我在Comments#Controller中有一個新錯誤,「找不到沒有ID的東西」。任何想法爲什麼這可能是?我的模型設置正確,每個評論都有一個thing_id。 – 2014-09-27 23:58:15

+0

@ user3739453你的表單必須是'<%= form_for @thing,@comment do%>'在你的'Comment Controller'中你要求'params [:thing_id]',但是如果你檢查了你的表單的url它是'/ comments'。它需要是'/ thing /:thing_id/comments''這也意味着你需要調整你的路線,以包括'資源:事情做資源:評論結束' – kittyminky 2014-09-28 00:04:19

+0

好吧,現在它給了我「不能寫未知的屬性'html'「並突出顯示行<%= form_for @thing,@comment do | f | %>'。 – 2014-09-28 00:16:42

相關問題