【问题标题】:Rails 'best in place gem', update only one rowRails '最佳位置 gem',只更新一行
【发布时间】:2014-05-17 03:20:55
【问题描述】:

我将 Best in Place gem 与 rails 4 应用程序一起使用,并从显示给用户的数据库表中获取多条记录。最初,我的数据库没有更新,但有了Undefined method update_attributes? 解决方案,我的数据库现在更新了。

我当前的问题是:当用户编辑特定行时,我希望更新数据库中的该行。现在更新的字段会导致数据库中的所有行都被更新。

这是我希望用户编辑的表格的代码,它呈现在视图中:

<tbody>

<% @userdailydata.each do |data| %>
<tr>
<td> <%= best_in_place data, :date,:display_with => lambda { |v| v.strftime("%Y-%m-%d") }  %> </td>
<td> <%= best_in_place data, :calories_consumed %> </td>
<td> <%= best_in_place data, :calories_exercised %> </td>
<td> <%= best_in_place data, :weight %> </td>
</tr> 
<% end %>
</tbody>

从控制器更新操作:

def update
@userdailydata = Userdailydata.where(:user_id=>current_user.id)
respond_with @userdailydata
@userdailydata.each do |data|
   data.update_attributes(allowed_params)
end
end

这段代码循环遍历暴露给用户的每一行并更新所有行(而不是有问题的一行),所以我知道为什么我当前的代码不正确,但我不确定如何解决这个问题。

谁能帮我弄清楚如何只更新用户正在积极编辑的记录?

【问题讨论】:

  • 这是来自控制器update 操作的代码吗?我建议发布整个操作代码。和视图中的代码
  • 感谢您的反馈,我编辑了问题并提供了更多代码。
  • 我认为您的 update 操作代码不正确。你想更新用户的属性(换句话说,用户的数据)。对?你上面贴的视图,是来自users.index.html.erb吗?
  • 是的,视图来自使用 userdailydata.index.html.erb。我想从用户执行内联编辑的任何日期更新用户的数据。但是会向用户公开包含许多日期的整个数据表。

标签: ruby-on-rails ruby ruby-on-rails-4 best-in-place


【解决方案1】:

您的update 操作应如下所示:

def update
  @userdailydata = Userdailydata.find(params[:id]) #first, find the specific user by id
  #maybe, @userdailydata = Userdailydata.where(:user_id=>current_user.id) can replace the line above. they seem to be doing the same thing. 
  #the goal is fetch a specific user to edit it's attributes. 
  respond_to do |format|
    if @userdailydata.update(allowed_params) # i'm assuming you got allowed_params set up properly
       format.html { redirect_to @userdailydata }
       format.json { respond_with_bip(@userdailydata) }
   else
       format.html { render :action => "edit" }
       format.json { respond_with_bip(@userdailydata) }
   end
 end
end

只会更新您传入的参数。不需要所有参数。

如果您正在编辑,比如users show page,您将上面的代码更改为:

<tbody>


<tr>
<td> <%= best_in_place data @userdailydata, :date,:display_with => lambda { |v| v.strftime("%Y-%m-%d") }  %> </td>
<td> <%= best_in_place data @userdailydata, :calories_consumed %> </td>
<td> <%= best_in_place data @userdailydata, :calories_exercised %> </td>
<td> <%= best_in_place data @userdailydata, :weight %> </td>
</tr> 
</tbody>

请参阅此处,您只会对要编辑的特定 attributes 进行内联编辑。

【讨论】:

  • if @userdailydata.update(allowed_params) 尝试调试时收到错误“参数数量错误(1 对 2)”。我的 allowed_pa​​rams 代码是 def allowed_params params.require(:userdailydata).permit(:weight,:calories_consumed, :calories_exercised, :date, :user_id) end
  • 您的allowed_params 看起来不错。前面的线是什么?是@userdailydata = Userdailydata.find(params[:id])吗?
  • 另一个问题,关于这一行:&lt;% @userdailydata.each do |data| %&gt;:您试图在index view 中循环的@userdailydata 是如何定义的?发布那个。如果@userdailydata 拥有one user 的属性,我认为您无法遍历。除非是array of multiple users' daily data
  • 我尝试了@userdailydata = Userdailydata.find(params[:id]) 以及我原来的@userdailydata = Userdailydata.where(:user_id=&gt;current_user.id)。 @user 每日数据在索引操作中定义为@userdailydata = Userdailydata.where(:user_id=&gt;current_user.id),因此我得到了一个唯一用户的每日数据表。我能够遍历数据以在视图中公开它。那部分显示正常。唯一的问题是只更新数据库中的一条记录(而不是针对该特定用户的所有记录)
  • 在您的更新操作中,如果您使用我的示例中的代码(第一行是:@userdailydata = Userdailydata.find(params[:id])),编辑操作是否在不使用best_in_place 的情况下工作?你能发布UserDailyDataUser 模型吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 2015-09-08
  • 1970-01-01
相关资源
最近更新 更多