我正在 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
和 rake 路由结果
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}