自定义控制器动作
问题描述:
我有一个按钮,在我的Rails项目更改数据库值:自定义控制器动作
项目显示视图(HAML)
= link_to("", :controller => "items", :action => "set_active", :id => @item.id) do
.btn Publish
上述ItemsController
def show
@item = Item.find(params[:id])
def set_active
@item.is_active = true
@item.save!
flash[:notice] = "Item got published"
end
end
该视图除按钮外正常工作。
如果我点击按钮,页面只刷新,但值不会改变,也不会弹出Flash消息。
任何想法为什么?
在此先感谢您的每一个答案!如果您需要更多信息,请告诉我。
答
类似的规定:
# controller
def set_active
@item.is_active = true
@item.save!
flash[:notice] = "Item got published"
end
# routes
resources :jobs do
get :set_active, on: :member
end
# view
link_to 'Activete', set_active_job_path(job)
+0
非常感谢!我想出了一个解决方案,基于你的好,干净的解决方案:) – Gugubaight
内部方法定义方法 - baaad;) –
我应该做的,在你看来,实现我的目标? :)我可以做一个私人的方法,但是这并没有解决问题... – Gugubaight