我有一个包含布尔列published的Entry模型,该列在缺省情况下设置为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发布于 2016-08-20 23:38:13
感谢所有的回答,我已经找出了我的错误所在,但我稍微考虑了一下,决定让它变得更简单:我只是添加了一个复选框来编辑表单,它将条目的:published属性设置为真。这就是它:
<%=form_for(@entry, as: :entry, url: content_entry_path(@entry)) do |f| %>
# ...
<p>
<%= f.label "Publish" %> <br />
<%= f.hidden_field :published, value: '' %>
<%= f.check_box :published, checked: true %>
</p>
<% end %>无论如何,非常感谢你的回答!那是我缺乏知识,我会记住我做错了什么
发布于 2016-08-20 23:22:55
首先,没有名为:publish的超文本传输协议方法,它应该是:put或:patch
其次,您需要将id作为参数传递
<%= link_to "Publish", publish_entry_path(@entry) %>此外,您还需要为发布操作添加路由
resources :events do
member do
put :publish
end
endpublish方法应为实例方法
def publish
self.update(published: true)
end发布于 2016-08-20 23:31:19
根据API,该方法在link_to中是错误的,因此您必须提到一个有效的Http方法(在您的情况下首选补丁),然后编辑您的route.rb文件以将此补丁请求传输到您指定的函数,如下所示:
patch'/entries/publish', to: 'entries#publish'然后将"entries_path“更改为"entry_path”
因此,链接代码应如下所示:
<%= link_to "Publish", entry_path, method: :patch%>https://stackoverflow.com/questions/39055496
复制相似问题