(Rails 3.2 新手,App 是 twitter)
我希望我了解路线,但我仍然不了解。我设置了一个系统,用户可以通过手动提交用户名来关注其他用户:
@relationship = User.find_by_username(params[:username])
if @relationship
if current_user.following? @relationship
current_user.unfollow @relationship
else
current_user.follow @relationship
end
else
flash[:notice] = "User with username #{params[:username]} is not found"
end
吐出这个:
undefined method `model_name' for NilClass:Class
在我将 @relationship 更改为 :relationship 之前,这并没有真正起作用,如下所示:
<%= form_for :relationship do |f| %>
<%= f.text_field :username, placheholder: "username" %>
</div>
<br/>
<%= f.submit "Add/Subtract" %>
<% end %>
现在,错误消息(“用户名..”)一直弹出,但是当我单击“添加/减去”时,它变为:
没有路线匹配 [POST] "/buddies"
所有这些都在 User 模型下。
我想有些事情是非常错误的。
编辑:路线看起来像这样..(ribbit = tweets)
Ribbitapp::Application.routes.draw do
resources :ribbits
resources :relationships
resources :sessions
resources :users
get 'logout', to: 'sessions#destroy', as: 'logout'
#get 'buddies', to: 'users#buddies', as: 'buddies'
match 'buddies', to: 'users#buddies', as: 'buddies'
root to: 'users#new'