2016-09-26 11 views
0

レコードを削除せずにステータスを「キャンセル」に更新し、キャンセル前の最後のステータスを保存し、ユーザーからの理由(入力)を記録する、ユーザープロジェクトの「キャンセル」メソッドが必要です。カスタムメソッドでネストされたリソースは機能しませんか?

私は、ユーザー(開発者)用のコントローラとネストされたプロジェクトを持っています。 RESTfulなアクションはうまくいくようですが、キャンセルオプションはありません。私はそれを理解できません!私は私の見解でrake routes

   user_project_cancel GET /users/:user_id/projects/:project_id/cancel(.:format) projects#cancel 
           POST /users/:user_id/projects/:project_id/cancel(.:format) projects#cancel_save 
        user_projects GET /users/:user_id/projects(.:format)     projects#index 
           POST /users/:user_id/projects(.:format)     projects#create 
       new_user_project GET /users/:user_id/projects/new(.:format)    projects#new 
       edit_user_project GET /users/:user_id/projects/:id/edit(.:format)   projects#edit 
        user_project GET /users/:user_id/projects/:id(.:format)    projects#show 
           PATCH /users/:user_id/projects/:id(.:format)    projects#update 
           PUT /users/:user_id/projects/:id(.:format)    projects#update 
           DELETE /users/:user_id/projects/:id(.:format)    projects#destroy 

マイprojects_controller.rb(私は念の全部を含む)

class ProjectsController < ApplicationController 
before_filter :deny_to_visitors 
before_action :correct_user, only: [:show, :edit, :update, :destroy, :cancel, :cancel_save] 
before_action :set_current_status, only: [:cancel, :cancel_save] 

def index 
    @projects = Project.all.order(id: :asc) 
end 

def show 
end 

def new 
    @project = current_user.projects.new 
    @budgets = Budget.all 
end 

def edit 
end 

def create 
    @project = current_user.projects.new(project_params) 
    @project.project_status_id = 1 
    if @project.save 
    redirect_to root_path, notice: 'Your project is being reviewed. We will be in contact soon!' 
    AdminMailer.new_project_email(current_user, @project).deliver_now 
    else 
    render :new 
    end 
end 

def update 
    if @project.update(project_params) 
    if admin_signed_in? 
     redirect_to admin_projects_list_path, notice: 'Project was successfully updated.' 
    else 
     redirect_to root_path, notice: 'Project was successfully updated.' 
    end 
    else 
    render :edit 
    end 
end 

def destroy 
    @project.destroy 
    redirect_to projects_url, notice: 'Project was successfully destroyed.' 
end 

def cancel 
end 

def cancel_save 
    @project.cancel_reason.update(:project_id, :last_status_id, :comment) 
    @project.update_attribute(:project_status_id, 10) 

    redirect_to root_path, notice: 'Project has been successfully cancelled.' 
end 

private 
    # Use callbacks to share common setup or constraints between actions. 
    def deny_to_visitors 
    redirect_to root_path unless user_signed_in? or admin_signed_in? 
    end 

def correct_user 
    if admin_signed_in? 
    @project = Project.find_by(id: params[:id]) 
    else 
    @project = current_user.projects.find_by(id: params[:id]) 
    end 
    redirect_to user_projects_path, notice: "You are not authorised to view this project" if @project.nil? 
    end 

def set_current_status 
    @current_status = current_user.projects.find_by(id: params[:id]).project_status_id 
end 

def project_params 
    params.require(:project).permit(:user_id, :project_type_id, :name, :industry_id, :description, :budget_id, :project_status_id, feature_ids:[], addon_ids:[]) 
end 

end 

リンクを実行すると、ここで

は私のroutes.rb

devise_for :users 

resources :users do 
    resources :projects do 
    get "cancel" => 'projects#cancel' 
    post "cancel" => 'projects#cancel_save' 
    end 
end 

ある

<%= link_to 'Edit', edit_user_project_path(current_user, project) %> 
<%= link_to 'Cancel', user_project_cancel_path(current_user, project) %> 

私はcancel.html.erbのためのスーパーシンプルなページを持っていますが、フォームがまだ存在していない瞬間に、何がうまくいかないのかがわかります。

Started GET "https://stackoverflow.com/users/1/projects/3/cancel" for ::1 at 2016-09-26 17:21:50 +0200 
    Processing by ProjectsController#cancel as HTML 
    Parameters: {"user_id"=>"1", "project_id"=>"3"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]] 
    Project Load (0.3ms) SELECT "projects".* FROM "projects" WHERE "projects"."user_id" = $1 AND "projects"."id" IS NULL LIMIT $2 [["user_id", 1], ["LIMIT", 1]] 
    Redirected to http://localhost:3000/users/1/projects 
    Filter chain halted as :correct_user rendered or redirected 
    Completed 302 Found in 6ms (ActiveRecord: 0.8ms) 

それは上記のプロジェクトをロードすると、:

私はプロジェクトをキャンセルするリンクをクリックすると、私はレール・サーバーのコンソールログにこれを取得更新

<h1>Cancel</h1> 
<p><%= @project.name %></p> 

負荷がAND "projects"."id" IS NULL LIMIT $2 - NULLの値がproject_idであることが明らかになったときに、なぜ3 ??

答えて

0

[OK]をクリックします。私は、次の操作を行って、それを修正:routes.rb

私はライン変更:これは私のルーティングを変更し

get "cancel", :on => :member 

get "cancel" => 'projects#cancel' 

を。だから私はrake routes、私のルートは今、このように見えたとき:

のRESTfulパス機能と一致するようになりました
cancel_user_project GET /users/:user_id/projects/:id/cancel(.:format) projects#cancel 

(例:編集のために、パスがedit_user_projectです)。

古いリンク

<%= link_to 'Cancel', user_project_cancel_path(current_user, project) %> 

新規リンク

<%= link_to 'Cancel', cancel_user_project_path(current_user, project) %> 

そして今、それが動作します:次のように私はその後、私のリンクを更新しました。それはルーティングのカスタムアクションに満足していなかった理由はまだ完全にはわかりませんが、それは明らかに問題でした。

0

以下の関連付けの出力を確認できますか?これは少し変だった

current_user.projects

+0

申し訳ありません私はまだレールを学んでいます - その関連の出力を調べる最良の方法は何ですか? – Stephen

+0

簡単なテストとして、私は 'index'メソッドを次のように変更しました。 '@projects = Project.all.order(id::asc)'の代わりに '@projects = current_user.projects'を使いました。インデックスページで同じ結果を得ました。すべてのユーザプロジェクトをリスト表示します – Stephen

+0

pry gemからrails consoleまたはdebuggを使用できます。だからあなたがレールを使用している場合はちょうどタイプ... User.find(id).projects ...ここでは、現在のユーザーIDのユーザーのIDを渡します。また、UserクラスとProjectクラスの関連付けも確認してください。コードごとに、ユーザーhas_manyプロジェクトとプロジェクトbelongs_toユーザーである必要があります。あなたがadmin_signed_inで何をする必要があるのか​​を教えてください。調子。 –

関連する問題