1

button_toヘルパーを使用してデータベースをリモートで更新しようとしていますが、いくつかの問題があり、パラメーターを渡していないようです。

私の見解の中で私は使用しています

- @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"):
4

1 に答える 1

1

コントローラーの作成アクションに一致し、既存のオブジェクトへの更新の ed データをPOST拒否するデフォルトのルートが設定されているようです。POST

ボタンのコードで、メソッドを に変更します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
于 2013-02-25T11:09:09.030 に答える