我有一個狀態更新和註釋數據庫表。保存與狀態更新關係的評論對象.build ruby
用戶有很多狀態更新,狀態更新有很多評論。與Facebook類似,當用戶朋友轉到用戶供稿頁面(顯示頁面)時,他們應該能夠對用戶狀態更新發表評論。
我遇到了保存用戶朋友評論的問題..我的代碼在下面..我認爲它與評論控制器,創建方法有關「@comment = @ statusupdate.comments.build(params [ :comment])「
任何指導非常感謝!謝謝!
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@statusupdates = @user.statusupdates.paginate(:page => params[:page], :per_page => 25)
@statusupdate = Statusupdate.new
@comment = Comment.new
end
end
show.html.erb
<% form_for @statusupdate do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.text_field :content %>
</div>
<% @statusupdates.each do |s| %>
<%= s.content %><br />
<% form_for @comment do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.text_field :comment %>
</div>
<div class="field">
<%= f.hidden_field :user_id, :value => current_user.id %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<br><br>
<% end %>
<% end %>
class CommentsController < ApplicationController
def create
@comment = @statusupdate.comments.build(params[:comment])
if @comment.save
flash[:success] = "Comment created!"
redirect_to root_path
else
@feed_items = []
render 'pages/home'
end
end
end
你可以提供更多的細節問題是什麼?像Rails給出的錯誤是什麼? – dteoh 2010-11-22 03:41:32