それぞれのコントローラーとビューを持つ 2 つのモデルがあります:Profile
とComment
.
私のアプリケーションのビュー全体 (Web ページ全体) はProfile
show.html.erb
. このページで、ユーザーはコメントを作成できる必要がありbelongs_to
ますProfile
。
/comments/new
標準ページに移動せずにこれを行うにはどうすればよいでしょうか?
編集: Railsガイドに従った後、実装しました:
<%= 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 %>
コメントコントローラー
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