Jquery 从_form.html.erb到index.html.erb的Ajax帖子
在不刷新页面的情况下,我无法从索引页面的表单部分获取数据。仅发布名称、说明和复选框 表格:Jquery 从_form.html.erb到index.html.erb的Ajax帖子,jquery,ruby-on-rails,ajax,Jquery,Ruby On Rails,Ajax,在不刷新页面的情况下,我无法从索引页面的表单部分获取数据。仅发布名称、说明和复选框 表格: <%= form_for(@objective) do |form| %> <div class="name field"> <%= form.label :name %> <%= form.text_field :name %> </div> <div class="description field">
<%= form_for(@objective) do |form| %>
<div class="name field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="description field">
<%= form.label :description %>
<%= form.text_area :description %>
</div><br>
<div class="user field">
<%= form.label :user_id %>
<%= form.collection_select :user_id, User.all, :id, :name %>
</div>
<div class="date field">
<%= form.label :due_date %>
<%= form.date_select :due_date %>
</div>
<div class="group field">
<%= form.label :group %>
<%= form.collection_select :group_id, Group.all, :id, :name %>
</div>
<div class="completed field">
<%= form.label :completed %>
<%= form.check_box :completed %>
</div>
<div id="post" class="actions">
<%= form.submit %>
</div>
<% end %>
Index.html.erb
<div id="objectiveList">
<h1>Objectives</h1>
<table id="obj table">
<thead>
<tr>
<th>Assigned to</th>
<th>Name</th>
<th>Description</th>
<th>Due Date</th>
<th>Completed</th>
<th>Reveal</th>
<th>Make Changes</th>
<th>Clear</th>
</tr>
</thead>
<tbody>
<% @objectives.each do |objective| %>
<tr>
<!-- move into its own partial? render @objective? -->
<td id="postUser"><%= objective.user.name %></td>
<td id="postName"><%= objective.name %></td>
<td id="postDescription"><%= objective.description %></td>
<td id="postDate"><%= objective.due_date.strftime("%B %d, %Y") %></td>
<td id="postComplete"><%= objective.completed %></td>
<td><button><%= link_to 'Show', objective %></button></td>
<% if current_user %>
<td><button><%= link_to 'Edit', edit_objective_path(objective) %></button></td>
<td>
<button><%= link_to 'Destroy', objective, method: :delete, data: {confirm: 'Are you sure?'} %></button>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
目标
分配给
名称
描述
到期日
完整的
显露
改变
清楚的
def create
@objective = Objective.new(objective_params)
respond_to do |format|
if @objective.save
format.js
# comment out format.html to resolve 422 error
#format.html {redirect_to @objective, notice: 'Objective was successfully created.'}
format.json {render json: @objective}
else
format.html {render :new}
format.json {render json: @objective.errors, status: :unprocessable_entity}
end
end
end
<div id="objectiveList">
<h1>Objectives</h1>
<table id="obj table">
<thead>
<tr>
<th>Assigned to</th>
<th>Name</th>
<th>Description</th>
<th>Due Date</th>
<th>Completed</th>
<th>Reveal</th>
<th>Make Changes</th>
<th>Clear</th>
</tr>
</thead>
<tbody>
<% @objectives.each do |objective| %>
<tr>
<!-- move into its own partial? render @objective? -->
<td id="postUser"><%= objective.user.name %></td>
<td id="postName"><%= objective.name %></td>
<td id="postDescription"><%= objective.description %></td>
<td id="postDate"><%= objective.due_date.strftime("%B %d, %Y") %></td>
<td id="postComplete"><%= objective.completed %></td>
<td><button><%= link_to 'Show', objective %></button></td>
<% if current_user %>
<td><button><%= link_to 'Edit', edit_objective_path(objective) %></button></td>
<td>
<button><%= link_to 'Destroy', objective, method: :delete, data: {confirm: 'Are you sure?'} %></button>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>