我有兩個模型與相應的控制器和視圖:Profile
和Comment
。在一個視圖中顯示來自兩個模型的數據
我的應用程序的整個視圖(整個網頁)在Profile
show.html.erb
。在此頁面上,用戶應該能夠創建評論,其中belongs_to
a Profile
。
無需導航到標準/comments/new
頁面即可完成此操作?
編輯: 繼軌指南後,我實現:
<%= simple_form_for([@profile, @profile.comment.build], html: {class: "form-inline"}) do |f| %>
<%= f.error_notification %>
<%= f.input :description, label: false, placeholder: 'Create an comment', input_html: { class: "span4" } %>
<%= f.submit 'Submit', class: 'btn btn-small'%>
<% end %>
CommentController
def create
@profile = profile.find(params[:profile_id])
@comment = @profile.comments.create(params[:comment])
redirect_to profile_path(@profile)
而且我得到這個錯誤:
undefined method `comment' for #<Profile:
固定:在構建聲明,需要的意見是多
@profile.comments.build
看看這個:http://guides.rubyonrails.org/getting_started.html#generating-a-controller – siekfried 2013-03-22 15:29:47
請參閱上面的錯誤 – mnort9 2013-03-22 16:26:22
你在表單聲明中忘記了's'註釋:'<% = simple_form_for([@ profile,@ profile.comments.build]' – siekfried 2013-03-22 19:09:51