“找不到<型号>”没有身份证”路由/会话问题

发布于 2024-12-18 03:43:00 字数 3187 浏览 3 评论 0原文

我在这里上下搜索了我的问题,但还没有找到正确的答案。我认为这可能是一个简单的路线问题。在我的应用程序上,用户可以创建一个项目,并在正式注册之前通过他们的项目关注“植物”。更新和编辑项目本身的路线工作正常。仅当我尝试访问“Prelationships”控制器中的 CREATE 操作以在“项目”和“植物”之间建立关系时,才会出现“无法找到没有 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                

我的“Projects”模型

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"}  

如果这是一个路由问题,我似乎找不到合适的语言来解决它。我已经构建了一个能够关注其他用户的用户模型,并且没有这些问题,这让我相信我可能必须为我的项目创建一个会话?

请帮助这个新手!

I've searched my problem high and low on here and haven't quite found the right answer. I think it might be a simple routes issue. On my app, a user can create a project and follow "plants" through their project before ever having to officially sign up. The routes for updating and editing the project itself work fine. The error I get "couldn't find Project without an ID" only occurs after I try accessing the CREATE action in my "Prelationships" controller to build a relationship between the "Project" and the "Plants." My code is below:

My 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       

My "Prelationships" controller

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

My "Prelationships" model

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                

My "Projects" model

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    

My "Plants" model

class Plant < ActiveRecord::Base      

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

My partial for building the relationship on the @project 'show' page

<%= 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 %>

The error message is "Could not find Project without an ID" once I hit submit on the partial.

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

and the Parameters:

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

If this is a routes issue, I can't seem to find the right language to fix it. I've built a User model with the ability to follow other users and did not have these problems, which leads me to believe I might have to create a session for my project?

Help this newbie, please!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-12-25 03:43:00

简单的方法:

<%= 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 %>

正确的方法是在项目上创建嵌套路由,但您可以将此重构作为稍后的练习。

The simple way:

<%= 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 %>

And the right way would be creating a nested route on projects, but you can do this refactoring as an exercise for later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文