2011-09-12 66 views
1

這是我的路線:如何爲嵌套資源的視圖渲染局部圖像? - Rails的3.1

scope ":username" do 
    resources :feedbacks 
end 

我對我home/index.html.erb看法,我試圖做到這一點:

<%= render "feedbacks/form" %> 

但是這給了我這個錯誤:

ActionView::Template::Error (No route matches {:controller=>"feedbacks", :format=>nil}): 
    1: <%= form_for(@feedback) do |f| %> 
    2: <% if @feedback.errors.any? %> 
    3:  <div id="error_explanation"> 
    4:  <h2><%= pluralize(@feedback.errors.count, "error") %> prohibited this feedback from being saved:</h2> 
    app/views/feedbacks/_form.html.erb:1:in `_app_views_feedbacks__form_html_erb__3181571289116259961_2487495480' 
    app/views/home/index.html.erb:18:in `_app_views_home_index_html_erb___397233383548615486_2487321620' 

這是我的耙路線反饋意見:

 feedbacks GET /:username/feedbacks(.:format)   {:action=>"index", :controller=>"feedbacks"} 
       POST /:username/feedbacks(.:format)   {:action=>"create", :controller=>"feedbacks"} 
    new_feedback GET /:username/feedbacks/new(.:format)  {:action=>"new", :controller=>"feedbacks"} 
    edit_feedback GET /:username/feedbacks/:id/edit(.:format) {:action=>"edit", :controller=>"feedbacks"} 
     feedback GET /:username/feedbacks/:id(.:format)  {:action=>"show", :controller=>"feedbacks"} 
       PUT /:username/feedbacks/:id(.:format)  {:action=>"update", :controller=>"feedbacks"} 
       DELETE /:username/feedbacks/:id(.:format)  {:action=>"destroy", :controller=>"feedbacks"} 

編輯1

當我通過在本地實例變量@feedback這樣的:​​這是從我的home controller,在此聲明如下拉:

class HomeController < ApplicationController 
    def index 
     @users = User.all 
     @feedback = Feedback.new 
    end 

end 

我仍然得到這個錯誤:

Routing Error 

No route matches {:controller=>"feedbacks", :format=>nil} 

編輯2

我也有我的路線文件中指定一個users資源,看起來像這樣:

resources :users 

    scope ":username" do 
     resources :feedbacks 
    end 

回答

2

好像form_for無法找到正確的路線,這條線意味着你沒有一個實例試圖渲染時可變@feedback組部分:

(No route matches {:controller=>"feedbacks", :format=>nil}) 

公告不存在:id參數散列(這是很可能被傳遞到路由器生成正確的UR L將表格提交給)。

您是否在控制器動作(更可取)或您正在使用的視圖中的某處定義了@feedback?如果你認爲你,試試你的偏下述以上線路1:

logger.info "feedback object: #{@feedback.inspect}" 

編輯

首先,本地人應該沒有通過爲:local => @feedback,但爲:

:locals => {:variable_name_in_partial => value_for_variable_name} 

其次,您試圖訪問@feedback,即使它沒有明確傳遞(只要它已設置),它也是一個可供視圖訪問的實例變量。所以只要你在HomeController#index方法中設置它,你不需要將它傳遞給本地任何視圖。

此外,您沒有使用正確的語法來渲染partials(希望我以前已經注意到!)。

更改如下:

<%= render "feedbacks/form" %> 

要這樣:

<%= render :partial => "feedbacks/form" %> 
+0

你可能是對的。這是我從'logger.info'中看到的:'feedback object:# marcamillion

+0

我更新了問題以反映我嘗試過的其他事情。 – marcamillion

+0

就是這樣,我在動作中(@ home&'feedback'控制器)聲明瞭'@feedback = Feedback.new',因爲我調用了部分的視圖。它仍然給我這個錯誤,即使在我用你的語法。此外,我的語法是正確的:http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials但我仍然嘗試了你的語法,但仍然收到錯誤。 – marcamillion

1

你沒有忘記在:username通過?您的路線說scope :username

如果我添加像你這樣的路線,開闢了Rails控制檯,include Rails.application.routes.url_helpers和運行feedbacks_path,我得到一個路由錯誤,但feedbacks_path :username => 'bob'工作正常。

> feedbacks_path(:username => 'bob') 
=> "/bob/feedbacks" 
+0

Hrmm ...這個問題是部分應該允許我創建一個新的'feedback'(我指定哪個'user')負責反饋。因此,部分表單有兩個'user_id'字段,所以不確定在這種情況下傳遞':username =>'bob''會起作用 – marcamillion

+0

也許你需要一個到作用域之外的資源的路由:用戶名,然後 –

0

您確定要範圍?怎麼樣一個嵌套的資源按照:

resources :users do 
    resources :feedbacks 
end 

此外,我有麻煩時,使用複數名稱的控制器。您可以嘗試將FeedbacksController重命名爲FeedbackController

+0

你能解釋一下我的範圍和嵌套資源之間的區別?爲什麼我需要一個,而不是另一個? – marcamillion

+0

另外,有沒有我可以處理重命名的Rails方式?當我走時,我生成了一個腳手架並定製了所有的東西。所以我不能只更改控制器名稱。我將不得不改變路線,型號名稱,控制器名稱,所有路徑等。是否有一種簡單的方法可以一舉完成所有事情?即一個命令? – marcamillion

+0

我在最後用Edit 2更新了這個問題,它顯示了我更多的'routes.rb'文件。即我的'範圍:用戶名'在我的路線文件中有'resources:users'。我是多餘的嗎?另外,請記住,我想要實現的目標是我的網址看起來像'mydomain.com/joe-blow/feedback/1234',其中'joe-blow'是'user = Joe'的'用戶名'。這與每個用戶都有所不同,就像Twitter網址一樣。如果你有更好的建議,我很樂意聽到它。謝謝。 – marcamillion