0
什麼是最好的(最簡單的)方式來瀏覽MVC並檢查一切是否設置正確?排除rails應用程序故障的最佳步驟
我有點疲憊,我覺得必須有一個真正簡單的修復錯誤這樣的消息:
undefined method `invitations_path' for #<#<Class:0x00000105ad5cb8>:0x00000105820b30>
加入少量的代碼,以我的應用程序後的東西打破,我想麻煩拍他們自己。
感謝您的提示!
編輯
也許排查具體問題會導致的方式廣義的做法,
-
LINK_TO不連接使用<%=%>而不是<%%>。 - visting本地主機時生成上述錯誤:3000 /邀請/新
視圖(用在家庭/ index.erb.html)
<% if @user.invitation_limit > 0 %>
<% link_to 'Send Invitations', new_invitation_path %>
(<%= @user.invitation_limit %> left)
<% end %>
視圖(在邀請/ new.erb.html)
<%= error_messages_for :invitation %>
<% form_for @invitation do |f| %>
<p>
<%= f.label :recipient_email, "Friend's email address" %><br />
<%= f.text_field :recipient_email %>
</p>
<p><%= f.submit "Invite!" %></p>
<% end %>
控制器
class InvitationController < ApplicationController
def new
@invitation = Invitation.new
end
def create
@invitation = Invitation.new(params[:invitation])
@invitation.sender = current_user
if @invitation.save
if logged_in?
Mailer.deliver_invitation(@invitation, signup_url(@invitation.token))
flash[:notice] = "Thank you, invitation sent."
redirect_to projects_url
else
flash[:notice] = "Thank you, we will notify when we are ready."
redirect_to root_url
end
else
render :action => 'new'
end
end
end
模式
class Invitation < ActiveRecord::Base
belongs_to :sender, :class_name => 'User'
has_one :recipient, :class_name => 'User'
attr_accessible :recipient_email, :sender_id, :sent_at, :token
end
的routes.rb
resources :home, :only => :index
resources :invitation
資源不應該是複數,例如':invitations'?在任何情況下,當有路由問題時,「耙路」通常是第一個啓動的地方。 –
好吧,我的管理員的名字是單數(這是非常規的?)和'rake routes'看起來很好:'invitation_index GET /invitation(.:format)invitation#index' 和'POST /invitation(.:format)invitation#create ' – broinjc
資源通常是多元化的,所以是的,這將被認爲是非常規的。看看它是如何'invitations_path'而不是'invitation_path'?這是因爲表單助手試圖爲帖子建立鏈接,至少這是我的第一個猜測。 –