通过按钮单击编辑表中的值(Ruby on Rails)

时间:2012-12-20 16:10:34

标签: ruby-on-rails button controller icons

我有点惭愧,因为现在超过2个小时我无法弄清楚如何做到这一点。但我对Rails很新,所以忍受我:)

我有两组用户:海报和实干家 然后有一个模型“任务”。分配由海报创建(与他们具有belongs_to关系)。 然后,实施者可以从未分配的池中选择分配并将它们分配给自己(这就是为什么分配也与Doers具有belongs_to关系)。

在页面本身上我现在有这个代码(在过去一小时内改了20次):

<table class="table">
  <thead>
    <tr>
      <th>Title</th>
      <th>Assign to me</th>
    </tr>
  </thhead>      
  <tbody>
    <% @assignments.each do |assignment| %>
      <tr>
        <td><%= assignment.title %></td>
        <td>
           <%= link_to(edit_assignment_path(assignment), :doer => current_doer.id, confirm: 'Really take this assignment on?') do%>
             <i class="icon-folder-open", alt="assign"></i>
           <% end %> 
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

在AssignmentController中使用此代码:

def edit
  @assignment = Assignment.find(params[:id])
  @doer = Doer.find(params[:doer])
  if @patient.assign(@doer)
    redirect_to dashboard_index_path, notice: 'Successfully assigned to you'
  else
    redirect_to dashboard_index_path, alert: 'Error while trying to assign to you'
  end
end

这在Assignment.rb中:

def assign(current_doer)
  if self.doer_id != nil then 
    false 
  else 
    self.doer_id << current_doer
    true
  end
end

正如您所看到的,我在此示例中以doer身份登录。我不能为我的生活弄清楚如何正确和干净!

我还尝试了link_to代码部分,赋值函数等的变体。 目前我收到错误,@ doer = Doer.find(params [:doer])无法搜索Nil的id。我知道这意味着我并没有真正地发送doer_id参数,但我是在初学者知识端。

非常感谢你帮助我。

问候, Lordylike

1 个答案:

答案 0 :(得分:0)

你几乎把它当了,当使用带有块的link_to时,它接受参数变化的方式有点变化。像这样使用路径助手传递参数。

<%= link_to(edit_assignment_path(assignment, doer: current_doer.id), confirm: 'Really take this assignment on?') do %>
  <i class="icon-folder-open", alt="assign"></i>
<% end %>