我正在尝试使用button_to帮助程序来远程更新我的数据库,虽然我遇到了一些问题,它似乎没有通过params。
在我看来我正在使用
- @availabilities.each do |a|
=button_to 'Accept', { :controller => 'availabilities', :action => :update, :id => a.id, :available => true }, :confirm => 'Are you sure?', :method => :post, :remote => true
并在控制器中
# PUT /availabilities/1
# PUT /availabilities/1.json
def update
@availability = Availability.find(params[:id])
respond_to do |format|
if @availability.update_attributes(params[:availability])
format.html { redirect_to @availability, :notice => 'Availability was successfully updated.' }
format.js
else
format.html { render :action => "edit" }
format.js
end
end
end
控制台输出
Started POST "/availabilities/2/edit?available=true" for 127.0.0.1 at 2013-02-25 21:43:30 +1100
ActionController::RoutingError (No route matches [POST] "/availabilities/2/edit"):
答案 0 :(得分:1)
看起来您已经设置了默认路由,该路由将POST
与控制器的创建操作匹配,并拒绝任何POST
ed数据以更新现有对象。< / p>
在按钮的代码中,将方法更改为PUT
。
=button_to 'Accept', { :controller => 'availabilities', :action => :update, :id => a.id, :available => true }, :confirm => 'Are you sure?', :method => :put, :remote => true
在帮助器中,我在视图中显示以下内容。
def toggle_admin(user)
if user.is_admin?
button_to "Yes", toggle_admin_path(user), :id => "toggle_admin_#{user.id}", :class => "btn btn-mini toggle-admin", :remote => true
else
button_to "No", toggle_admin_path(user), :id => "toggle_admin_#{user.id}", :class => "btn btn-inverse btn-mini toggle-admin", :remote => true
end
end
我的路线文件将toggle_admin_path
指向包含以下内容的用户设置控制器:
def toggle_admin
@user = User.find(params[:id])
@account = @user.account
if @user.is_admin? && @account.admins > 1
@user.remove_role :admin
else
@user.roles << :admin
end
if request.xhr?
render :status => 200, :content_type => 'text/javascript'
else
redirect_to edit_account_path
end
end