2016-05-15 38 views
1

私はこの奇妙なエラーが発生しました。これは私のアプリのroutes.rb部分から来ていると仮定しています。私はユーザーがフォームに入るときに改訂者になる機会を与えようとしています。ユーザーは一度だけ改訂者になることができるので、そのhas_one reviseruser.rbモデルありがとう!未定義のメソッド `user_revisers_path '" NoMethodError in Revisers#new "

routes.rbを:

Rails.application.routes.draw do 
    root 'pages#home' 

    devise_for :users , 
      :path => '' , 
      :path_names => { :sign_in => 'login', :sign_out => 'logout', :edit => 'profile' }, 
      :controllers => { :omniauth_callbacks => 'omniauth_callbacks', 
           :registrations => 'registrations' 
          } 

    resources :users, only: [:index, :show] do 
    resource :reviser 
    end 

revisers_controller:

class RevisersController < ApplicationController 
    before_action :set_reviser, only: [:show, :edit, :update] 
    before_action :authenticate_user!, except: [:show] 

    def index 
    @reviser = current_user.reviser 
    end 

    def show 
    end 

    def new 
    @reviser = current_user.build_reviser(params[:reviser]) 
    @user = User.find(params[:user_id]) 
    end 

    def create 
    @reviser = current_user.reviser.build(reviser_params) 

    if @reviser.save 
     redirect_to @reviser,notice: "saved...." 
    else 
     render :new 
    end 
    end 

    def edit 
    set_reviser 
    end 

    def update 
    set_reviser 

    if @reviser.update(reviser_params) 
     redirect_to @reviser, notice: "updated.." 
    else 
     render :edit 
    end 
    end 

    private 
    def set_reviser 
    @reviser = Reviser.find(params[:id]) 
    end 

    def reviser_params 
    params.require(:reviser).permit(:description, :average_start, :average_end, :max_pages, :price_per, :active) 
    end 
end 

new.html

<%= form_for [current_user, @reviser] do |f| %> 
    <div class="row"> 
    <div class="div.col-md-4 select"> 
     <div class="form-group"> 
     <label>dsd</label> 
     <%= f.input :description, label: false, class: 'controls',:input_html => { :id => 'description' } %> 
     </div> 
    </div> 
    </div> 

    <%= f.submit "Become Adviser", class: "btn btn-large btn-primary" %> 
<% end %> 

エラーログ:

Rendered revisers/_form.html.erb (14.7ms) 
    Rendered revisers/new.html.erb within layouts/application (15.9ms) 
Completed 500 Internal Server Error in 48ms (ActiveRecord: 1.1ms) 

ActionView::Template::Error (undefined method `user_revisers_path' for #<#<Class:0x007faf35f0cc60>:0x007faf35ee7e60>): 
    5:  <div class="panel-body"> 
    6:   <div class="container"> 
    7: 
    8:   <%= form_for [current_user, @reviser] do |f| %> 
    9: 
    10:   <div class="row"> 
    11:    <div class="div.col-md-4 select"> 
    app/views/revisers/_form.html.erb:8:in `_app_views_revisers__form_html_erb___2471522092853631188_70195335070980' 
    app/views/revisers/new.html.erb:1:in `_app_views_revisers_new_html_erb___1451348464463745171_70195280947160' 

レーキ路線:

Prefix Verb  URI Pattern          Controller#Action 
           root GET /            pages#home 
        new_user_session GET  /login(.:format)         devise/sessions#new 
         user_session POST  /login(.:format)         devise/sessions#create 
       destroy_user_session DELETE /logout(.:format)         devise/sessions#destroy 
       user_omniauth_authorize GET|POST /auth/:provider(.:format)       omniauth_callbacks#passthru {:provider=>/facebook/} 
       user_omniauth_callback GET|POST /auth/:action/callback(.:format)     omniauth_callbacks#(?-mix:facebook) 
         user_password POST  /password(.:format)        devise/passwords#create 
        new_user_password GET  /password/new(.:format)       devise/passwords#new 
        edit_user_password GET  /password/edit(.:format)       devise/passwords#edit 
             PATCH /password(.:format)        devise/passwords#update 
             PUT  /password(.:format)        devise/passwords#update 
      cancel_user_registration GET  /cancel(.:format)         registrations#cancel 
        user_registration POST /            registrations#create 
       new_user_registration GET  /sign_up(.:format)         registrations#new 
       edit_user_registration GET  /profile(.:format)         registrations#edit 
             PATCH /            registrations#update 
             PUT /            registrations#update 
             DELETE /            registrations#destroy 
        user_confirmation POST  /confirmation(.:format)       devise/confirmations#create 
       new_user_confirmation GET  /confirmation/new(.:format)      devise/confirmations#new 
             GET  /confirmation(.:format)       devise/confirmations#show 
         user_reviser POST  /users/:user_id/reviser(.:format)     revisers#create 
        new_user_reviser GET  /users/:user_id/reviser/new(.:format)    revisers#new 
        edit_user_reviser GET  /users/:user_id/reviser/edit(.:format)    revisers#edit 
             GET  /users/:user_id/reviser(.:format)     revisers#show 
             PATCH /users/:user_id/reviser(.:format)     revisers#update 
             PUT  /users/:user_id/reviser(.:format)     revisers#update 
             DELETE /users/:user_id/reviser(.:format)     revisers#destroy 
           users GET  /users(.:format)         users#index 
           user GET  /users/:id(.:format)        users#show 
           photos GET  /photos(.:format)         photos#index 
             POST  /photos(.:format)         photos#create 
          new_photo GET  /photos/new(.:format)        photos#new 
          edit_photo GET  /photos/:id/edit(.:format)       photos#edit 
           photo GET  /photos/:id(.:format)        photos#show 
             PATCH /photos/:id(.:format)        photos#update 
             PUT  /photos/:id(.:format)        photos#update 
             DELETE /photos/:id(.:format)        photos#destroy 
           pages GET  /pages(.:format)         pages#index 
             POST  /pages(.:format)         pages#create 
          new_page GET  /pages/new(.:format)        pages#new 
          edit_page GET  /pages/:id/edit(.:format)       pages#edit 
           page GET  /pages/:id(.:format)        pages#show 
             PATCH /pages/:id(.:format)        pages#update 
             PUT  /pages/:id(.:format)        pages#update 
             DELETE /pages/:id(.:format)        pages#destroy 
autocomplete_user_country_suggestions GET  /suggestions/autocomplete_user_country(.:format) suggestions#autocomplete_user_country 
    autocomplete_user_city_suggestions GET  /suggestions/autocomplete_user_city(.:format)  suggestions#autocomplete_user_city 
autocomplete_user_school_suggestions GET  /suggestions/autocomplete_user_school(.:format) suggestions#autocomplete_user_school 
    autocomplete_user_major_suggestions GET  /suggestions/autocomplete_user_major(.:format)  suggestions#autocomplete_user_major 
          suggestions GET  /suggestions(.:format)        suggestions#index 
             POST  /suggestions(.:format)        suggestions#create 
         new_suggestion GET  /suggestions/new(.:format)       suggestions#new 
         edit_suggestion GET  /suggestions/:id/edit(.:format)     suggestions#edit 
          suggestion GET  /suggestions/:id(.:format)       suggestions#show 
             PATCH /suggestions/:id(.:format)       suggestions#update 
             PUT  /suggestions/:id(.:format)       suggestions#update 
             DELETE /suggestions/:id(.:format)       suggestions#destroy 
       conversation_messages GET  /conversations/:conversation_id/messages(.:format) messages#index 
             POST  /conversations/:conversation_id/messages(.:format) messages#create 
         conversations GET  /conversations(.:format)       conversations#index 
             POST  /conversations(.:format)       conversations#create 
         post_comments GET  /posts/:post_id/comments(.:format)     comments#index 
             POST  /posts/:post_id/comments(.:format)     comments#create 
        new_post_comment GET  /posts/:post_id/comments/new(.:format)    comments#new 
        edit_post_comment GET  /posts/:post_id/comments/:id/edit(.:format)  comments#edit 
         post_comment GET  /posts/:post_id/comments/:id(.:format)    comments#show 
             PATCH /posts/:post_id/comments/:id(.:format)    comments#update 
             PUT  /posts/:post_id/comments/:id(.:format)    comments#update 
             DELETE /posts/:post_id/comments/:id(.:format)    comments#destroy 
          post_places GET  /posts/:post_id/places(.:format)     places#index 
             POST  /posts/:post_id/places(.:format)     places#create 
         new_post_place GET  /posts/:post_id/places/new(.:format)    places#new 
         edit_post_place GET  /posts/:post_id/places/:id/edit(.:format)   places#edit 
          post_place GET  /posts/:post_id/places/:id(.:format)    places#show 
             PATCH /posts/:post_id/places/:id(.:format)    places#update 
             PUT  /posts/:post_id/places/:id(.:format)    places#update 
             DELETE /posts/:post_id/places/:id(.:format)    places#destroy 
           posts GET  /posts(.:format)         posts#index 
             POST  /posts(.:format)         posts#create 
          new_post GET  /posts/new(.:format)        posts#new 
          edit_post GET  /posts/:id/edit(.:format)       posts#edit 
           post GET  /posts/:id(.:format)        posts#show 
             PATCH /posts/:id(.:format)        posts#update 
             PUT  /posts/:id(.:format)        posts#update 
             DELETE /posts/:id(.:format)        posts#destroy 
+0

「レーキルート」の出力を表示できますか? –

+0

haha​​ rake routesは私がそれらを掲示するととても良く見えません。より良い方法があるのか​​、リビジョンのルートだけを表示するのでしょうか? –

答えて

2

あなたは:reviserルートの特異リソース定義を持っています。これはあなたがしようとしていることには意味がありますが、form_for [current_user, @reviser]によって生成されたルートは:user_id:idの両方を持つルートを生成しようとします。

resource :reviser:idを作成しないため、:idフィールドはあなたのケースでは定義されていません。

ソリューションは、明示的にルートあなたたいあなたform_forにURLを設定することです:あなたのコードとの大きな問題は、あなたのことです

form_for @reviser, url: edit_user_reviser_path(user_id: current_user.id) 

:それはの線に沿って何かする必要があります

あなたのコントローラのidによって改訂者を直接参照しています。現在のルート設定では、Reviser.find()メソッドでエラーが返されます。しかし、もっと重要なのは、悪意のあるユーザーがPATCH要求にidパラメータを送信して、別のユーザーに属するレコードを乗っ取りして更新する可能性があるためです。

単純な解決策は、リビジョンオブジェクトをcurrent_userで直接参照することです。

def set_reviser 
    @reviser = current_user.reviser 
end 
+0

よろしく!ヒントありがとう!私はどんな悪意のあるユーザーも欲しくない! –

関連する問題