Rails button to update status of nested attributes

ruby-on-rails

46 观看

1回复

588 作者的声誉

I have a model Project and a model ProjectLine. Project has nested attributes for ProjectLine.

On my projects show view I'm listing project info with list of tasks.

 <% @project.project_lines.each do |project_line| %>

 <%= project_line.user.name %>
 <%= project_line.description %>
 <%= number_to_currency project_line.amount, precision: 0, unit: "USD ", separator: ",", delimiter: "," %>
 <%= project_line.task_status_name %>

 <% end %>

Tasks are created on project form using cocoon.

In the view above, after status name, I would like to have a button for each project line that changes the status.

The solution I found so far is to create a form.

<%= semantic_form_for @project_line do |f| %>

  <%= f.hidden_field :status, :value=>2 %>
  <%= f.submit 'ACCEPT', class: 'btn btn-primary btn-md' %>

<% end %>

I tried to place it on a partial on projects and call it on the loop <% @project.project_lines.each do |project_line| %>but i can't get it work.

How can I get a button for each project line to change project line status?

作者: Catmal 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

1226 作者的声誉

If i understand your request, somthing along the lines of

view

<% @project.project_lines.each do |project_line| %>

  <%= project_line.user.name %>
  <%= project_line.description %>
  <%= number_to_currency project_line.amount, precision: 0, unit: "USD ", separator: ",", delimiter: "," %>
  <%= project_line.task_status_name %>
  <button class="accept_to_server">Accept</button>
 <% end %>

javascript(jquery)

$.('.accept_to_server').on('click', function(){
  $.ajax({
    url: 'path/to/accept/route'
    method: 'PUT' // or what method your route uses
    data: {data:you, need:here}
  }).done(function(data){
    // update your view
  }).fail(function(){
    // something went wrong
  });
};

Hope this helps out.

作者: Jon 发布者: 2017 年 12 月 27 日
32x32