Rails 使用 Ajax 创建记录时不渲染布局
Rails don't render layout when create record with Ajax
我的问题是在使用 ajax 创建新记录后,我收到了 NOMETHODERROR,我必须重新加载页面才能看到新记录。
我在终端中遇到的错误。
Rendered time_entries/_time_entry.html.erb (3.4ms) Rendered
time_entries/create.js.erb (4.9ms) Completed 500 Internal Server Error
in 41ms (ActiveRecord: 4.4ms)
NoMethodError - undefined method `each' for nil:NilClass:
app/views/time_entries/_time_entry.html.erb:1:in '_app_views_time_entries__time_entry_html_erb___4086905375499854267_70174828432680'
我做错了什么?
time_entries_controller.erb
class TimeEntriesController < ApplicationController
...
respond_to :html, :js
def index
@time_entries = current_user.time_entries.all.order("date DESC")
@time_entries_days = @time_entries.group_by{ |t| t.date.beginning_of_day }
respond_with(@time_entry)
end
def create
@time_entry = TimeEntry.new(timeentry_params)
@time_entry.user_id = current_user.id
respond_to do |format|
if @time_entry.save
format.html { redirect_to @time_entry, notice: 'Arbeitszeit wurde eingetragen' }
format.json { render :show, status: :created, location: @time_entry }
format.js
else
format.html { render :new }
format.json { render json: @time_entry.errors, status: :unprocessable_entity }
format.js
end
end
end
...
_time_entry.html.erb
部分
<% @time_entries_days.each do |day, time_entries| %>
<% if day.today? %>
<div class="column-12 list-group">Today, <%= l day, format: :dm %></div>
<% else %>
<div class="column-12 list-group"><%= l day, format: :dm %></div>
<% end %>
<ul class="lists">
<% time_entries.each do |time_entry| %>
<li class="list-item" id="time_entry_<%= time_entry.id %>">
<div class="list-item__content">
<h3 class="list-item__title">
<%= link_to time_entry.category.name, time_entry %>
<span><%= l time_entry.start_time, format: :hm %></span> - <span><%= l time_entry.end_time, format: :hm %></span>
</h3>
<p><%= time_entry.note %></p>
</div>
<span class="list-item__label"><%= time_entry.hours %>h</span>
</li>
<% end %>
</ul>
<% end %>
index.html.erb
<% if can? :create, TimeEntry %>
<%= link_to 'Add new Time Entry', new_time_entry_path, remote: true, class: 'btn btn-primary', :data => { :'popup-open' => 'popup-1' } %>
<% end %>
...
<div class="row" id="container_time_entries">
<%= render "time_entry" %>
</div>
create.js.erb 用于测试
创建新时间条目后我没有收到提醒。
alert("HALLO");
create.js.erb正常
$('.modal-bg').hide();
$('.popup').fadeOut(350);
$('#container_time_entries').html("<%= j(render 'time_entry') %>");
您没有在创建方法中设置变量 @time_entries_days
,但您需要在 _time_entry.html.erb 中使用它。
设置它,你就可以开始了。
我的问题是在使用 ajax 创建新记录后,我收到了 NOMETHODERROR,我必须重新加载页面才能看到新记录。
我在终端中遇到的错误。
Rendered time_entries/_time_entry.html.erb (3.4ms) Rendered time_entries/create.js.erb (4.9ms) Completed 500 Internal Server Error in 41ms (ActiveRecord: 4.4ms)
NoMethodError - undefined method `each' for nil:NilClass:
app/views/time_entries/_time_entry.html.erb:1:in '_app_views_time_entries__time_entry_html_erb___4086905375499854267_70174828432680'
我做错了什么?
time_entries_controller.erb
class TimeEntriesController < ApplicationController
...
respond_to :html, :js
def index
@time_entries = current_user.time_entries.all.order("date DESC")
@time_entries_days = @time_entries.group_by{ |t| t.date.beginning_of_day }
respond_with(@time_entry)
end
def create
@time_entry = TimeEntry.new(timeentry_params)
@time_entry.user_id = current_user.id
respond_to do |format|
if @time_entry.save
format.html { redirect_to @time_entry, notice: 'Arbeitszeit wurde eingetragen' }
format.json { render :show, status: :created, location: @time_entry }
format.js
else
format.html { render :new }
format.json { render json: @time_entry.errors, status: :unprocessable_entity }
format.js
end
end
end
...
_time_entry.html.erb
部分
<% @time_entries_days.each do |day, time_entries| %>
<% if day.today? %>
<div class="column-12 list-group">Today, <%= l day, format: :dm %></div>
<% else %>
<div class="column-12 list-group"><%= l day, format: :dm %></div>
<% end %>
<ul class="lists">
<% time_entries.each do |time_entry| %>
<li class="list-item" id="time_entry_<%= time_entry.id %>">
<div class="list-item__content">
<h3 class="list-item__title">
<%= link_to time_entry.category.name, time_entry %>
<span><%= l time_entry.start_time, format: :hm %></span> - <span><%= l time_entry.end_time, format: :hm %></span>
</h3>
<p><%= time_entry.note %></p>
</div>
<span class="list-item__label"><%= time_entry.hours %>h</span>
</li>
<% end %>
</ul>
<% end %>
index.html.erb
<% if can? :create, TimeEntry %>
<%= link_to 'Add new Time Entry', new_time_entry_path, remote: true, class: 'btn btn-primary', :data => { :'popup-open' => 'popup-1' } %>
<% end %>
...
<div class="row" id="container_time_entries">
<%= render "time_entry" %>
</div>
create.js.erb 用于测试
创建新时间条目后我没有收到提醒。
alert("HALLO");
create.js.erb正常
$('.modal-bg').hide();
$('.popup').fadeOut(350);
$('#container_time_entries').html("<%= j(render 'time_entry') %>");
您没有在创建方法中设置变量 @time_entries_days
,但您需要在 _time_entry.html.erb 中使用它。
设置它,你就可以开始了。