2016-04-24 5 views
0

Webpage Viewリモートフォームの編集をRailsにセットアップする方法は?

上記の画像ボタンでは、「マークアイテムを完了」というボタンがあります。そのItemテーブルの中には、due_date、string:task_title、text:description、およびboolean:doneという日付があります。私は、私もやるべきこと、今

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.rbformat.jsを置きますののtodoitems_controller.rbにも、またはdef updateのように? remote: trueTodolists/_form.html.erbファイルに入れておくと、そこに置くのが最適なのですか、それともTodoitems/_form.html.erbファイルに入れるべきですか?そして、これらすべてをした後、次のステップは何ですか?

これは私がこれまで達成してきたことです。これはリモートフォームを使用して初めての作業です。私は、講義が混乱しているので、これを始めるには苦労しています。不足していることがあれば、私はこれについてさらに詳しい情報を提供しています!どんな助けもありがとう!

todolists/show.html.erb - button_toを参照してください。私はすでにアップデートでrender.jsを置く - それは私が

<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を助けが必要な部分です。

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 [email protected]_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

フォームはどのように提出できますか、フォームには 'action-url'がありません。フォームデータはどこに提出されるのですか? – illusionist

+0

フォームの中の 'input'、' checkbox'のような 'form-elements'もありません – illusionist

+0

@illusionistあなたが何を意味するのか分かりません... – ETUDESC

答えて

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のいずれかが更新のためのものです) 。ルーティングについても読むことができます。当初は少し混乱するかもしれませんが、対話型のレールアプリケーションの中心にあり、それらを理解することが重要です。

+0

パッチメソッドは何をしますか?そして私はルートリストを入れます – ETUDESC

+0

ルートとCRUD動詞 - http://guides.rubyonrails.org/routing.html#crud-verbs-and-actionsについて読むことができます。 docsにリンクして申し訳ありませんが、ほとんどのドキュメントがお客様の質問にお答えします。あなたは、何が起こった上記を試してみましたか?コンソールの[ネットワーク]タブで要求が行われているかどうかを確認するか、ログを調べて何が起きているかを確認してください。ログとコンソールを使用することは、物事がどこでどのように働いているのか、なぜ仕事をしないのかを理解する上で非常に役立ちます。 – margo

+0

ああまだ試していない。そして、なぜあなたはedit_todolist_todoitem_pathの代わりにtodolist_todoitem_pathを使用しますか? – ETUDESC

関連する問題