1

Ruby on Rails 3 でアプリケーションを開発していますが、入れ子になったリソースのパスを生成する_ _path 関数に問題があります。

関数 edit_praise_support_path() は、賞賛とサポートをメンバー オブジェクトとして (@ を使用して) 送信すると機能しますが、サポート変数が each/do ループ内の反復子である場合は機能せず、ルーティング エラーがスローされます。

{:action="edit", :controller="supports", :praise_id=>#} に一致するルートはありません

**MODEL classes**
class Praise < ActiveRecord::Base
  self.table_name = 'praise'
  self.primary_key = 'sid'
  belongs_to :user,  :foreign_key => 'creatorSid'
  has_many :praise_supports,  :foreign_key => :praiseSid
end

class PraiseSupport < ActiveRecord::Base
  self.table_name = 'praise_support'
  self.primary_key = 'sid'
  belongs_to :praise
end

賞賛_コントローラー.rb

class SupportsController < ApplicationController
  before_filter :load_praise

  # GET /praises
  # GET /praises.json
  def index
    @praise_supports = @praise.praise_supports.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @praise_supports }
    end
  end

  # GET /praises/1
  # GET /praises/1.json
  def show
    @praise_support = PraiseSupport.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @praise_support }
    end
  end

  # GET /praises/new
  # GET /praises/new.json
  def new
    @praise_support = @praise.praise_supports.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @praise_support }
    end
  end

  # GET /praises/1/edit
  def edit
    @praise_support = PraiseSupport.find(params[:id])
  end

  def create
    @praise_support = @praise.praise_supports.new(params[:praise_support])

    respond_to do |format|
      if @praise_support.save
        format.html { redirect_to @praise, notice: 'Praise Support was successfully created.' }
        format.json { render json: @praise_support, status: :created, location: @praise_support }
      else
        format.html { render action: "new" }
        format.json { render json: @praise_support.errors, status: :unprocessable_entity }
      end
    end

  end

  # PUT /praises/1
  # PUT /praises/1.json
  def update
    @praise_support = PraiseSupport.find(params[:id])

    respond_to do |format|
      if @praise.update_attributes(params[:praise_support])
        format.html { redirect_to @praise_support, notice: 'PraiseSupport was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @praise_support.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @praise_support = @praise.praise_supports.find(params[:id])
    @praise_support.destroy

    respond_to do |format|
      format.html { redirect_to praises_support_url }
      format.json { head :no_content }
    end
  end

  private
    def load_praise
      @praise = Praise.find(params[:praise_id])
    end
end

賞賛_コントローラー.rb によって使用されるビュー ファイル

**VIEW files**
#show.html.erb, THIS WORKS FINE
<%= link_to 'Edit', **edit_praise_support_path(@praise, @praise_support)** %> |
<%= link_to 'Back', praise_supports_path %>


#index.thml.erb, THIS DOES NOT WORK!!
<% @praise_supports.each do |praise_support| %>
  <tr>
    <td><%= praise_support.sid %></td>
    <td><%= praise_support.body %></td>
    <td><%= link_to 'Edit', **edit_praise_support_path(@praise, praise_support)** %></td>
    <td><%= link_to 'Destroy', praise_support, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

ルート定義

  get "supports/create"

  get "praise_support/create"

  resources :praises do
    resources :supports
  end

  get "home/index"

  resources :users
  resources :sessions, :only => [:new, :create, :destroy]


  root :to => 'home#index'


  match ':controller(/:action(/:id))(.:format)'

  match '/betasignup' => 'users#betasignup', :as => 'betasignup'
  match '/signin' => 'sessions#create', :as => 'signin'
  match '/signout' => 'sessions#destroy', :as => 'signout' , via: :delete

レーキルートの結果

    supports_create GET    /supports/create(.:format)                      supports#create
praise_support_create GET    /praise_support/create(.:format)                praise_support#create
      praise_supports GET    /praises/:praise_id/supports(.:format)          supports#index
                      POST   /praises/:praise_id/supports(.:format)          supports#create
   new_praise_support GET    /praises/:praise_id/supports/new(.:format)      supports#new
  edit_praise_support GET    /praises/:praise_id/supports/:id/edit(.:format) supports#edit
       praise_support GET    /praises/:praise_id/supports/:id(.:format)      supports#show
                      PUT    /praises/:praise_id/supports/:id(.:format)      supports#update
                      DELETE /praises/:praise_id/supports/:id(.:format)      supports#destroy
              praises GET    /praises(.:format)                              praises#index
                      POST   /praises(.:format)                              praises#create
           new_praise GET    /praises/new(.:format)                          praises#new
          edit_praise GET    /praises/:id/edit(.:format)                     praises#edit
               praise GET    /praises/:id(.:format)                          praises#show
                      PUT    /praises/:id(.:format)                          praises#update
                      DELETE /praises/:id(.:format)                          praises#destroy
           home_index GET    /home/index(.:format)                           home#index
                users GET    /users(.:format)                                users#index
                      POST   /users(.:format)                                users#create
             new_user GET    /users/new(.:format)                            users#new
            edit_user GET    /users/:id/edit(.:format)                       users#edit
                 user GET    /users/:id(.:format)                            users#show
                      PUT    /users/:id(.:format)                            users#update
                      DELETE /users/:id(.:format)                            users#destroy
             sessions POST   /sessions(.:format)                             sessions#create
          new_session GET    /sessions/new(.:format)                         sessions#new
              session DELETE /sessions/:id(.:format)                         sessions#destroy
                 root        /                                               home#index
                             /:controller(/:action(/:id))(.:format)          :controller#:action
           betasignup        /betasignup(.:format)                           users#betasignup
               signin        /signin(.:format)                               sessions#create
              signout DELETE /signout(.:format)                              sessions#destroy

これも機能しません(賞賛_コントローラーから呼び出されるビュー)

<% @praises.each do |praise| 
    praise_support = praise.praise_supports.new
    %>
    <%= form_for(praise_support, :html => {:id => 'PraiseSupportForm', :class => 'form-inline'}) do |f|  %>
    <div class="label-text">More proof</div> <%= text_area_tag  :support, nil, :id => 'support', :class=>'input-xlarge', :rows=>'1' %>
    <% end %>
<% end %>

#ERROR THROWN:
#No route matches {:controller=>"supports", :format=>nil}
4

0 に答える 0