Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 回调与表单提交混为一谈_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 回调与表单提交混为一谈

Ruby on rails 回调与表单提交混为一谈,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,好的,下面是发生的情况 用户模型有一些方法,它们的功能取决于其他对象(称为状态更新)的存在,这些对象在一个has\u many behing\u to关系中与其关联 所以现在我意识到status_updates新页面上有一个表单,控制器提供了status_update.build对象,该对象已初始化,但未保存该表单。尽管如此,当从视图中调用用户方法时,它返回未保存的初始化值,而不是点击数据库返回类 我通过对需要它的用户方法设置以下条件来修复该错误 if self.status_updates.c

好的,下面是发生的情况

用户模型有一些方法,它们的功能取决于其他对象(称为状态更新)的存在,这些对象在一个has\u many behing\u to关系中与其关联

所以现在我意识到status_updates新页面上有一个表单,控制器提供了status_update.build对象,该对象已初始化,但未保存该表单。尽管如此,当从视图中调用用户方法时,它返回未保存的初始化值,而不是点击数据库返回类

我通过对需要它的用户方法设置以下条件来修复该错误

if self.status_updates.count == 0
  temp_status_update
else
  self.status_updates.last
end 
如果用户对象没有任何状态更新,则返回一个包含默认值的初始化状态更新-不会保存此状态更新。在实际创建并保存状态更新之前,它只是一个占位符

这就是奇怪的地方

现在,当我尝试实际保存状态更新时,无论我通过表单提交什么,它都只保存初始化的默认值

我知道这些值正在被提交,只是看起来这些值并没有提交给控制器

以下是我的状态更新控制器中的操作

   def create
      @status_update = current_user.status_updates.build(params[:status_update])
      if @status_update.save
        flash[:success] = "Status Update Saved! #{params[:status_update]}"
      # redirect_to status_update_path(current_user.id)
        redirect_to new_status_update_path
      else
        flash[:error] = "Input Error - Couldn't Save Status Update"
        redirect_to new_status_update_path
      end 
    end  



   def new
     @status_update = current_user.status_updates.build if user_signed_in?
   end 
参数不应该具有提交的值而不是默认的初始化值吗

我在success flash中返回参数,它将正确的值返回给视图

以下是状态更新模型:

    class StatusUpdate < ActiveRecord::Base
      belongs_to :user

      after_initialize :default_values 
      before_create :sanitize 

      #removed stuff from here for brevity

      def default_values
        if self.created_at == nil
          self.current_bf_pct        = 0
          self.current_weight        = 0
          self.current_lbm           = 0    
          self.current_fat_weight    = 0
          self.change_in_weight      = 0 
          self.change_in_bf_pct      = 0
          self.change_in_lbm         = 0
          self.change_in_fat_weight  = 0
          self.total_weight_change   = 0
          self.total_bf_pct_change   = 0
          self.total_lbm_change      = 0
          self.total_fat_change      = 0 
        end
      end

      #removed rest of model from here for brevity
class StatusUpdate
这是用户模型

    class User < ActiveRecord::Base


      before_create :sanitize

      has_many :status_updates, dependent: :destroy

     #removed stuff from here for brevity

    def sanitize
      #inputs
      self.activity_factor       = 1.3
      self.deficit_amnt          = 1
      self.target_bf_pct         = 10 
      self.fat_factor            = 0.45
      self.protein_factor        = 1
    end

    #Removed functions for brevity


      def temp_status_update
        self.status_updates.build
      end
    end
class用户
我用来提交状态更新的表单

        <%= form_for(@status_update) do |f| %>

          <%= f.label :current_weight %>
          <%= f.text_field :current_weight %>

          <%= f.label :current_bf_pct %>
          <%= f.text_field :current_bf_pct %>

          <%= f.submit "Update", class:"btn btn-large btn-primary" %> 
        <% end %>

以下是从new status_update视图对用户模型的方法调用

        <tr>
          <td> <%= current_user.current_bf_pct %> % </td>
          <td> <%= current_user.target_bf_pct  %> % </td>
        </tr> 

% 
% 
不过,提交到创建操作的status\u update build存储在@status\u update变量中


因此,为什么还要考虑其他状态更新构建?它没有插入到传递给创建操作的变量中。

切勿使用默认模型值创建临时对象


与避免使用数据库不同,只要在模型中添加一个名为“temporary”的属性,并在创建临时对象时将其设置为“true”。然后使用find_by_temporary来查找它,并根据需要进行销毁

切勿使用默认模型值创建临时对象


与避免使用数据库不同,只要在模型中添加一个名为“temporary”的属性,并在创建临时对象时将其设置为“true”。然后使用find_by_temporary来查找它,并根据需要进行销毁

无论何时,只要您想在任何操作后设置属性,您都应该这样设置属性,并在创建、更新等之后调用该方法

对模型的回调处理数据库中填充的所有数据

before_create :sanitize    

def sanitize
self.users.build(user_id, User.current.id, activity_factor:1.3,
  deficit_amnt: 1
  target_bf_pct: 10 
  fat_factor: 0.45
  protein_factor: 1)
end

对要更新或设置值的任何模型执行相同操作。。。它将从控制器中调用该方法,并从模型中检查它们,使其正常工作。

无论何时在任何操作后要设置属性,都应该这样设置属性,并在创建、更新等之后调用该方法

对模型的回调处理数据库中填充的所有数据

before_create :sanitize    

def sanitize
self.users.build(user_id, User.current.id, activity_factor:1.3,
  deficit_amnt: 1
  target_bf_pct: 10 
  fat_factor: 0.45
  protein_factor: 1)
end

对要更新或设置值的任何模型执行相同操作。。。它将从控制器调用该方法,并从模型中检查它们,使其工作。

我的问题是否得到了充分的解释,解释得太长,还是无法理解?哈哈,我怎样才能确保其他人愿意回答我的问题?我尽量说清楚。我的问题是否得到了充分的解释,解释得太长,还是无法理解?哈哈,我怎样才能确保其他人愿意回答我的问题?我尽量说清楚。