将嵌套属性与用户相关联
[英] Associating nested attributes to user
本文介绍了将嵌套属性与用户相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Rails 4.1 构建一个小额费用跟踪应用程序.使用设备进行授权.费用及其嵌套属性,评论属于用户.在模型中建立关联,费用与用户相关联.这是费用控制器:
class ExpensesController <应用控制器定义新@expense = Expense.new@item = @expense.items.build#@comment = @expense.comments.build结尾定义索引@expenses = Expense.all#@items = Item.where(:expense_id => @expense.id)结尾高清秀@expense = Expense.find(params[:id])@items = Item.where(:expense_id =>@expense.id)结尾定义创建@expense = current_user.expenses.new(expense_params)response_to do |格式|如果@expense.saveExpenseMailer.expense_submission(@expense).deliverformat.html { redirect_to @expense,注意:'费用报告已提交.'}format.json { 渲染:显示,状态::创建,位置:@expense }别的format.html { 渲染:新}format.json { 渲染 json: @expense.errors, 状态: :unprocessable_entity }结尾结尾结尾定义编辑@expense = Expense.find(params[:id])结尾定义更新@expense = Expense.find(params[:id])#@comment = @expense.comments.build如果@expense.update(expense_params)#if @comment.save#ExpenseMailer.comments_ added(@expense).deliverflash[:notice] = "费用报告更新"重定向到费用路径#别的# flash[:notice] = "费用报告更新"#redirect_to费用_路径##结尾别的渲染编辑"结尾结尾
构建注释属性的表单如下所示:
<%=nested_form_for (@expense) do |f|%><div class="form-group"><%= f.label :state %><br/><%= f.select :state, Expense.states, :include_blank =>假,类:表单控件"%>
<%= f.fields_for :comments, @expense.comments.build 做 |comment|%><div class="form-group"><%= comment.label :comment%><%= comment.text_area :comment, class: "form-control" %>
<%= comment.hidden_field :commenter %><%结束%><%= f.submit "Submit", class: "btn btn-primary" %><%结束%>
@comment.commenter = current_user 不会将当前用户 ID 添加到数据库中.我应该将它包含在费用控制器的某个地方吗?
解决方案
您必须添加:
@comment.commenter = current_user
低于 if 语句.像这样:
def 创建@article = Expense.find(params[:expense_id])如果@comment = @expense.comments.create(comment_params)@comment.commenter = current_user@comment.saveExpenseMailer.comments_ added(@expense).deliver重定向到费用路径结尾结尾
然后再次保存评论.在您当前的代码中,您通过执行以下操作使用新创建的对象覆盖 @comment
对象:
@comment = @expense.comments.create(comment_params)
但是您还没有在该新对象上的任何地方设置 commenter
.
I'm trying to build a small expense tracking app using Rails 4.1. Using devise for authorization. Expense and it's nested attribute, comments belong to a user. The associations are set up in the model and expenses are getting associated with the user. Here's the Expense controller:
class ExpensesController < ApplicationController
def new
@expense = Expense.new
@item = @expense.items.build
#@comment = @expense.comments.build
end
def index
@expenses = Expense.all
#@items = Item.where(:expense_id => @expense.id)
end
def show
@expense = Expense.find(params[:id])
@items = Item.where(:expense_id => @expense.id)
end
def create
@expense = current_user.expenses.new(expense_params)
respond_to do |format|
if @expense.save
ExpenseMailer.expense_submission(@expense).deliver
format.html { redirect_to @expense, notice: 'Expense Report Submitted.' }
format.json { render :show, status: :created, location: @expense }
else
format.html { render :new }
format.json { render json: @expense.errors, status: :unprocessable_entity }
end
end
end
def edit
@expense = Expense.find(params[:id])
end
def update
@expense = Expense.find(params[:id])
#@comment = @expense.comments.build
if @expense.update(expense_params)
#if @comment.save
#ExpenseMailer.comments_added(@expense).deliver
flash[:notice] = "Expense Report Updated"
redirect_to expenses_path
#else
# flash[:notice] = "Expense Report Updated"
#redirect_to expenses_path
##end
else
render 'edit'
end
end
The form from where the comment attributes are built looks like:
<%= nested_form_for (@expense) do |f| %>
<div class="form-group">
<%= f.label :state %><br />
<%= f.select :state, Expense.states, :include_blank => false, class: "form-control" %>
</div>
<%= f.fields_for :comments, @expense.comments.build do |comment| %>
<div class="form-group">
<%= comment.label :comment%>
<%= comment.text_area :comment, class: "form-control" %>
</div>
<%= comment.hidden_field :commenter %>
<% end %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
The @comment.commenter = current_user isn't adding the current user id to the database. Should I include it in the expense controller somewhere?
解决方案
You have to add:
@comment.commenter = current_user
below that if statement. Like this:
def create
@article = Expense.find(params[:expense_id])
if @comment = @expense.comments.create(comment_params)
@comment.commenter = current_user
@comment.save
ExpenseMailer.comments_added(@expense).deliver
redirect_to expenses_path
end
end
And then save the comment again. In your current code you're overwriting the @comment
object with the newly created object by doing:
@comment = @expense.comments.create(comment_params)
but you haven't set the commenter
on that new object anywhere yet.
这篇关于将嵌套属性与用户相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!