link_to 中的参数不正确
incorrect params in link_to
我想单击名为 "User" 和 "Admin" 的下拉项,这应该将我帐户 table 中的角色列更新为此值。
%tr
%td
.dropdown
%button#dropdownMenuLink.btn.btn-outline.btn-sm.dropdown-toggle{"aria-expanded" => "false", "aria-haspopup" => "true", "data-toggle" => "dropdown", :type => "button"}
=account.role
.dropdown-menu{"aria-labelledby" => "dropdownMenuButton"}
.dropdown-item #{link_to "User", account.update_attribute("role", "user")}
.dropdown-item #{link_to "Admin", account.update_attribute("role", "admin")}
我收到这个错误:
undefined method `to_model' for true:TrueClass Did you mean? to_yaml
您需要在控制器中更新属性,而不是在视图中。您可以使用 link_to 转到操作并传递其他参数。
%tr
%td
.dropdown
%button#dropdownMenuLink.btn.btn-outline.btn-sm.dropdown-toggle{"aria-expanded" => "false", "aria-haspopup" => "true", "data-toggle" => "dropdown", :type => "button"}
=account.role
.dropdown-menu{"aria-labelledby" => "dropdownMenuButton"}
.dropdown-item= link_to "User", path_to_your_action(id: account.id, role: :user)
.dropdown-item= link_to "Admin", path_to_your_action(id: account.id, role: :admin)
无论如何,您处理更新和重定向的控制器应该如下所示:
accounts_controller.rb
def your_action
@account = Account.find(params[:id])
if @account.update(role: params[:role])
redirect_to # <somewhere>
end
end
而不是link_to
,你最好使用form_for
。
.dropdown-item
= form_for account do |f|
= f.hidden_field :role, value: :user
= f.submit 'User'
我想单击名为 "User" 和 "Admin" 的下拉项,这应该将我帐户 table 中的角色列更新为此值。
%tr
%td
.dropdown
%button#dropdownMenuLink.btn.btn-outline.btn-sm.dropdown-toggle{"aria-expanded" => "false", "aria-haspopup" => "true", "data-toggle" => "dropdown", :type => "button"}
=account.role
.dropdown-menu{"aria-labelledby" => "dropdownMenuButton"}
.dropdown-item #{link_to "User", account.update_attribute("role", "user")}
.dropdown-item #{link_to "Admin", account.update_attribute("role", "admin")}
我收到这个错误:
undefined method `to_model' for true:TrueClass Did you mean? to_yaml
您需要在控制器中更新属性,而不是在视图中。您可以使用 link_to 转到操作并传递其他参数。
%tr
%td
.dropdown
%button#dropdownMenuLink.btn.btn-outline.btn-sm.dropdown-toggle{"aria-expanded" => "false", "aria-haspopup" => "true", "data-toggle" => "dropdown", :type => "button"}
=account.role
.dropdown-menu{"aria-labelledby" => "dropdownMenuButton"}
.dropdown-item= link_to "User", path_to_your_action(id: account.id, role: :user)
.dropdown-item= link_to "Admin", path_to_your_action(id: account.id, role: :admin)
无论如何,您处理更新和重定向的控制器应该如下所示:
accounts_controller.rb
def your_action
@account = Account.find(params[:id])
if @account.update(role: params[:role])
redirect_to # <somewhere>
end
end
而不是link_to
,你最好使用form_for
。
.dropdown-item
= form_for account do |f|
= f.hidden_field :role, value: :user
= f.submit 'User'