2015-10-11 147 views
0

我試圖用Rails 4做一個應用程序。Rails - 鏈接到父協會

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

的關聯是:

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

我的項目展示頁面,我有一個鏈接到一個表格,學生可以表達參加一個項目的興趣。

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

然後在新的頁面(嵌套表單),我有一個後面的鏈接,應該回到項目本身。腳手架結構可以追溯到所有項目學生eois的索引。我正在嘗試改變這一點,以便路徑返回到項目中。

我試過所有這些變化,我沒有得到任何地方。我一般都在與協會鬥爭。我的項目學生eoi表有一個名爲project_id的屬性。我想用它來匹配正在用來發送鏈接到新的興趣表達式的項目。

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

和一些其他的變化 - 你是怎麼做到這一點?

+0

可以共享航線,好嗎? – akbarbin

回答

0

你需要通過 「PROJECT_ID」 參數 「加入此項目」

應用程序/視圖/項目/ 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 %> 

並確保PROJECT_ID屬性設置在 「新」 行動

應用程序/控制器/項目/ 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) %>