我正在尝试使用Rails 4和Simple Form制作应用程序。
我有3个模型:project,project_question和project_answer。
协会是: Project.rb:
has_many :project_questions, dependent: :destroy#, through: :projects
accepts_nested_attributes_for :project_questions
项目问题:
belongs_to :project#, counter_cache: true
has_one :project_answer, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :project_answer
项目答案:
belongs_to :project_questions#, counter_cache: true
belongs_to :user
路线是:
resources :projects do
resources :project_questions do
resources :project_answers
end
end
在我的项目问题视图中,我尝试显示项目答案。如果项目创建者是当前用户,我想添加一个链接来编辑答案。我在路径上尝试了至少10种变体,但仍然无法弄清楚如何链接到编辑project_answer路径。
项目答案的路线是:
project_project_question_project_answers GET /projects/:project_id/project_questions/:project_question_id/project_answers(.:format) project_answers#index
POST /projects/:project_id/project_questions/:project_question_id/project_answers(.:format) project_answers#create
new_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format) project_answers#new
edit_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/:id/edit(.:format) project_answers#edit
project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#show
PATCH /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#update
PUT /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#update
DELETE /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format) project_answers#destroy
我的project_question部分是:
<div class="containerfluid">
<div class="row">
<div class="col-md-12">
<% @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 && singleQuestion.project_answer.try(:answer).blank? %>
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %>
<% elsif %>
<% if current_user.id == @project.creator_id %>
<div class="datasubtextq">
<%=singleQuestion.project_answer.try(:answer) %>
<span class="editproject">
<%= link_to 'Edit Answer', [@project_answer.project_question, @project_answer] %>
</span>
</div>
<% else %>
<div class="datasubtextq">
<%=singleQuestion.project_answer.try(:answer) %>
</div>
<% end %>
<% end %>
</span>
<% end %>
</div>
</div>
</div>
我想弄清楚的是:
<%= link_to 'Edit Answer', [@project_answer.project_question, @project_answer] %>
我试过这个,因为我在http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/找到了一个教程。虽然该博客中的编辑示例使用单独的文件作为我的等效答案,但我认为可能更改对象的顺序可能会有所帮助。它没有。
我也尝试过:
<%= link_to 'Edit Answer',[@project_answer.singleQuestion, @project_answer] %>
<%= link_to 'Edit Answer', edit_project_project_question_project_answer_path(@project_answer.singleQuestion, @project_answer) %>
<%= link_to 'Edit Answer', edit_project_project_question_project_answer_path(@singleQuestion.project_answer, @project_answer) %>
<%= link_to 'Edit Answer', edit_project_project_question_project_answer_path(@project_answer.project_question, @project_answer) %>
如果项目答案显示在项目问题的视图部分内,如何链接到项目答案的编辑路径?
我的project_answer控制器有:
class ProjectAnswersController < ApplicationController
before_action :set_project
before_action :set_project_question
before_action :set_project_answer, only: [:show, :edit, :update, :destroy]
# GET /project_answers
# GET /project_answers.json
def index
@project_answers = ProjectAnswer.all
end
# GET /project_answers/1
# GET /project_answers/1.json
def show
end
# GET /project_answers/new
def new
@project_answer = @project_question.build_project_answer
end
# GET /project_answers/1/edit
def edit
end
# POST /project_answers
# POST /project_answers.json
def create
@project_answer = @project_question.build_project_answer(project_answer_params)
respond_to do |format|
if @project_answer.save
format.html { redirect_to @project, notice: 'Project answer was successfully created.' }
format.json { render action: 'show', status: :created, location: @project_answer }
else
format.html { render action: 'new' }
format.json { render json: @project_answer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /project_answers/1
# PATCH/PUT /project_answers/1.json
def update
respond_to do |format|
if @project_answer.update(project_answer_params)
format.html { redirect_to @project_answer, notice: 'Project answer was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @project_answer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /project_answers/1
# DELETE /project_answers/1.json
def destroy
@project_answer.destroy
respond_to do |format|
format.html { redirect_to project_answers_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:project_id])
end
def set_project_question
@project_question = @project.project_questions.find(params[:project_question_id])
end
def set_project_answer
@project_answer = ProjectAnswer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_answer_params
params[:project_answer].permit(:answer, :project_question_id, :id)
end
end
我认为点击“编辑回答”链接时显示的网址中有线索。页面本身是空白的。 url只是project / id / project_question / id。我期待项目答案ID也在那一行。
我尝试在网址栏中输入一个包含项目答案的网址,但我收到路由错误(即使答案显示在我的节目页面上)。
我试图在项目控制台中找到答案。我收到的错误是:
p.project_questions.project_answers.find
NoMethodError: undefined method `project_answers' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_ProjectQuestion:0x00000104382e08>
我尝试过没有复数表名的相同搜索。
答案 0 :(得分:1)
:project_id
您必须为<%= link_to 'Edit Answer', edit_project_project_question_project_answer_path(@project, singleQuestion, @project_answer) %>
赋予价值才能使链接正常工作。
这应该有效
def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)
println "Sql Instance: " + sql
sql.execute(
" DROP TABLE IF EXISTS test;"
+ "CREATE TABLE test ("
+ "id SERIAL,"
+ "word TEXT,"
+ "number INTEGER,"
+ "decimal NUMERIC,"
+ "datetime TIMESTAMP"
+ ");"
)
def params = ['Hello, World!', 42, 3.14159, null]
sql.execute("INSERT INTO test (word, number, decimal, datetime)"
+ "VALUES (?,?,?,?);", params)
sql.eachRow("SELECT * FROM test;") { row ->
println "The row Id is: ${row.id}"
// HERE??
+ "The word is: ${row.word}"
+ "The number is: ${row.number}"
+ "The decimal is: ${row.decimal}"
+ "The date-time is: ${row.datetime}"
}
sql.close()
答案 1 :(得分:1)
您尚未在表单中定义@project_answer变量,因此无法使用它。
你应该使用......
<%= link_to 'Edit Answer', edit_project_project_question_project_answer_path(@project, singleQuestion, singleQuestion.project_answer) %>