Ruby on rails Rails-路由错误-从另一个模型调用模型

Ruby on rails Rails-路由错误-从另一个模型调用模型,ruby-on-rails,routing,controller,Ruby On Rails,Routing,Controller,问题:我的目标是在用户页面下列出项目,并在此用户页面上列出的每个项目下呈现注释框。但是,当我尝试呈现注释框表单时,会出现路由错误。我知道这是因为它无法提取项目的ID。我的猜测与控制器有关,但还没有弄清楚。有人知道我如何解决这个问题吗? Routing Error No route matches {:controller=>"comments", :format=>nil, :project_id=>#<Project id: nil...>} project.r

问题:我的目标是在用户页面下列出项目,并在此用户页面上列出的每个项目下呈现注释框。但是,当我尝试呈现注释框表单时,会出现路由错误。我知道这是因为它无法提取项目的ID。我的猜测与控制器有关,但还没有弄清楚。有人知道我如何解决这个问题吗?

Routing Error
No route matches {:controller=>"comments", :format=>nil, :project_id=>#<Project id: nil...>}
project.rb

has_many :projects
has_many :comments
belongs_to :user
belongs_to :project
resources :users do
  resources :projects do
    resources :comments
  end
end

resources :projects do
  resources :comments 
end

resources: comments
<%= render @projects %>
<%= project.content %>
<%= render 'comments/form' %>
<%= form_for([@project, @project.comments.build]) do |f| %>
    <div class="field">
    <%= f.text_area :content, :class => "span12", :rows => "3" %>
    </div>

    <%= f.hidden_field :user_id, :value => current_user.id %>

    <div class="actions">
    <%= f.submit "Add Comment", :class => "btn btn-header" %>
    </div>
<% end %>
def create
  @project = Project.find(params[:project_id])
  @comment = @project.comments.create!(params[:comment])

  if @comment.save
    redirect_to projects_user_path(@project.user)
  end   
end
<%= project.content %>
<%= render 'comments/form', project: project %>
<%= form_for([project, project.comments.build]) do |f| %>
...
comment.rb

has_many :projects
has_many :comments
belongs_to :user
belongs_to :project
resources :users do
  resources :projects do
    resources :comments
  end
end

resources :projects do
  resources :comments 
end

resources: comments
<%= render @projects %>
<%= project.content %>
<%= render 'comments/form' %>
<%= form_for([@project, @project.comments.build]) do |f| %>
    <div class="field">
    <%= f.text_area :content, :class => "span12", :rows => "3" %>
    </div>

    <%= f.hidden_field :user_id, :value => current_user.id %>

    <div class="actions">
    <%= f.submit "Add Comment", :class => "btn btn-header" %>
    </div>
<% end %>
def create
  @project = Project.find(params[:project_id])
  @comment = @project.comments.create!(params[:comment])

  if @comment.save
    redirect_to projects_user_path(@project.user)
  end   
end
<%= project.content %>
<%= render 'comments/form', project: project %>
<%= form_for([project, project.comments.build]) do |f| %>
...
routes.rb

has_many :projects
has_many :comments
belongs_to :user
belongs_to :project
resources :users do
  resources :projects do
    resources :comments
  end
end

resources :projects do
  resources :comments 
end

resources: comments
<%= render @projects %>
<%= project.content %>
<%= render 'comments/form' %>
<%= form_for([@project, @project.comments.build]) do |f| %>
    <div class="field">
    <%= f.text_area :content, :class => "span12", :rows => "3" %>
    </div>

    <%= f.hidden_field :user_id, :value => current_user.id %>

    <div class="actions">
    <%= f.submit "Add Comment", :class => "btn btn-header" %>
    </div>
<% end %>
def create
  @project = Project.find(params[:project_id])
  @comment = @project.comments.create!(params[:comment])

  if @comment.save
    redirect_to projects_user_path(@project.user)
  end   
end
<%= project.content %>
<%= render 'comments/form', project: project %>
<%= form_for([project, project.comments.build]) do |f| %>
...
view/users/projects.html.erb

has_many :projects
has_many :comments
belongs_to :user
belongs_to :project
resources :users do
  resources :projects do
    resources :comments
  end
end

resources :projects do
  resources :comments 
end

resources: comments
<%= render @projects %>
<%= project.content %>
<%= render 'comments/form' %>
<%= form_for([@project, @project.comments.build]) do |f| %>
    <div class="field">
    <%= f.text_area :content, :class => "span12", :rows => "3" %>
    </div>

    <%= f.hidden_field :user_id, :value => current_user.id %>

    <div class="actions">
    <%= f.submit "Add Comment", :class => "btn btn-header" %>
    </div>
<% end %>
def create
  @project = Project.find(params[:project_id])
  @comment = @project.comments.create!(params[:comment])

  if @comment.save
    redirect_to projects_user_path(@project.user)
  end   
end
<%= project.content %>
<%= render 'comments/form', project: project %>
<%= form_for([project, project.comments.build]) do |f| %>
...
上面的重定向错误

NoMethodError in CommentsController#create
undefined method `user'
试试这个:

查看/projects/_project.html.erb

has_many :projects
has_many :comments
belongs_to :user
belongs_to :project
resources :users do
  resources :projects do
    resources :comments
  end
end

resources :projects do
  resources :comments 
end

resources: comments
<%= render @projects %>
<%= project.content %>
<%= render 'comments/form' %>
<%= form_for([@project, @project.comments.build]) do |f| %>
    <div class="field">
    <%= f.text_area :content, :class => "span12", :rows => "3" %>
    </div>

    <%= f.hidden_field :user_id, :value => current_user.id %>

    <div class="actions">
    <%= f.submit "Add Comment", :class => "btn btn-header" %>
    </div>
<% end %>
def create
  @project = Project.find(params[:project_id])
  @comment = @project.comments.create!(params[:comment])

  if @comment.save
    redirect_to projects_user_path(@project.user)
  end   
end
<%= project.content %>
<%= render 'comments/form', project: project %>
<%= form_for([project, project.comments.build]) do |f| %>
...

查看/comments/_form.html.erb

has_many :projects
has_many :comments
belongs_to :user
belongs_to :project
resources :users do
  resources :projects do
    resources :comments
  end
end

resources :projects do
  resources :comments 
end

resources: comments
<%= render @projects %>
<%= project.content %>
<%= render 'comments/form' %>
<%= form_for([@project, @project.comments.build]) do |f| %>
    <div class="field">
    <%= f.text_area :content, :class => "span12", :rows => "3" %>
    </div>

    <%= f.hidden_field :user_id, :value => current_user.id %>

    <div class="actions">
    <%= f.submit "Add Comment", :class => "btn btn-header" %>
    </div>
<% end %>
def create
  @project = Project.find(params[:project_id])
  @comment = @project.comments.create!(params[:comment])

  if @comment.save
    redirect_to projects_user_path(@project.user)
  end   
end
<%= project.content %>
<%= render 'comments/form', project: project %>
<%= form_for([project, project.comments.build]) do |f| %>
...

...
你必须通过该项目才能形成分部


希望这有帮助

您的关联中存在错误。是
属于\u:用户
属于\u项目
的单数形式。谢谢。固定的。路由错误仍然存在。好的,现在它会发布到我的数据库,但是当我尝试在comments controller中将重定向设置为@comment.project时,我得到了一个NoMethodError。既然评论属于项目,为什么我不能将此重定向到?我编辑了上面的问题,显示:“重定向到项目用户路径(@project.user)”已添加到上面问题的底部。错误似乎出现在
user.find(params[:user\u id])
中。检查服务器日志中的参数。这是因为
params[:project\u id]
为nil,
@project
为nil,
user
不是nil类的方法。