Rails - 链接到家长协会

Rails - linking to parent associations

我正在尝试用 Rails 4 制作一个应用程序。

我有一个project.rb和一个project_student_eoi.rb。

协会是:

project has many project student eons
project student eois belong to project

在我的项目展示页面上,我有一个 link 表单,学生可以在其中表达对加入项目的兴趣。

<% if can? :read, Project && current_user.profile.has_role?(:student) %>
          <%= link_to  'Join this project', new_project_student_eoi_path %>
        <% end %>

然后在新页面(嵌套表单)上,我有一个返回link,它应该返回到项目本身。脚手架结构可以追溯到所有项目学生 eois 的索引。我正在尝试更改此设置,以便路径返回到项目。

我已经尝试了所有这些变体,但一无所获。我一般都在与协会作斗争。我的项目学生 eoi table 有一个名为 :project_id 的属性。我想用它来匹配用于将 link 发送到新的兴趣表达形式的项目。

  <h1 class="header-project" style="margin-bottom:10%">
    Express your interest in this project
  </h1>


  <%= render 'form' %>


<div class="formminor">
    <%= link_to 'Back', project_path(:project_id => project.id) %>

我也试过:

<%= link_to 'Back', project_path(:project_id => :project.id) %>
<%= link_to 'Back', project_path(project_id: @project.id) %>
<%= link_to 'Back', project_path(project_id: => project.id) %>

和其他几个变体 - 你是怎么做到的?

您需要在 "Join This Project"

上传递 "project_id" 参数

app/views/projects/show.html.erb

<% if can? :read, Project && current_user.profile.has_role?(:student) %>
    <%= link_to  'Join this project', new_project_student_eoi_path(project_id: @project.id) %>
<% end %>

然后确保在 "new" 操作

上设置了 project_id 属性

app/controllers/projects/project_student_eois_controller.rb

class ProjectStudentEoisController < ApplicationController
  def new
    @project_student_eoi = ProjecStudentEoi.new(project_id: params[:project_id])
  end
end

设置project_id后,您可以使用它:

<%= link_to 'Back', project_path(@project_student_eoi.project) %>