admin管理员组文章数量:1331927
I am following section 4 (Server Side Concerns) to set up ajax on a page. I've copied the tutorial text pletely (replacing the model names with my own) and it creates and saves my "Participants" record, but does not automatically refresh the ajax partial.
This is the error I get...which looks like it's referrring to my create.js.erb
ActionView::Template::Error ('nil' is not an ActiveModel-patible object. It must implement :to_partial_path.):
1: $("<%= escape_javascript(render @participant) %>").appendTo("#participants");
2: // $('#participants').html("<%= j (render @participants) %>");
app/views/participants/create.js.erb:2:in `_app_views_participants_create_js_erb___1675277149181037111_70181034249880'
Here's my code
class ParticipantsController < ApplicationController
def new
@participant = Participant.new
@participants = @participants.recently_updated
end
def create
@participant = Participant.new(participant_params)
respond_to do |format|
if @participant.save
format.html { redirect_to @participant, notice: 'Helper Invited!' }
format.js {}
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
_form.html.erb
<ul id="participants">
<%= render @participants %>
</ul>
<%= form_for(@participant, remote: true) do |f| %>
<%= f.label :email %><br>
<%= f.email_field :email %>
<%= f.submit 'SUBMIT' %>
<script>
$(document).ready(function() {
return $("#new_participant").on("ajax:success", function(e, data, status, xhr) {
return $("#new_participant").append(xhr.responseText);
}).on("ajax:error", function(e, xhr, status, error) {
return $("#new_participant").append("<p>Oops. Please Try again.</p>");
});
});
</script>
<script>
$(function() {
return $("a[data-remote]").on("ajax:success", function(e, data, status, xhr) {
return alert("The helper has been removed and notified.");
});
});
</script>
_participant.html.erb
<li >
<%= participant.email %> <%= link_to participant, remote: true, method: :delete, data: { confirm: 'Are you sure?' } do %>REMOVE<% end %>
</li>
create.js.erb
$("<%= escape_javascript(render @participant) %>").appendTo("#participants");
destroy.js.erb
$('#participants').html("<%= j (render @participants) %>");
I am following section 4 (Server Side Concerns) to set up ajax on a page. I've copied the tutorial text pletely (replacing the model names with my own) and it creates and saves my "Participants" record, but does not automatically refresh the ajax partial.
This is the error I get...which looks like it's referrring to my create.js.erb
ActionView::Template::Error ('nil' is not an ActiveModel-patible object. It must implement :to_partial_path.):
1: $("<%= escape_javascript(render @participant) %>").appendTo("#participants");
2: // $('#participants').html("<%= j (render @participants) %>");
app/views/participants/create.js.erb:2:in `_app_views_participants_create_js_erb___1675277149181037111_70181034249880'
Here's my code
class ParticipantsController < ApplicationController
def new
@participant = Participant.new
@participants = @participants.recently_updated
end
def create
@participant = Participant.new(participant_params)
respond_to do |format|
if @participant.save
format.html { redirect_to @participant, notice: 'Helper Invited!' }
format.js {}
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
_form.html.erb
<ul id="participants">
<%= render @participants %>
</ul>
<%= form_for(@participant, remote: true) do |f| %>
<%= f.label :email %><br>
<%= f.email_field :email %>
<%= f.submit 'SUBMIT' %>
<script>
$(document).ready(function() {
return $("#new_participant").on("ajax:success", function(e, data, status, xhr) {
return $("#new_participant").append(xhr.responseText);
}).on("ajax:error", function(e, xhr, status, error) {
return $("#new_participant").append("<p>Oops. Please Try again.</p>");
});
});
</script>
<script>
$(function() {
return $("a[data-remote]").on("ajax:success", function(e, data, status, xhr) {
return alert("The helper has been removed and notified.");
});
});
</script>
_participant.html.erb
<li >
<%= participant.email %> <%= link_to participant, remote: true, method: :delete, data: { confirm: 'Are you sure?' } do %>REMOVE<% end %>
</li>
create.js.erb
$("<%= escape_javascript(render @participant) %>").appendTo("#participants");
destroy.js.erb
$('#participants').html("<%= j (render @participants) %>");
Share
Improve this question
asked Mar 12, 2015 at 14:58
NothingToSeeHereNothingToSeeHere
2,3735 gold badges30 silver badges61 bronze badges
1
-
CHeck if you are getting
@participant
increate.js.erb
file – Sonalkumar sute Commented Mar 12, 2015 at 15:04
2 Answers
Reset to default 5It's on line 2 of your create.js.erb
file, it's the missing @participants
not the @participant
.
You've mented the line out in JS, but the ERB is still going to be processed by Rails, so it's still trying to do the render @participants
Update
For future... it's the last line of that error that's the key:
app/views/participants/create.js.erb:2
See the 2
at the end, that's telling you which line the error happened on, and so that's where you need to focus when looking for the problem.
I was able to fix this error on my end. I had missed creating the action in the controller.
So in your example, I would have missed the create action in the controller. Thus, when testing anything using that action, it would fail.
Any future users experiencing this issue, make sure the action is defined and no errors/typos in the controller level.
本文标签:
版权声明:本文标题:javascript - Rails 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path. ajax - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742252719a2441061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论