所以在上面的图像按钮中,我有一个名为“Mark Item as Done”的按钮。在Item表中,我有一个日期:due_date,string:task_title,text:description和boolean:done。
现在我将format.js
放在 todoitems_controller.rb 中,因为我认为这是放置它的最佳位置,因为我正在调用他们的编辑链接
todolist_todoitems GET /todolists/:todolist_id/todoitems(.:format) todoitems#index
POST /todolists/:todolist_id/todoitems(.:format) todoitems#create
new_todolist_todoitem GET /todolists/:todolist_id/todoitems/new(.:format) todoitems#new
edit_todolist_todoitem GET /todolists/:todolist_id/todoitems/:id/edit(.:format) todoitems#edit
todolist_todoitem GET /todolists/:todolist_id/todoitems/:id(.:format) todoitems#show
PATCH /todolists/:todolist_id/todoitems/:id(.:format) todoitems#update
PUT /todolists/:todolist_id/todoitems/:id(.:format) todoitems#update
DELETE /todolists/:todolist_id/todoitems/:id(.:format) todoitems#destroy
todolists GET /todolists(.:format) todolists#index
POST /todolists(.:format) todolists#create
new_todolist GET /todolists/new(.:format) todolists#new
edit_todolist GET /todolists/:id/edit(.:format) todolists#edit
todolist GET /todolists/:id(.:format) todolists#show
PATCH /todolists/:id(.:format) todolists#update
PUT /todolists/:id(.:format) todolists#update
DELETE /todolists/:id(.:format) todolists#destroy
root GET / todolists#index
现在,我是否也应该对 todoitems_controller.rb 中的def edit
或def update
做些什么?将remote: true
放在Todolists/_form.html.erb
文件中,它是放在那里的最佳位置还是应该将其放在Todoitems/_form.html.erb
文件中?完成所有这些后,下一步是什么?
这是我迄今为止所取得的成就,这是我第一次使用远程表单。我只是努力让这个开始,因为讲座令人困惑。如果还有其他缺失,我很乐意提供更多相关信息!任何帮助将不胜感激!
todolists / show.html.erb - 请查看button_to
。这是我需要帮助的部分
<p>
<% @paginate_items.each do |item| %>
<div class="list">
<form class="oneLine">
<a class="notDue">
<%= item.due_date %>
</a>
<a class="linkResults">
<%= link_to "#{item.task_title}", [@todolist, item], style: "font-weight: bold;" %>
<%= button_to "Mark Item as Done", remote: true %><br/> <br/>
</a>
</form>
<% end %>
todoitems_controller.rb - 我已将render.js
放入更新中。
def update
respond_to do |format|
if @todoitem.update(todoitem_params)
format.html { redirect_to @todolist, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: @todoitem }
render.js
else
format.html { render :edit }
format.json { render json: @todoitem.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todoitem
@todoitem = Todoitem.find(params[:id])
end
def set_todolist
@todolist = Todolist.find(params[:todolist_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todoitem_params
params.require(:todoitem).permit(:due_date, :task_title, :description, :done, :todolist_id)
end
todolists / _form.html.erb - 我已经放了remote: true
<body class="page">
<%= form_for(@todolist, remote: true) do |f| %>
<% if @todolist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@todolist.errors.count, "error") %> prohibited this todolist from being saved:</h2>
<ul>
<% @todolist.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :list_name %><br>
<%= f.text_field :list_name %>
</div>
<br/>
<div class="field">
<%= f.label :list_due_date %><br>
<%= f.text_field :list_due_date, class: 'dateSelect' %>
</div>
<br/>
<div class="actions">
<%= f.submit %>
</div>
<br/>
<% end %>
</body>
todoitems / _form.html.erb
<body class="page">
<%= form_for([@todolist, @todoitem], remote: true) do |f| %>
<% if @todoitem.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@todoitem.errors.count, "error") %> prohibited this todoitem from being saved:</h2>
<ul>
<% @todoitem.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :due_date %><br>
<%= f.text_field :due_date, class: 'dateSelect' %>
</div>
<br/>
<div class="field">
<%= f.label :task_title %><br>
<%= f.text_field :task_title %>
</div>
<br/>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description, size: "40x10" %>
</div>
<br/>
<% if !@todoitem.new_record? %>
<div class="field">
<%= f.label :done, 'Task Completed' %>
<%= f.check_box :done %>
</div>
<br/>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<br/>
<% end %>
</body>
答案 0 :(得分:0)
您尚未向button_to提供所有参数以使表单正常工作。这个帮助器方法生成一个表单,您需要提供表单的路径到POST。看看docs for the button_to helper,看看你错过了什么。
<%= button_to "Mark Item as Done", todolist_todoitem_path(@todolist, @todoitem), method: patch, remote_true %>
(我不确定你的路线是什么,所以我做了。)
当您更新资源时,您需要提供该方法(补丁或put用于更新)。您可能还想阅读路由。最初,它们可能有点混乱,但它们是交互式rails应用程序的核心,因此理解它们是关键。