update_attributes无法正常工作 [英] update_attributes is not working

查看:105
本文介绍了update_attributes无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器

class ActivitiesController < ApplicationController 
  def exercises
    if current_user.userprofile.present? #chef whether there is a userprofile object
     @weeknum = current_user.userprofile.week 
     @dayly_activity = Activity.where(:week => 1, :day => 'Monday').first   
    end #end check userprofile     
  end

 def updatexercises
   respond_to do | format | 
    @dayly_activity = Activity.where(:week => 1, :day => 'Monday').first 
    @dayly_activity.update_attributes(params[:@dayly_activity])  
    @dayly_activity.save 
    format.html { render action: "exercises" }
  end
 end  
end

还有我的模板

<h1>WEEKLY EXERCICES</h1>

Day : <%= @dayly_activity.day %>

<%= form_for(@dayly_activity, :url => { :action => "updatexercises" }) do | f | %>
<table>
<tr>
    <td>Jogging:</td>
    <td>
        <% list = (0..20).to_a %>
        <%= f.select :jog, list %>
        x 0.1 km
    </td>
</tr>
<tr>
    <td>Bicycling:</td>
    <td>
        <% list = (0..10).to_a %>
        <%= f.select :bicycl, list %>
        km
    </td>
</tr>
<tr>
    <td>Push ups:</td>
    <td>
        <% list = (0..20).to_a %>
        <%= f.select :pushups, list %>
        x 10 times
    </td>
</tr>
<tr>
    <td colspan = "2"><%= f.submit %></td>
</tr>
  </table>

<% end %>

当我单击按钮时,每日+活动"对象未保存.我错过了什么吗

When I click the button, the Daily+activity object is not being saved. Am I missing some thing

我尝试用这种方式进行硬编码并将其保存到数据库中.

I've tried to hard code this way and it saving to the database.

@dayly_activity.jog = 17
@dayly_activity.pushups = 13
@dayly_activity.save

很明显,问题必须出在 update_attributes

Obviously, the problem must be with the update_attributes

推荐答案

您需要使用params[:dayly_activity](删除@符号).

You need to use params[:dayly_activity] (drop the @ sign).

此外,我会把这两行:

   @dayly_activity = Activity.where(:week => 1, :day => 'Monday').first 
   @dayly_activity.update_attributes(params[:dayly_activity])

respond_to块之外(将它们放在其顶部).

Outside of your respond_to block (put them on top of it).

您还可以放下@dayly_activity.save,update_attributes会自动执行该操作,并且在工作/失败时将返回true/false.

You can also drop the @dayly_activity.save, update_attributes do it automatically and will returns true/false if it works/fails.

这篇关于update_attributes无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆