Rails 链接到路径
[英] Rails link_to path
本文介绍了Rails 链接到路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Rails 4 中制作一个应用.
我有 3 个模型:Project、Project_Question、Project_Answer
关联是:
项目:
has_many :project_questions
并接受项目问题的嵌套属性.
项目问题:
belongs_to :projecthas_one :project_answer
并接受 Project Answers 的嵌套属性.
我的路由嵌套如下:
资源:项目做资源:project_questions 做资源:project_answers结尾结尾
在我的项目问题部分,我想要一个链接来回答问题.我试过了:
<%= link_to '回答这个问题', new_project_project_question_project_answer_path(:project_question_id => project_question.id) %>
我的路由文件中有一个具有该名称的路由,但我收到此错误消息:
未定义的局部变量或方法`project_question' for #<#:0x0000010f810b68>
括号里应该是什么?
查看:
<div class="row"><div class="col-md-10 col-md-offset-1"><% @project.project_questions.each 做 |singleQuestion|%><div class="categorytitle"><%= singleQuestion.title %>
<div class="generaltext"><%= singleQuestion.try(:content) %>
<span class="editproject"><% if current_user.id == @project.creator_id %><%= link_to '回答这个问题', new_project_project_questions_project_answer_path(:project_question_id => project_question.id) %><%结束%></span><%结束%>
项目问题控制器:
class ProjectQuestionsController <应用控制器before_action :set_project_question, 仅: [:show, :edit, :update, :destroy]# GET/project_questions# GET/project_questions.json定义索引@project_questions = ProjectQuestion.all结尾# 获取/project_questions/1# 获取/project_questions/1.json高清秀结尾# GET/project_questions/new定义新@project_question = ProjectQuestion.new@project = Project.find(params[:project_id])# @project_id = params[:project_id]@project_question.project_answers[0] = ProjectAnswer.new结尾# GET/project_questions/1/edit定义编辑结尾# POST/project_questions# POST/project_questions.json定义创建@project_question = ProjectQuestion.new(project_question_params)@project_question.project_id = project_question_params[:project_id]response_to do |格式|如果@project_question.saveformat.html { redirect_to project_url(Project.find(project_question_params[:project_id])),注意:'项目问题已成功创建.'}format.json { 渲染动作:'show',状态::已创建,位置:@project_question }别的format.html { 渲染动作:'新' }format.json { 渲染 json: @project_question.errors, 状态: :unprocessable_entity }结尾结尾结尾# PATCH/PUT/project_questions/1# PATCH/PUT/project_questions/1.json定义更新response_to do |格式|如果@project_question.update(project_question_params)format.html { redirect_to @project_question,注意:'项目问题已成功更新.'}format.json { 头:no_content }别的format.html { 渲染动作:'编辑' }format.json { 渲染 json: @project_question.errors, 状态: :unprocessable_entity }结尾结尾结尾# 删除/project_questions/1# 删除/project_questions/1.json销毁@project_question.destroyresponse_to do |格式|format.html { redirect_to project_questions_url }format.json { 头:no_content }结尾结尾私人的# 使用回调在动作之间共享公共设置或约束.def set_project_question@project_question = ProjectQuestion.find(params[:id])结尾# 永远不要相信来自可怕互联网的参数,只允许白名单通过.def project_question_paramsparams[:project_question].permit(:id,:title,:content,:project_id,:user_id,project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id])结尾结尾
解决方案
当你运行 rake routes
时,你会发现这个
new_project_project_question_project_answer GET/projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format) project_answers#new
这意味着它需要 :project_id
和 :project_question_id
作为键.
这应该有效
<%= link_to '回答这个问题', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %>
注意new_project_project_question_project_answer_path
不是new_project_project_questions_project_answer_path
I am trying to make an app in Rails 4.
I have 3 models: Project, Project_Question, Project_Answer
The associations are:
Project:
has_many :project_questions
and accepts nested attributes for project questions.
Project Question:
belongs_to :project
has_one :project_answer
and accepts nested attributes for Project Answers.
My routes are nested as follows:
resources :projects do
resources :project_questions do
resources :project_answers
end
end
In my Project Questions partial, I want a link to answer the question. I've tried:
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_question_id => project_question.id) %>
I have a route with that name in my routes file, but I'm getting this error message:
undefined local variable or method `project_question' for #<#<Class:0x0000010742b9d8>:0x0000010f810b68>
What should go in the brackets?
View:
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<% @project.project_questions.each do |singleQuestion| %>
<div class="categorytitle">
<%= singleQuestion.title %>
</div>
<div class="generaltext">
<%= singleQuestion.try(:content) %>
</div>
<span class="editproject">
<% if current_user.id == @project.creator_id %>
<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_question_id => project_question.id) %>
<% end %>
</span>
<% end %>
</div>
</div>
</div>
Project Question controller:
class ProjectQuestionsController < ApplicationController
before_action :set_project_question, only: [:show, :edit, :update, :destroy]
# GET /project_questions
# GET /project_questions.json
def index
@project_questions = ProjectQuestion.all
end
# GET /project_questions/1
# GET /project_questions/1.json
def show
end
# GET /project_questions/new
def new
@project_question = ProjectQuestion.new
@project = Project.find(params[:project_id])
# @project_id = params[:project_id]
@project_question.project_answers[0] = ProjectAnswer.new
end
# GET /project_questions/1/edit
def edit
end
# POST /project_questions
# POST /project_questions.json
def create
@project_question = ProjectQuestion.new(project_question_params)
@project_question.project_id = project_question_params[:project_id]
respond_to do |format|
if @project_question.save
format.html { redirect_to project_url(Project.find(project_question_params[:project_id])), notice: 'Project question was successfully created.' }
format.json { render action: 'show', status: :created, location: @project_question }
else
format.html { render action: 'new' }
format.json { render json: @project_question.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /project_questions/1
# PATCH/PUT /project_questions/1.json
def update
respond_to do |format|
if @project_question.update(project_question_params)
format.html { redirect_to @project_question, notice: 'Project question was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @project_question.errors, status: :unprocessable_entity }
end
end
end
# DELETE /project_questions/1
# DELETE /project_questions/1.json
def destroy
@project_question.destroy
respond_to do |format|
format.html { redirect_to project_questions_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_question
@project_question = ProjectQuestion.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_question_params
params[:project_question].permit(:id, :title, :content, :project_id, :user_id,
project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id]
)
end
end
解决方案
When you run rake routes
, you will find this one
new_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format) project_answers#new
That means it requires :project_id
and :project_question_id
as keys.
This should work
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %>
Notice new_project_project_question_project_answer_path
not new_project_project_questions_project_answer_path
这篇关于Rails 链接到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!