Rails-链接到父级关联 [英] Rails - linking to parent associations
问题描述
我正在尝试使用Rails 4制作应用程序.
I'm trying to make an app with Rails 4.
我有一个project.rb和一个project_student_eoi.rb.
I have a project.rb and a project_student_eoi.rb.
关联为:
project has many project student eons
project student eois belong to project
在我的项目展示页面上,我有一个表格链接,学生可以表达对加入项目的兴趣.
On my project show page, I have a link to a form where students can express interest in joining a project.
<% if can? :read, Project && current_user.profile.has_role?(:student) %>
<%= link_to 'Join this project', new_project_student_eoi_path %>
<% end %>
然后在新页面(嵌套表单)上,我有一个反向链接,该链接应返回到项目本身.脚手架的结构可以追溯到所有项目学生的学习成绩.我正在尝试更改此设置,以使路径返回项目.
Then on the new page (which nests the form), I have a back link, which should go back to the project itself. The scaffolding structure goes back to an index of all the project student eois. I'm trying to change this so that the path goes back to the project.
我已经尝试了所有这些变体,但一无所获.我一般都在与协会做斗争.我的专科学生eoi表具有一个名为:project_id的属性.我想用它来匹配用于将链接发送到新的兴趣表达形式的项目.
I've tried all of these variations and am not getting anywhere. I'm struggling with associations generally. My project student eoi table has an attribute called :project_id. I want to use that to match up to the project which is being used to send the link to the new expression of interest form.
<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) %>
和其他几种变体-您该怎么做?
and several other variations - how do you do this?
推荐答案
您需要在加入此项目"上传递"project_id"参数
You need to pass "project_id" param on "Join This Project"
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 %>
,然后确保对新"操作设置了project_id属性
and then make sure project_id attribute is set on "new" action
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,就可以使用它:
once project_id is set, you can use this :
<%= link_to 'Back', project_path(@project_student_eoi.project) %>
这篇关于Rails-链接到父级关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!