我在一个页面中有一个ajax调用,在控制器中创建一个Goal对象,然后呈现_create.js.erb:
$("form#edit_goal_<%= @last_goal.id%>").after('<%= escape_javascript(render(:partial => "show_new_goal", :locals => {:g => @goal, :user => @goal.user, :can_admin_athlete => true})).html_safe %>');
这是_show_new_goal.html.erb部分:
<%= form_for( g, :url => user_goal_path(user, g), :method => :put, :remote => true) do |f| %>
<div id="row_<%=g.id%>"><%= f.check_box(:is_complete)%> <%= g.name%></div>
<% end %>
此部分呈现的HTML已损坏:
(note the closing div> and form> tags that should be </div> and </form>):
<form accept-charset=UTF-8 action=/users/1/goals/11 class=edit_goal data-remote=true id=edit_goal_11 method=post>
<div style=margin:0;padding:0;display:inline>
<input name=utf8 type=hidden value=✓ />
<input name=_method type=hidden value=put />
<input name=authenticity_token type=hidden value=blahblah />
div>
<div id=row_11>
<input name=goal[is_complete] type=hidden value=0 />
<input id=goal_is_complete name=goal[is_complete] type=checkbox value=1 />
Goal name
div>
form>
修改1 当我不使用partial来包含_show_new_goal.html.erb partial来呈现表单时,会返回格式良好的HTML。 I.E.这将返回格式良好的HTML:
$("form#edit_goal_<%= @last_goal.id%>").after('<%= form_for( @goal, :url => user_goal_path(@goal.user, @goal), :method => :put, :remote => true) do |f| %><div id="row_<%=@goal.id%>"><%= f.check_box(:is_complete)%> <%= @goal.name%></div><% end %>');
HTML返回:
<form accept-charset="UTF-8" action="/users/1/goals/3" class="edit_goal" data-remote="true" id="edit_goal_3" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="blahblah" />
</div>
<div id="row_3">
<input name="goal[is_complete]" type="hidden" value="0" />
<input id="goal_is_complete" name="goal[is_complete]" type="checkbox" value="1" /> My goal
</div>
</form>