“找不到没有ID的<model>路由/会话问题</model>

时间:2011-11-25 10:34:34

标签: ruby-on-rails rails-activerecord

我在这里搜索了我的问题的高低,并没有找到正确的答案。我认为这可能是一个简单的路线问题。在我的应用程序中,用户可以创建项目并在必须正式注册之前通过他们的项目跟踪“植物”。更新和编辑项目本身的路线运行良好。我尝试访问“Prelationships”控制器中的CREATE操作以构建“Project”和“Plants”之间的关系后,我发现“找不到没有ID的Project”的错误。我的代码如下:

我的routes.rb

resources :sessions,       :only => [:new, :create, :destroy]
resources :microposts,     :only => [:create, :destroy]
resources :relationships,  :only => [:create, :destroy]  
resources :prelationships, :only => [:create]
resources :plants   
resources :projects do        
  member do
   get :pfollowing, :pfollowers  
  end
end       

我的“Prelationships”控制器

class PrelationshipsController < ApplicationController

  def create   
    @project = Project.find(params[:project_id])                                   
    @plant = Plant.find(params[:prelationship][:pfollowed_id])
    @project.pfollow!(@plant)
    respond_to do |format|
      format.html { redirect_to @project }
      format.js { redirect_to @project }
    end
  end
end

我的“Prelationships”模式

class Prelationship < ActiveRecord::Base                          
attr_accessible :pfollowed_id

belongs_to :pfollower, :class_name => "Project"
belongs_to :pfollowed, :class_name => "Plant"

validates :pfollower_id, :presence => true
validates :pfollowed_id, :presence => true
end                

我的“项目”模型

class Project < ActiveRecord::Base                    
attr_accessible :title, :address, :latitude, :longitude, :state      

belongs_to :user        
has_many :prelationships, :foreign_key => "pfollower_id",
                        :dependent => :destroy   
has_many :pfollowing, :through => :prelationships, :source => :pfollowed          

  def pfollowing?(pfollowed)
    prelationships.find_by_pfollowed_id(pfollowed)  
  end  

  def pfollow!(pfollowed)
    prelationships.create!(:pfollowed_id => pfollowed.id)
  end
end    

我的“植物”模型

class Plant < ActiveRecord::Base      

has_many :prelationships, :foreign_key => "pfollowed_id",
                        :class_name => "Prelationship"
has_many :pfollowers, :through => :reverse_prelationships, 
                    :source => :pfollower   
end

我在@project'show'页面

上建立关系的部分原因
<%= form_for @project.prelationships.build(:pfollowed_id =>
                                 @project_id) do |f| %>
<%= collection_select(:prelationship, :pfollower_id, Plant.all, :id, :name, options =   
{:prompt => "Select your plants"}, :class => "listselect") %>
<div class="actions">
<%= f.submit "Pfollow" %>
 </div>
<% end %>

在我点击部分提交后,错误消息是“找不到没有ID的项目”。

app/controllers/prelationships_controller.rb:4:in `create'

和参数:

{"utf8"=>"✓",
"authenticity_token"=>"NKqa1f0M2yPLQDHbRLnxl3SiwBeTus/1q1hpZjD7hgY=",
"prelationship"=>{"pfollower_id"=>"5"},"commit"=>"Pfollow"}  

如果这是路线问题,我似乎找不到合适的语言来修复它。我已经建立了一个能够跟随其他用户并且没有这些问题的用户模型,这让我相信我可能需要为我的项目创建一个会话?

请帮助这个新手!

1 个答案:

答案 0 :(得分:2)

简单的方法:

<%= form_for @project.prelationships.build(:pfollowed_id =>
                             @project_id) do |f| %>
    <%= collection_select(:prelationship, :pfollower_id, Plant.all, :id, :name, options =   
    {:prompt => "Select your plants"}, :class => "listselect") %>
    <%= hidden_field_tag :project_id, @project.id %>
    <div class="actions">
        <%= f.submit "Pfollow" %>
    </div>
<% end %>

正确的方法是在项目上创建一个嵌套路径,但是你可以将这个重构作为练习以供日后使用。