私は初心者であり、jQuery コードを使用した Ruby の経験は限られています。
jQueryにRubyコードを追加するには?
以下の太字のように from _multicourse_meal.html.erb を使用して jQuery にパーツを挿入しようとしましたが、meal_(meal) から削除メソッドを呼び出せないことを除いて機能しません。jQueryでそれを行う方法は?
$('#new-<%= @multicourse_meal.id %>').before("
<tr id=<%= @meal.id %>>
<td>**<%= link_to '<i class="icon-remove"></i>'.html_safe, meal_path(meal), method: :delete, remote: true %>**(<--ERROR code)<%= meal.title %>
</i> <%= @meal.title %></td>
<td><%= @meal.portion_amt_g %></td>
<td><%= @meal.calories %></td>
<td><%= @meal.carbs_amt_g.round(1) %></td>
<td><%= @meal.protein_amt_g.round(1) %></td>
<td><%= @meal.fat_amt_g.round(1) %></td></tr>")
.
application.html.erb in views/layouts
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag :defaults, "application" %>
<%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
.
_multicourse_meals.html.erb in views/multicourse_meals
<% @multicourse_meals_for_the_day.each do |multicourse_meal| %>
<table class="table table-condensed table-striped table-bordered">
<thead>
<tr>
<th>Meal: <%= multicourse_meal.meal_no %></th>
<th>Portion Size (ml/g.)</th>
<th>Calories</th>
<th>Carbs: (g.)</th>
<th>Pro: (g.)</th>
<th>Fat: (g.)</th>
</tr>
</thead>
<tbody>
<% multicourse_meal.meals.each do |meal| %>
<tr id="<%= meal.id %>">
<td>
<%= link_to '<i class="icon-remove"></i>'.html_safe, meal_path(meal), method: :delete, remote: true %>
<%= meal.title %>
</td>
<td><%= meal.portion_amt_g %></td>
<td><%= meal.calories %></td>
<td><%= meal.carbs_amt_g.round(1) %></td>
<td><%= meal.protein_amt_g.round(1) %></td>
<td><%= meal.fat_amt_g.round(1) %></td>
</tr>
<% end %>
<tr id="new-<%= multicourse_meal.id %>">
<td>
<%= form_for( @meal, remote: true) do |f| %>
<%= f.select :nutrient_id, options_for_select(Nutrient.all.collect { |p| [p.title, p.id]}) %>
<%= f.text_field :portion_amt_g, class: "input-mini" %>
<%= f.hidden_field :multicourse_meal_id, :value => multicourse_meal.id %>
<%= f.submit "Add" %>
<% end %>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<th></th>
</tr>
<tr>
<td></td>
<th>Target Amount</th>
<td><%= multicourse_meal.tgt_calories %></td>
<td><%= multicourse_meal.tgt_carbs_amt_g.round(1) %></td>
<td><%= multicourse_meal.tgt_protein_amt_g.round(1) %></td>
<td><%= multicourse_meal.tgt_fat_amt_g.round(1) %></td>
</tr>
<tr>
<td></td>
<th>Total</th>
<td id="<%= multicourse_meal.id %>-tot_calories"><%= multicourse_meal.total_calories %></td>
<td id="<%= multicourse_meal.id %>-tot_carbs"><%= multicourse_meal.total_carbs_amt_g.round(1) %></td>
<td id="<%= multicourse_meal.id %>-tot_protein"><%= multicourse_meal.total_protein_amt_g.round(1) %></td>
<td id="<%= multicourse_meal.id %>-tot_fat"><%= multicourse_meal.total_fat_amt_g.round(1) %></td>
</tr>
</tbody>
</table>
<% end %>
.
create.js.erb in views/meals
$('#new-<%= @multicourse_meal.id %>').before("<tr id=<%= @meal.id %>><td><i class='icon-remove'></i> <%= @meal.title %></td><td><%= @meal.portion_amt_g %></td><td><%= @meal.calories %></td><td><%= @meal.carbs_amt_g.round(1) %></td><td><%= @meal.protein_amt_g.round(1) %></td><td><%= @meal.fat_amt_g.round(1) %></td></tr>")
// update multicourse meal view
$('#<%= @multicourse_meal.id %>-tot_calories').html("<%= @multicourse_meal.total_calories %>")
$('#<%= @multicourse_meal.id %>-tot_carbs').html("<%= @multicourse_meal.total_carbs_amt_g.round(1) %>")
$('#<%= @multicourse_meal.id %>-tot_protein').html("<%= @multicourse_meal.total_protein_amt_g.round(1) %>")
$('#<%= @multicourse_meal.id %>-tot_fat').html("<%= @multicourse_meal.total_fat_amt_g.round(1) %>")
// update calories Journal view
$('#meals_mn_ratio').html("<%= @calories_journal.meals_mn_ratio %>");
$('#cj_total_calories').html("<%= @calories_journal.total_calories %>");
$('#cj_total_carbs').html("<%= @calories_journal.total_carbs_amt_g.round(1) %>");
$('#cj_total_pro').html("<%= @calories_journal.total_protein_amt_g.round(1) %>");
$('#cj_total_fat').html("<%= @calories_journal.total_fat_amt_g.round(1) %>");