1
我在这里搜索了我的问题高和低,并没有完全找到正确的答案。我认为这可能是一个简单的路线问题。在我的应用程序中,用户可以创建一个项目,并在正式注册之前通过他们的项目关注“工厂”。更新和编辑项目本身的路线工作正常。在我尝试访问我的“Prelationships”控制器中的CREATE操作以建立“项目”和“植物”之间的关系后,我得到的错误“找不到没有ID的项目”。我的代码如下:“找不到<model>没有ID”路线/会话问题
我的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“秀”的关系页面
<%= 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"}
如果这是一个路线问题,我似乎无法找到合适的语言来解决它。我已经建立了一个能够跟随其他用户并且没有这些问题的用户模型,这使我相信我可能需要为我的项目创建一个会话?
请帮助这个新手,请!
太棒了!谢谢。我曾与hidden_field_tags混淆,但这个概念已经躲过了我。 –