2016-09-19 98 views
1

我建立一個Facebook的克隆,我試圖讓每個用戶的頁面上的文本區域,讓他們做出的帖子。我試過沒有成功一大堆不同的東西,但在嘗試訪問用戶的顯示頁面時,現在我收到此錯誤:與此代碼的Rails:顯示用戶頁面上的用戶後的form_for嵌套路線

First argument in form cannot contain nil or be empty 

Rails.application.routes.draw do 
    resources :friends, only: [:index, :destroy] 
    resources :posts 

    resources :friend_requests 
    devise_for :users 
    devise_scope :user do 
     root 'devise/sessions#new' 
    end 

    resources :users, only: [:index, :show] do 
    resources :posts 
    end 

    get 'about', to: 'static_pages#about' 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
    end 

_post_form.html.erb 

<%= form_for [@user, @post] do |f| %> 
<%= f.text_area :content, size: "60x12", placeholder: "What do you want to say?" %> 
<%= f.submit "Post" %> 
<% end %> 

class PostsController < ApplicationController 

def index 
    @posts = Post.all 
end 

def new 
    @post = Post.new 
    @user = User.find(params[:user_id]) 
end 

def create 
    @post = current_user.posts.build(post_params) 
    if @post.save 
     flash[:success] = "Posted!" 
     redirect_to user_path(current_user) 
    else 
     flash[:notice] = "Post could not be submitted" 
     redirect_to users_path 
    end 
end 

private 

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

class UsersController < ApplicationController 

def index 
    @users = User.all 
end 

def show 
    @user = User.find(params[:id]) 
end 

end 

​​
server log: 
Processing by UsersController#show as HTML 
Parameters: {"id"=>"4"} 
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]] 
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]] 
Rendering users/show.html.erb within layouts/application 
FriendRequest Load (0.5ms) SELECT "friend_requests".* FROM "friend_requests" WHERE "friend_requests"."friend_id" = $1 ORDER BY "friend_requests"."id" ASC LIMIT $2 [["friend_id", 4], ["LIMIT", 1000]] 
Rendered users/_notifications.html.erb (2.0ms) 
Rendered shared/_post_form.html.erb (3.0ms) 
Rendered users/show.html.erb within layouts/application (10.2ms) 
Completed 500 Internal Server Error in 23ms (ActiveRecord: 1.3ms) 



ActionView::Template::Error (First argument in form cannot contain nil or be empty): 
1: <%= form_for [@user, @post] do |f| %> 
2: <%= f.text_area :content, size: "60x12", placeholder: "What do you want to say?" %> 
3: <%= f.submit "Post" %> 
4: <% end %>  

    app/views/shared/_post_form.html.erb:1:in `_app_views_shared__post_form_html_erb___99030300856795657_70237723952000' 
    app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb___3196827877852207953_70237724137160' 
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack- 5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout 
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack- 5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb 

渲染/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues /_source.html.erb(6.7ms) 渲染/usr/local/lib/ruby/gems/2.3.0/gems/actionpack- 5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb 渲染/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb(5.0ms)012渲染/ usr/local/lib/ruby /gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb(1.1ms) 渲染/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-`5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb內救援/佈局(96.4ms)

回答

1

您說你的表單在用戶的展示頁面上呈現問題。如果你有這種形式的W /嵌套的資源設置是這樣的:

form_for [@user, @post] 

這意味着你的表格需要同時訪問@user@post實例變量等。無論形式被呈現。在這種情況下,它在用戶控制器中的顯示操作中。所以,你的用戶控制器應該有這樣的事情:

def show 
    @user = User.find(params[:id]) 
    @post = @user.posts.build 
end 
+0

謝謝這給了我很多! –

+0

不客氣:) – Ren

0

我假設你的_post_form加載時,當你去你的posts#new路線其由該帖子控制器操作處理:

def new 
    @post = Post.new 
    @user = User.find_by(id: params[:id]) 
end 

嵌套路由(在這種情況下,用戶>後)將父資源的ID在參數resource_id,在你情況下,將params[:user_id]。因此,從本質上講,改變這一行:

@user = User.find_by(id: params[:id]) 

...到:

@user = User.find(params[:user_id]) 

將訪問在PARAMS正確的ID,並會導致異常,如果沒有用戶發現(通過使用find而不是find_by),在進入視圖渲染之前,會提醒您任何問題。在你的情況下,@user是零,你得到了form_for你發佈的錯誤。

更新

我從你的日誌中看到你要的users#show行動,這是這一個:

def show 
    @user = User.find(params[:id]) 
end 

,你可以看到,你不設置@post變量,你傳遞到這裏的形式:

form_for [@user, @post] 

加入此行爲:

def show 
    @user = User.find(params[:id]) 
    @post = Post.new 
end 
+0

感謝您的答覆。我嘗試實施您所建議的更改,但在嘗試訪問用戶頁面時仍出現同樣的錯誤 –

+0

請張貼您的新代碼,以及您正在瀏覽的路徑以及您的服務器日誌以進行該操作 – DiegoSalazar

+0

好吧我用服務器日誌信息編輯。至於路徑,我認爲我不需要提供一個,因爲rails會看到form_for的東西,並根據[@user,@post]決定它的帖子或補丁以及發佈到哪裏? –