我有一个Entry
模型,其布尔列published
,默认设置为false
。我写在模型下面的方法:发布方法无法正常工作
def self.publish
self.update(published: true)
end
,并在我的控制器我有
def publish
@entry = Entry.find(params[:id]
@entry.publish
redirect_to entries_path
end
(我想使它类似destroy
方法的模型调用)。最后,我认为我有这样的:
<%= link_to "Publish", entries_path, method: :publish %>
但是,当我点击链接,该请求被create
方法处理,并返回我下面的错误:
ActionController::ParameterMissing in Multiflora::EntriesController#create
param is missing or the value is empty: entry
你在这里做了很多错误的事情。您是否已将“发布”添加到您的路线?请发布您的'routes.rb'。 – dp7
@dkp我看到..可能有另一种方法来设置布尔值为真,而不重新发明轮子? @ _ @ – AlexNikolaev94
首先,您需要了解'link_to'中传递的'method'是什么。这是一个HTTP请求,也就是说它可以是'post'或'put'或'delete'或'get'(默认)。你已经分配了'method::publish'这是错误的,因为你正在尝试更新它应该是'method :: put'。其次,你的路线'entries_path'应该指向你的控制器的'publish'动作。而且,最后'publish'应该是模型中的**实例方法**。目前,它是一种类方法。 – dp7