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", 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'

相关内容

  • 没有找到相关文章

最新更新