使用 AJAX 刷新 Rails 部分时的未定义方法

Posted

技术标签:

【中文标题】使用 AJAX 刷新 Rails 部分时的未定义方法【英文标题】:Undefined Method when Refreshing Rails partial with AJAX 【发布时间】:2016-05-06 16:11:09 【问题描述】:

我正在为如何使用 Rails 和 AJAX 重新加载部分而苦苦挣扎,并且遇到了未定义的方法错误。我认为这可能是因为我没有正确地将局部变量传递回部分,但我不确定这应该如何工作。

我有一个“项目”Show.html.erb 页面。在该显示页面上,与该项目相关的“投资”有一个特定的 DIV。

Show.html.erb

  <div class="col-md-3" id="investments-container">
    <%= render 'proj_investments' %>
  </div>

然后,它会在 _proj_investments.html.erb 部分中呈现一个循环遍历与项目相关的所有投资(它按奖励类型分组)的部分。每个都有一个表单提交,允许以某种方式点击 INVEST 按钮并使用 AJAX 创建新的投资。 ( :js do |f| %> )

_proj_investments.html.erb

<% if @project.rewards.any? %>
          <% @project.rewards.each do |reward| %>
            <div class="panel panel-default project-rightpanel">
              <div class="project-rightpanel-header">
                <span class="project-rightpanel-header amount"><%=number_to_currency(reward.amount,precision: 0)%></span>
                <span class="project-rightpanel-header amount"><%=reward.rewardtype.name %></span>
              </div>
              <div class="project-rightpanel-header">
                  <%=reward.investments.count(:amount)%> investments for <%=number_to_currency(reward.investments.sum(:amount),precision: 0)%>
              </div>
              </div>
              <%= form_for [@investment, Investment.new], remote: true, "data-type" => :js do |f| %>
              <div class="field">
                <%= f.hidden_field :amount, :value => reward.amount %>
                <%= f.hidden_field :User_id, :value => current_user.id %>
                <%= f.hidden_field :Reward_id, :value => reward.id %>
              </div>
              <div class="actions">
                <%= f.submit "INVEST", :class => "btn btn-success" %>
              </div>
            <% end %>

            </div>
          <% end %>
      <% end %>

Investments 控制器然后调用 Create 方法并响应 JS 并调用我的 create.js.erb

investments_controller.rb

  def create
@investment = Investment.new(investment_params)

respond_to do |format|
  if @investment.save
    format.js 
    format.html  redirect_to @investment, notice: 'Investment was successfully created.' 
    format.json  render :show, status: :created, location: @investment 
  else
    format.html  render :new 
    format.json  render json: @investment.errors, status: :unprocessable_entity 
  end
end

结束

在 create.js.erb 中,我只想刷新原始 proj_investments 部分中的所有内容。但是,当我加载它时,当我使用此代码时,它会给我一个“未定义的方法‘投资’”错误:

创建.js.erb

$("#investments-container").html("<%= escape_javascript(render :partial => 'projects/proj_investments', :formats => :html)%>"); 

认为这里发生的事情是 Rails 部分已经渲染,所以我尝试使用不再通过 AJAX 访问的项目、奖励和投资等对象。也许我需要在渲染部分时以某种方式传递这些对象。但我不知道该怎么做。或者,如果我在概念上错了,那也很高兴!

谢谢, 迈克

添加模型:

class Project < ActiveRecord::Base
    belongs_to :user
    belongs_to :genre 
    has_many :rewards, dependent: :destroy
    has_many :investments, through: :rewards
    has_many :collaborators
    has_attached_file :image, :styles =>  :large => "600x600>", :medium => "300x300>", :thumb => "100x100>" 
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]
    validates :image, presence: true
    accepts_nested_attributes_for :rewards,
        reject_if: proc  |attributes| attributes['name'].blank? ,
        allow_destroy: true
    accepts_nested_attributes_for :collaborators,
        reject_if: proc  |attributes| attributes['last'].blank? ,
        allow_destroy: true
end

class Investment < ActiveRecord::Base
  belongs_to :user
  belongs_to :reward
  belongs_to :project
end

class Reward < ActiveRecord::Base
  belongs_to :project
  belongs_to :rewardtype
  has_many :investments, dependent: :destroy
  validates :rewardtype, presence: true
  validates :name,
        presence: true
end

下面的完整堆栈跟踪:

D, [2016-01-28T21:44:45.289613 #50185] DEBUG -- : 
D, [2016-01-28T21:44:45.289663 #50185] DEBUG -- : 
D, [2016-01-28T21:44:45.289679 #50185] DEBUG -- : 
D, [2016-01-28T21:44:45.289689 #50185] DEBUG -- : 
I, [2016-01-28T21:44:45.289850 #50185]  INFO -- : Started GET "/projects/28" for ::1 at 2016-01-28 21:44:45 -0800
I, [2016-01-28T21:44:45.289887 #50185]  INFO -- : Started GET "/projects/28" for ::1 at 2016-01-28 21:44:45 -0800
D, [2016-01-28T21:44:45.298245 #50185] DEBUG -- :   ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-01-28T21:44:45.298297 #50185] DEBUG -- :   ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
I, [2016-01-28T21:44:45.326787 #50185]  INFO -- : Processing by ProjectsController#show as HTML
I, [2016-01-28T21:44:45.326853 #50185]  INFO -- : Processing by ProjectsController#show as HTML
I, [2016-01-28T21:44:45.326896 #50185]  INFO -- :   Parameters: "id"=>"28"
I, [2016-01-28T21:44:45.326923 #50185]  INFO -- :   Parameters: "id"=>"28"
D, [2016-01-28T21:44:45.349554 #50185] DEBUG -- :   Project Load (0.2ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1  [["id", 28]]
D, [2016-01-28T21:44:45.349601 #50185] DEBUG -- :   Project Load (0.2ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1  [["id", 28]]
D, [2016-01-28T21:44:45.375951 #50185] DEBUG -- :   User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.376001 #50185] DEBUG -- :   User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.418778 #50185] DEBUG -- :    (0.2ms)  SELECT SUM("investments"."amount") FROM "investments" INNER JOIN "rewards" ON "investments"."reward_id" = "rewards"."id" WHERE "rewards"."project_id" = ?  [["project_id", 28]]
D, [2016-01-28T21:44:45.418823 #50185] DEBUG -- :    (0.2ms)  SELECT SUM("investments"."amount") FROM "investments" INNER JOIN "rewards" ON "investments"."reward_id" = "rewards"."id" WHERE "rewards"."project_id" = ?  [["project_id", 28]]
D, [2016-01-28T21:44:45.438045 #50185] DEBUG -- :   Reward Exists (0.1ms)  SELECT  1 AS one FROM "rewards" WHERE "rewards"."project_id" = ? LIMIT 1  [["project_id", 28]]
D, [2016-01-28T21:44:45.438090 #50185] DEBUG -- :   Reward Exists (0.1ms)  SELECT  1 AS one FROM "rewards" WHERE "rewards"."project_id" = ? LIMIT 1  [["project_id", 28]]
D, [2016-01-28T21:44:45.438824 #50185] DEBUG -- :   Reward Load (0.1ms)  SELECT "rewards".* FROM "rewards" WHERE "rewards"."project_id" = ?  [["project_id", 28]]
D, [2016-01-28T21:44:45.438858 #50185] DEBUG -- :   Reward Load (0.1ms)  SELECT "rewards".* FROM "rewards" WHERE "rewards"."project_id" = ?  [["project_id", 28]]
D, [2016-01-28T21:44:45.448028 #50185] DEBUG -- :   Rewardtype Load (0.1ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.448074 #50185] DEBUG -- :   Rewardtype Load (0.1ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.453290 #50185] DEBUG -- :    (0.2ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 50]]
D, [2016-01-28T21:44:45.453370 #50185] DEBUG -- :    (0.2ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 50]]
D, [2016-01-28T21:44:45.455273 #50185] DEBUG -- :    (0.3ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 50]]
D, [2016-01-28T21:44:45.455328 #50185] DEBUG -- :    (0.3ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 50]]
D, [2016-01-28T21:44:45.476845 #50185] DEBUG -- :   User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.476897 #50185] DEBUG -- :   User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.478277 #50185] DEBUG -- :   Rewardtype Load (0.1ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 3]]
D, [2016-01-28T21:44:45.478313 #50185] DEBUG -- :   Rewardtype Load (0.1ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 3]]
D, [2016-01-28T21:44:45.479190 #50185] DEBUG -- :    (0.1ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 55]]
D, [2016-01-28T21:44:45.479218 #50185] DEBUG -- :    (0.1ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 55]]
D, [2016-01-28T21:44:45.479621 #50185] DEBUG -- :    (0.1ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 55]]
D, [2016-01-28T21:44:45.479645 #50185] DEBUG -- :    (0.1ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 55]]
D, [2016-01-28T21:44:45.481244 #50185] DEBUG -- :   CACHE (0.0ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.481287 #50185] DEBUG -- :   CACHE (0.0ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 2]]
D, [2016-01-28T21:44:45.482172 #50185] DEBUG -- :    (0.1ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 56]]
D, [2016-01-28T21:44:45.482204 #50185] DEBUG -- :    (0.1ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 56]]
D, [2016-01-28T21:44:45.482744 #50185] DEBUG -- :    (0.1ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 56]]
D, [2016-01-28T21:44:45.482779 #50185] DEBUG -- :    (0.1ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 56]]
D, [2016-01-28T21:44:45.484161 #50185] DEBUG -- :   CACHE (0.0ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 3]]
D, [2016-01-28T21:44:45.484191 #50185] DEBUG -- :   CACHE (0.0ms)  SELECT  "rewardtypes".* FROM "rewardtypes" WHERE "rewardtypes"."id" = ? LIMIT 1  [["id", 3]]
D, [2016-01-28T21:44:45.484953 #50185] DEBUG -- :    (0.1ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 57]]
D, [2016-01-28T21:44:45.484984 #50185] DEBUG -- :    (0.1ms)  SELECT COUNT("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 57]]
D, [2016-01-28T21:44:45.485425 #50185] DEBUG -- :    (0.1ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 57]]
D, [2016-01-28T21:44:45.485451 #50185] DEBUG -- :    (0.1ms)  SELECT SUM("investments"."amount") FROM "investments" WHERE "investments"."reward_id" = ?  [[nil, 57]]
I, [2016-01-28T21:44:45.486265 #50185]  INFO -- :   Rendered projects/_proj_investments.html.erb (93.5ms)
I, [2016-01-28T21:44:45.486298 #50185]  INFO -- :   Rendered projects/_proj_investments.html.erb (93.5ms)
I, [2016-01-28T21:44:45.486347 #50185]  INFO -- :   Rendered projects/show.html.erb within layouts/application (123.7ms)
I, [2016-01-28T21:44:45.486419 #50185]  INFO -- :   Rendered projects/show.html.erb within layouts/application (123.7ms)
I, [2016-01-28T21:44:46.135983 #50185]  INFO -- :   Rendered layouts/_header.html.erb (1.0ms)
I, [2016-01-28T21:44:46.136035 #50185]  INFO -- :   Rendered layouts/_header.html.erb (1.0ms)
I, [2016-01-28T21:44:46.136408 #50185]  INFO -- : Completed 200 OK in 809ms (Views: 778.2ms | ActiveRecord: 3.3ms)
I, [2016-01-28T21:44:46.136454 #50185]  INFO -- : Completed 200 OK in 809ms (Views: 778.2ms | ActiveRecord: 3.3ms)
D, [2016-01-28T21:44:58.979348 #50185] DEBUG -- : 
D, [2016-01-28T21:44:58.979399 #50185] DEBUG -- : 
D, [2016-01-28T21:44:58.979420 #50185] DEBUG -- : 
D, [2016-01-28T21:44:58.979434 #50185] DEBUG -- : 
I, [2016-01-28T21:44:58.979548 #50185]  INFO -- : Started POST "/investments" for ::1 at 2016-01-28 21:44:58 -0800
I, [2016-01-28T21:44:58.979579 #50185]  INFO -- : Started POST "/investments" for ::1 at 2016-01-28 21:44:58 -0800
I, [2016-01-28T21:44:58.986293 #50185]  INFO -- : Processing by InvestmentsController#create as JS
I, [2016-01-28T21:44:58.986368 #50185]  INFO -- : Processing by InvestmentsController#create as JS
I, [2016-01-28T21:44:58.986441 #50185]  INFO -- :   Parameters: "utf8"=>"✓", "investment"=>"amount"=>"50", "User_id"=>"2", "Reward_id"=>"50", "commit"=>"INVEST"
I, [2016-01-28T21:44:58.986457 #50185]  INFO -- :   Parameters: "utf8"=>"✓", "investment"=>"amount"=>"50", "User_id"=>"2", "Reward_id"=>"50", "commit"=>"INVEST"
D, [2016-01-28T21:44:58.987691 #50185] DEBUG -- :    (0.1ms)  begin transaction
D, [2016-01-28T21:44:58.987725 #50185] DEBUG -- :    (0.1ms)  begin transaction
D, [2016-01-28T21:44:58.991624 #50185] DEBUG -- :   SQL (0.4ms)  INSERT INTO "investments" ("User_id", "Reward_id", "amount", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["User_id", 2], ["Reward_id", 50], ["amount", 50.0], ["created_at", "2016-01-29 05:44:58.990349"], ["updated_at", "2016-01-29 05:44:58.990349"]]
D, [2016-01-28T21:44:58.991667 #50185] DEBUG -- :   SQL (0.4ms)  INSERT INTO "investments" ("User_id", "Reward_id", "amount", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["User_id", 2], ["Reward_id", 50], ["amount", 50.0], ["created_at", "2016-01-29 05:44:58.990349"], ["updated_at", "2016-01-29 05:44:58.990349"]]
D, [2016-01-28T21:44:58.992612 #50185] DEBUG -- :    (0.6ms)  commit transaction
D, [2016-01-28T21:44:58.992677 #50185] DEBUG -- :    (0.6ms)  commit transaction
I, [2016-01-28T21:44:59.002803 #50185]  INFO -- :   Rendered projects/_proj_investments.html.erb (3.3ms)
I, [2016-01-28T21:44:59.002882 #50185]  INFO -- :   Rendered projects/_proj_investments.html.erb (3.3ms)
I, [2016-01-28T21:44:59.002941 #50185]  INFO -- :   Rendered investments/create.js.erb (4.6ms)
I, [2016-01-28T21:44:59.002974 #50185]  INFO -- :   Rendered investments/create.js.erb (4.6ms)
I, [2016-01-28T21:44:59.003170 #50185]  INFO -- : Completed 500 Internal Server Error in 17ms (ActiveRecord: 1.1ms)
I, [2016-01-28T21:44:59.003204 #50185]  INFO -- : Completed 500 Internal Server Error in 17ms (ActiveRecord: 1.1ms)
F, [2016-01-28T21:44:59.004536 #50185] FATAL -- :
ActionView::Template::Error (undefined method `investments' for nil:NilClass):
1: <div class="panel panel-default project-rightpanel">
2:       <div class="project-rightpanel-header">
3:         <span class="project-rightpanel-header amount"><%=number_to_currency(@project.investments.sum(:amount),precision: 0)%></span> of <span class="project-rightpanel-header amount">$20,000</span> raised in 15 days
4:       </div>
5:       <div id="projectprogress"></div>
6:       <div class="project-rightpanel-header">
app/views/projects/_proj_investments.html.erb:3:in         `_app_views_projects__proj_investments_html_erb__2910721485294471226_70328031908000'
app/views/investments/create.js.erb:1:in `_app_views_investments_create_js_erb__4066473719869479756_70328057899160'


F, [2016-01-28T21:44:59.004591 #50185] FATAL -- : 

ActionView::Template::Error(未定义方法investments' for nil:NilClass): 1: <div class="panel panel-default project-rightpanel"> 2: <div class="project-rightpanel-header"> 3: <span class="project-rightpanel-header amount"><%=number_to_currency(@project.investments.sum(:amount),precision: 0)%></span> of <span class="project-rightpanel-header amount">$20,000</span> raised in 15 days 4: </div> 5: <div id="projectprogress"></div> 6: <div class="project-rightpanel-header"> app/views/projects/_proj_investments.html.erb:3:in_app_views_projects__proj_investments_html_erb__29107214852944712​​26_70328031908000' app/views/investments/create.js.erb:1:in `_app_views_investments_create_js_erb__4066473719869479756_70328057899160'

I, [2016-01-28T21:44:59.114304 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.5ms)
I, [2016-01-28T21:44:59.114355 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.5ms)
I, [2016-01-28T21:44:59.126017 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.6ms)
I, [2016-01-28T21:44:59.126055 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.6ms)
I, [2016-01-28T21:44:59.126101 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (24.6ms)
I, [2016-01-28T21:44:59.126152 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (24.6ms)
I, [2016-01-28T21:44:59.144002 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.3ms)
I, [2016-01-28T21:44:59.144052 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.3ms)
I, [2016-01-28T21:44:59.166877 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
I, [2016-01-28T21:44:59.166935 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
I, [2016-01-28T21:44:59.177608 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
I, [2016-01-28T21:44:59.177656 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
I, [2016-01-28T21:44:59.187782 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
I, [2016-01-28T21:44:59.187836 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
I, [2016-01-28T21:44:59.188232 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (33.9ms)
I, [2016-01-28T21:44:59.188272 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (33.9ms)
I, [2016-01-28T21:44:59.198215 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
I, [2016-01-28T21:44:59.198255 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
I, [2016-01-28T21:44:59.209355 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
I, [2016-01-28T21:44:59.209420 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
I, [2016-01-28T21:44:59.210343 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (74.6ms)
I, [2016-01-28T21:44:59.210453 #50185]  INFO -- :   Rendered /Users/mikelincoln/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (74.6ms)

【问题讨论】:

你的代码有问题。但在此之前,你能告诉我们你在 Rails 日志文件中得到的错误堆栈跟踪吗? Arup - 我放入了完整的堆栈跟踪(可能比您所寻找的更详细)。我希望它有所帮助,并感谢任何想法或想法。 感谢 Arup .. 我已经编辑了我的原始帖子以包含模型 【参考方案1】:

您的错误是 &lt;%=number_to_currency(@project.investments.sum(:amount),precision: 0) 。当您从 create.js.erb 渲染部分时,没有为 ivar @project 分配值,这就是它是 nil 的原因。你需要给它赋值。

错误回溯的修复看起来像:

def create
  @investment = Investment.new(investment_params)

  respond_to do |format|
    if @investment.save
      @project = @investment.reload.project
      format.js 
      format.html  redirect_to @investment, notice: 'Investment was successfully created.' 
      format.json  render :show, status: :created, location: @investment 
    else
      format.html  render :new 
      format.json  render json: @investment.errors, status: :unprocessable_entity 
  end
end

【讨论】:

感谢奥雅纳。我试过了,它说'project'有一个未定义的方法......错误如下......有什么想法吗? F,[2016-01-29T06:31:10.509297 #50185] 致命 -- :NoMethodError(未定义方法 project' for #&lt;Investment:0x007fed1152c7a8&gt;): app/controllers/investments_controller.rb:32:in block in create' app/controllers/investments_controller.rb:29:in `创建' @MikeL 你能分享你的那两个模型吗?您似乎没有正确配置关联。 嗨 Arup .. 我在原帖中分享了模型。谢谢! Arup - 不用担心。感谢大家的帮助!【参考方案2】:

补充Arup的回答,报错如下:

ActionView::Template::Error(nil:NilClass 的未定义方法“投资”):

&lt;%= number_to_currency(@project.investments.sum(:amount),precision: 0) %&gt;

问题是您没有定义@project(如上所述)。


解决方案是确保create 操作填充@project

实现这一目标的最佳方式是使用nested resources 为项目创建投资

#config/routes.rb
resources :projects do
   resources :investments, only: :create #-> url.com/projects/:project_id/investments
end

#app/controllers/projects_controller.rb
def create
  @project    = Project.find params[:project_id]
  @investment = @project.investments.new investment_params

对于investments#new 方法(您尚未共享),您必须使用以下内容:

<%= form_for [@project, @investment] do |f| %>

IE - 路由每个investments#create通过一个对应的project

【讨论】:

有钱 - 你就是男人。我有一段时间难以理解这一点(并且仍然在概念上有点挣扎),但是当我看到它神奇地工作时,它让我很开心。唯一的问题是:似乎我现在已经在我的 projects_controller.rb 文件的 create 方法中覆盖了构建新项目的逻辑。我曾经在“@project = current_user.projects.build(project_params)”中有一行,我怎样才能让这个和我的核心项目创建表单共存? 你是什么意思?很高兴它成功了,但我不明白你说的新问题已经创建? 嗯,有一个单独的表单用于创建一个新项目,但它也调用了 Projects 控制器中的 Create 方法。但是,为了让你们帮助的上述功能发挥作用,我不得不注释掉完全从头开始构建一个新项目的行。那行是:@project = current_user.projects.build(project_params)。现在,我在其中有“@project= Project.find params[:project_id”] 行,当我从头开始创建新项目时,它会引发“找不到带有 'id'= 的项目”错误。我想我可以创建 2 个单独的方法?

以上是关于使用 AJAX 刷新 Rails 部分时的未定义方法的主要内容,如果未能解决你的问题,请参考以下文章

创建类别时Rails 6 Ajax页面不断刷新

Ajax 和 Rails 4:创建实例变量并更新视图而不刷新

使用 Rails.ajax 和 jquery 可排序插件时的 ActionView::MissingTemplate

使用 ajax 刷新页面时,Ruby on Rails 失败

Rails 5 - Ajax 刷新后重新初始化 Javascript?

如何使用 Rails 4 轮询数据库中的一个值,如果值更改,则使用 AJAX 刷新