I am struggling with how to reload a partial with Rails and AJAX, and am getting an Undefined Method error. I think this may be because I am not properly passing local variables back to the partial, but I'm not sure how this should work.
I have a "Projects" Show.html.erb page. On that show page there is a certain DIV for "Investments" associated with that Project.
Show.html.erb
<div class="col-md-3" id="investments-container">
<%= render 'proj_investments' %>
</div>
It then renders a partial that cycles through all of the Investments associated with the Project (it groups by Reward type), in the _proj_investments.html.erb partial. There is a form submission in each that allows somehow to hit the INVEST button and create a new investment using AJAX. ( <%= form_for [@investment, Investment.new], remote: true, "data-type" => :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 %>
The Investments controller then calls the Create method and responds to JS and calls my 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
end
In the create.js.erb I just want to refresh everything that was in that original proj_investments partial. But, when I load it, it gives me an "Undefined method `investments'" error when I utilize this code:
Create.js.erb
$("#investments-container").html("<%= escape_javascript(render :partial => 'projects/proj_investments', :formats => :html)%>");
I think what's going on here is that the Rails part has already rendered, so I am trying to utilizing objects like Project, Reward and Investment that are no longer accessible via AJAX. And maybe I need to somehow pass those objects on in rendering the partial. But I don't know how to do this exactly. Or, if I am wrong conceptually, that would be good to know too!
Thanks, Mike
Aucun commentaire:
Enregistrer un commentaire