[英]In my Rails 4 app, I'm getting “undefined method `admin?'” even though I have defined a method in my user model
[英]I cannot call method even though I defined it in rails
<% @links.each do |link| %>
<p class="source list-group-item"><%= link_to 'Plz', songs_path,
method: :current_user.save_link(link), data: {confirm: "Are you sure?"} %></p>
<% end %>
我试图激活该方法,但收到错误消息说没有这样的方法。
这是我的 controller 代码。
def self.save_link(linkurl)
@current_user = current_user.artist_url
@current_user= linkurl
@current_user.save
flash[:notice] = "Saved!"
redirect_to root_path
end
在链接上指定一个method
,不会做你期望它做的事情,因为这不是你指定要执行哪个controller方法的方式。 method
是请求类型。 要从这样的链接点击 controller 方法,您需要创建一条到它的路由并将其用作您的路径。
路线.rb
patch '/save_link', to: 'controller#save_link'
我们在这里将方法设置为:patch
,因为您正在更新信息。 如果需要,您也可以使用:post
,但:patch
可能更接近这里的最佳实践。
<%= link_to 'Plz', save_link_path(link: link), method: :patch, data: {confirm: "Are you sure?"} %>
你的方法也应该是这样的
def save_link
@current_user = current_user.artist_url
@current_user = params[:link]
@current_user.save
flash[:notice] = "Saved!"
redirect_to root_path
end
在此处查看有关轨道路由的文档以获取更多信息: https://guides.rubyonrails.org/routing.html#resources-on-the-web
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.