在我的rails应用程序中,我要求将Approvals
中的参数复制到Rotas
。
点击索引页面上Push
操作的链接时,我遇到了以下错误:param is missing or the value is empty: approval
我的审批索引页
<% if current_user.admin %>
<h1>Listing approvals</h1>
<% @approvals.each do |approval| %>
<table>
<tr>
<th></th>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<tr>
<th>Louise</th>
<td><%= approval.grid11 %></td>
<td><%= approval.grid12 %></td>
<td><%= approval.grid13 %></td>
<td><%= approval.grid14 %></td>
<td><%= approval.grid15 %></td>
<td><%= approval.grid16 %></td>
<td><%= approval.grid17 %></td>
</tr>
<tr>
<th>Ruairi</th>
<td><%= approval.grid21 %></td>
<td><%= approval.grid22 %></td>
<td><%= approval.grid23 %></td>
<td><%= approval.grid24 %></td>
<td><%= approval.grid25 %></td>
<td><%= approval.grid26 %></td>
<td><%= approval.grid27 %></td>
</tr>
<tr>
<th>Gavin</th>
<td><%= approval.grid31 %></td>
<td><%= approval.grid32 %></td>
<td><%= approval.grid33 %></td>
<td><%= approval.grid34 %></td>
<td><%= approval.grid35 %></td>
<td><%= approval.grid36 %></td>
<td><%= approval.grid37 %></td>
</tr>
<tr>
<th>Conn</th>
<td><%= approval.grid41 %></td>
<td><%= approval.grid42 %></td>
<td><%= approval.grid43 %></td>
<td><%= approval.grid44 %></td>
<td><%= approval.grid45 %></td>
<td><%= approval.grid46 %></td>
<td><%= approval.grid47 %></td>
</tr>
<tr>
<th>Michael K</th>
<td><%= approval.grid51 %></td>
<td><%= approval.grid52 %></td>
<td><%= approval.grid53 %></td>
<td><%= approval.grid54 %></td>
<td><%= approval.grid55 %></td>
<td><%= approval.grid56 %></td>
<td><%= approval.grid57 %></td>
</tr>
<tr>
<th>Emily</th>
<td><%= approval.grid61 %></td>
<td><%= approval.grid62 %></td>
<td><%= approval.grid63 %></td>
<td><%= approval.grid64 %></td>
<td><%= approval.grid65 %></td>
<td><%= approval.grid66 %></td>
<td><%= approval.grid67 %></td>
</tr>
<tr>
<th>Stephen W</th>
<td><%= approval.grid71 %></td>
<td><%= approval.grid72 %></td>
<td><%= approval.grid73 %></td>
<td><%= approval.grid74 %></td>
<td><%= approval.grid75 %></td>
<td><%= approval.grid76 %></td>
<td><%= approval.grid77 %></td>
</tr>
<tr>
<th>Niamh</th>
<td><%= approval.grid81 %></td>
<td><%= approval.grid82 %></td>
<td><%= approval.grid83 %></td>
<td><%= approval.grid84 %></td>
<td><%= approval.grid85 %></td>
<td><%= approval.grid86 %></td>
<td><%= approval.grid87 %></td>
</tr>
<tr>
<th>Josh</th>
<td><%= approval.grid91 %></td>
<td><%= approval.grid92 %></td>
<td><%= approval.grid93 %></td>
<td><%= approval.grid94 %></td>
<td><%= approval.grid95 %></td>
<td><%= approval.grid96 %></td>
<td><%= approval.grid97 %></td>
</tr>
<tr>
<th>Aidan</th>
<td><%= approval.grid101 %></td>
<td><%= approval.grid102 %></td>
<td><%= approval.grid103 %></td>
<td><%= approval.grid104 %></td>
<td><%= approval.grid105 %></td>
<td><%= approval.grid106 %></td>
<td><%= approval.grid107 %></td>
</tr>
<tr>
<th>Courtney</th>
<td><%= approval.grid111 %></td>
<td><%= approval.grid112 %></td>
<td><%= approval.grid113 %></td>
<td><%= approval.grid114 %></td>
<td><%= approval.grid115 %></td>
<td><%= approval.grid116 %></td>
<td><%= approval.grid117 %></td>
</tr>
</table>
<% end %>
<table>
<thead>
<tr>
<th>Week</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @approvals.each do |approval| %>
<tr>
<td><%= approval.week %></td>
<td><%= link_to 'Approve This', approvals_push_path(:id => approval.id) %></td>
<td><%= link_to 'Show', approval %></td>
<td><%= link_to 'Edit', edit_approval_path(approval) %></td>
<td><%= link_to 'Destroy', approval, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% end %>
<%= link_to 'New Approval', new_approval_path %>
我的ApprovalsController
class ApprovalsController < ApplicationController
before_action :set_approval, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
respond_to :html
def index
@approvals = Approval.all
respond_with(@approvals)
end
def push
@approval = Approval.find(params[:id])
@rotum = Rotum.find_by_id(@approval.rotumid)
@rotum.update_attributes(push_params)
end
def show
respond_with(@approval)
end
def new
@rota = Rotum.find(params[:id])
@approval = Approval.new
respond_with(@approval)
end
def edit
end
def create
@approval = Approval.new(approval_params)
@approval.save
respond_with(@approval)
end
def update
@approval.update(approval_params)
respond_with(@approval)
end
def destroy
@approval.destroy
respond_with(@approval)
end
private
def set_approval
@approval = Approval.find(params[:id])
end
def set_rotum
@rotum = Rotum.find(:rotumid)
end
def approval_params
params.require(:approval).permit(:week, :grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117, :rotumid)
end
def push_params
params.require(:approval).permit(:grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117)
end
def rotum_params
params.require(:rotum).permit(:week, :grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117)
end
end
非常感谢任何帮助!
答案 0 :(得分:0)
与以下问题类似:
Param is missing or the value is empty user Rails 4
和
Param is missing or the value is empty
您的问题似乎是由您在控制器中使用强参数的方式引起的。基本上,您不会将请求中的必需approval
字段传递给approvals_push_path
,只需发送id
批准。
def push
@approval = Approval.find(params[:id])
@rotum = Rotum.find_by_id(@approval.rotumid)
# Throws error, because there is no approval param in params
@rotum.update_attributes(push_params)
end
为避免错误,您可以从@approval
对象中选择要复制的字段:
def push
@approval = Approval.find(params[:id])
@rotum = Rotum.find_by_id(@approval.rotumid)
# We have to select which attributes we want to copy.
@rotum.update_attributes(attrs_for_push)
end
def attrs_for_push
# Let's select only attributes we need.
@approval.attributes.slice(:week, :grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117)
end
您可能希望将复制逻辑从控制器移动到模型或单独的实用程序服务对象。