我有两个模型与相应的控制器和视图: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