Ruby on rails 单击“更新数据库”
大家好,当用户单击某个链接时,我正在尝试更新我的数据库(PostgreSQL)中的一个值,尽管我所做的工作正常,但我认为这不是最佳做法。如果有人能向我展示如何更好地实现这一点,那将是非常棒的 看法 路线Ruby on rails 单击“更新数据库”,ruby-on-rails,routes,haml,Ruby On Rails,Routes,Haml,大家好,当用户单击某个链接时,我正在尝试更新我的数据库(PostgreSQL)中的一个值,尽管我所做的工作正常,但我认为这不是最佳做法。如果有人能向我展示如何更好地实现这一点,那将是非常棒的 看法 路线 resources :availabilities do put 'accept', :on => :member put 'decline', :on => :member end 你所做的没有什么特别的错误。您的控制器不需要使用update_属性,您没有向其
resources :availabilities do
put 'accept', :on => :member
put 'decline', :on => :member
end
你所做的没有什么特别的错误。您的控制器不需要使用update_属性,您没有向其传递任何属性。您只需将其
保存
即可,更改为。可用
def accept
@availability = Availability.find(params[:id])
@availability.available = true
respond_to do |format|
if @availability.save
format.html { render :nothing => true }
format.js
else
format.html { render :action => "edit" }
format.js
end
end
end
你可以这样整理你的路线:
resources :availabilities do
member do
put :accept
put :decline
end
end
有没有更好的方法使用链接/路线?它们看起来绝对不错。你的路线可以简化一点,答案更新。你的链接很好,
link\u to\u remote
曾经是这样做的,但是在rails 3中被弃用,取而代之的是:remote=>true
。虽然我需要在我的链接中使用use:method=>“put”
,你确实需要,如果你不把它放在那里,链接到默认值get
,即使它是远程的。@Paul'Whippet'McGuane这对你有用吗?可以找到有关链接到的更多详细信息-尽管您正确使用了它:)
def accept
@availability = Availability.find(params[:id])
@availability.available = true
respond_to do |format|
if @availability.save
format.html { render :nothing => true }
format.js
else
format.html { render :action => "edit" }
format.js
end
end
end
resources :availabilities do
member do
put :accept
put :decline
end
end