Rails Route Error - 新的路由不存在(Rails Route Error - route for new does not exist)
我正在尝试制作rails 4应用程序。
我有一个项目模型和一个project_questions模型。
项目问题属于项目。
我已经制作了一个项目问题表单,用户可以用它来询问有关项目的问题。
我的麻烦是,当我按提交时,出现路线错误,表示没有匹配的路线。 我不明白,因为我通过脚手架制作模型,我理解自动制作主要路线。
我的结构是:
Project_question表单(使用简单形式gem):
<%= simple_form_for :project_questions do |f| %> <%= f.input :project_id, as: :hidden, input_html: {value: @project_question_id} %> <%= f.input :title, label: 'Question:', :label_html => {:class => 'question-title'}, placeholder: 'Type your question here', :input_html => {:style => 'width: 100%', :rows => 4, class: 'response-project'} %> <%= f.input :content, label: 'Is there any context or other information?', :label_html => {:class => 'question-title'}, placeholder: 'Context might help to answer your question', :input_html => {:style => 'width: 100%', :rows => 5, class: 'response-project'} %> <br><br><br> <%= f.button :submit, 'Send!', :class => "cpb" %> <% end %>
路线:
resources :projects do resources :project_questions do resources :project_answers end end
项目问题控制器:
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_question, 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
项目控制器:
class ProjectsController < ApplicationController #layout :projects_student_layout before_action :authenticate_user! # GET /projects # GET /projects.json def index @projects = current_user.projects #can i have more than one index? do i need to change something in the routes? if i want to list the related projects and the expiring projects - how do i do that within one index? #is there a way to order these, so that for educators they are in order of course and for students they are in order of next milestone date? #@projects.order("created_at DESC") end # def index2 # @projects = Project.find_xxx_xx # end def list @projects = Project.find(:all) end def toggle_draft @project = Project.find(params[:id]) @project.draft = true @project.save redirect_to project_path(@project) end # GET /projects/1 # GET /projects/1.json def show #authorise @project @project = Project.find(params[:id]) @creator = User.find(@project.creator_id) @creator_profile = @creator.profile #@approver_profile = User.find(@project.educator_id).profile #educators are the only people who approve projects # if profile == 'studnet' #@approval = @project.approval # @invitations = @project.project_invitations end # GET /projects/new def new #authorise @project @project = Project.new @project.scope = Scope.new @project.scope.datum = Datum.new @project.scope.material = Material.new @project.scope.mentoring = Mentoring.new @project.scope.participant = Participant.new @project.scope.funding = Funding.new @project.scope.ethic = Ethic.new @project.scope.group_research = GroupResearch.new @project.scope.backgroundip = Backgroundip.new @project.scope.result = Result.new @project.scope.finalise = Finalise.new end # GET /projects/1/edit def edit #authorise @project @project =Project.find(params[:id]) end # POST /projects # POST /projects.json def create #authorise @project @project = Project.new(project_params) @project.creator_id = current_user.id @project.users << current_user respond_to do |format| if @project.save format.html { redirect_to @project } format.json { render action: 'show', status: :created, location: @project } else format.html { render action: 'new' } format.json { render json: @project.errors, status: :unprocessable_entity } end end end # PATCH/PUT /projects/1 # PATCH/PUT /projects/1.json def update #authorise @project @project = Project.find(params[:id]) @project.creator_id = current_user.id respond_to do |format| if @project.update(project_params) format.html { redirect_to @project } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @project.errors, status: :unprocessable_entity } end end end # DELETE /projects/1 # DELETE /projects/1.json def destroy #authorise @project @project.destroy respond_to do |format| format.html { redirect_to projects_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[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def project_params params.require(:project).permit( :id, :title, :description, :video_proposal, :link_to_video_proposal, :expiry_date_for_sponsor_interest, :motivation, :approach, :completion_date, :start_date, :industry_id, :recurring_project, :frequency, :date_for_student_invitation, :date_for_student_interest, :closed, :student_objective, :industry_relevance, :hero_image, :project_id, project_question_attributes: [:title, :content, :user_id, :project_id, project_answer_attributes: [:answer, :project_question_id]], scope_attributes: [:id, :project_id, :data, :material, :mentoring, :participant, :funding, :ethic, :group, :result, :disclosure, :finalise, :if_mentoring, :if_participant, :if_funding, :if_ethic, :if_group_research, :if_backgroundip, :if_datum, :if_material, datum_attributes: [:id, :prim_sec, :qual_quant, :survey, :survey_link, :experiment, :other_type, :other_description, :confidential, :data_description, :scope_id], material_attributes: [:id, :mattype, :description, :scope_id], mentoring_attributes: [:id, :frequency, :description, :scope_id], funding_attributes: [:id, :expenses, :honorarium, :financing, :currency, :size, :amount_expenses, :amount_honorarium, :comment, :amount_principal_financing, :return_on_finance, :period_of_return, :expense_description, :amount_expenses_pennies, :amount_honorarium_pennies, :amount_principal_financing_pennies, :amount_expenses_currency, :scope_id], participant_attributes: [:id, :title, :description, :location, :costs, :participation_cost, :eligibility, :eligibility_criteria, :currency, :participation_cost_pennies, :participation_cost_currency, :location_specific ], group_research_attributes: [:id, :number_of_group_members, :scope_id], ethic_attributes: [:id, :obtained, :date_expected, :ethics_comment, :ethics_policy_link, :scope_id], result_attributes: [:id, :report, :standard_licence, :bespoke_licence, :option, :assignment, :other_outcome, :consulting, :link_to_bespoke_licence, :description], disclosure_attributes: [:id, :allusers, :publicity, :limitedorganisation, :limitedindustry, :limiteduser, :approveddisclosure], backgroundip_attributes: [:id, :scope_id, :copyright, :design, :patent, :trademark, :geographical_indication, :trade_secret, :other, :identifier_copyright, :identifier_design, :identifier_patent, :identifier_trademark, :identifier_geographical_indication, :identifier_trade_secret, :identifier_other, :description, :registered_owner, :unregistered_interest, :conditions, :pbr, :identifier_pbr ], finalise_attributes: [:id, :draft, :reminder, :reminder_date, :finalised_at, :scope_id] ] ) end
错误消息说:
No route matches [POST] "/projects/70/project_questions/new"
我不知道在哪里制作路线或如何为新路线制作路线。 我认为这是生成脚手架时自动化过程的一部分。 任何人都可以看到出了什么问题?
我发现SO上的另一个用户有嵌套路由问题。 他们改变了他们的控制器显示动作如下:
def show @user = User.find(params[:user_id]) @album = @user.albums.find(params[:id]) @photo = @album.photos.build end
目前,我在项目问题中的表演行动是空的。 但是,它属于我认为会涵盖它的项目。 是否有一系列步骤可以使嵌套控制器工作?
谢谢
I am trying to make a rails 4 app.
I have a projects model and a project_questions model.
Project questions belongs to projects.
I have made a project questions form which users can use to ask a question about a project.
My trouble is, when I press submit, the route error appears saying there is no matching route. I don't understand that because I made the model through scaffolding which I understood made the main routes automatically.
My structure is:
Project_question form (using simple form gem):
<%= simple_form_for :project_questions do |f| %> <%= f.input :project_id, as: :hidden, input_html: {value: @project_question_id} %> <%= f.input :title, label: 'Question:', :label_html => {:class => 'question-title'}, placeholder: 'Type your question here', :input_html => {:style => 'width: 100%', :rows => 4, class: 'response-project'} %> <%= f.input :content, label: 'Is there any context or other information?', :label_html => {:class => 'question-title'}, placeholder: 'Context might help to answer your question', :input_html => {:style => 'width: 100%', :rows => 5, class: 'response-project'} %> <br><br><br> <%= f.button :submit, 'Send!', :class => "cpb" %> <% end %>
Routes:
resources :projects do resources :project_questions do resources :project_answers end end
Controller for project question:
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_question, 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
Controller for project:
class ProjectsController < ApplicationController #layout :projects_student_layout before_action :authenticate_user! # GET /projects # GET /projects.json def index @projects = current_user.projects #can i have more than one index? do i need to change something in the routes? if i want to list the related projects and the expiring projects - how do i do that within one index? #is there a way to order these, so that for educators they are in order of course and for students they are in order of next milestone date? #@projects.order("created_at DESC") end # def index2 # @projects = Project.find_xxx_xx # end def list @projects = Project.find(:all) end def toggle_draft @project = Project.find(params[:id]) @project.draft = true @project.save redirect_to project_path(@project) end # GET /projects/1 # GET /projects/1.json def show #authorise @project @project = Project.find(params[:id]) @creator = User.find(@project.creator_id) @creator_profile = @creator.profile #@approver_profile = User.find(@project.educator_id).profile #educators are the only people who approve projects # if profile == 'studnet' #@approval = @project.approval # @invitations = @project.project_invitations end # GET /projects/new def new #authorise @project @project = Project.new @project.scope = Scope.new @project.scope.datum = Datum.new @project.scope.material = Material.new @project.scope.mentoring = Mentoring.new @project.scope.participant = Participant.new @project.scope.funding = Funding.new @project.scope.ethic = Ethic.new @project.scope.group_research = GroupResearch.new @project.scope.backgroundip = Backgroundip.new @project.scope.result = Result.new @project.scope.finalise = Finalise.new end # GET /projects/1/edit def edit #authorise @project @project =Project.find(params[:id]) end # POST /projects # POST /projects.json def create #authorise @project @project = Project.new(project_params) @project.creator_id = current_user.id @project.users << current_user respond_to do |format| if @project.save format.html { redirect_to @project } format.json { render action: 'show', status: :created, location: @project } else format.html { render action: 'new' } format.json { render json: @project.errors, status: :unprocessable_entity } end end end # PATCH/PUT /projects/1 # PATCH/PUT /projects/1.json def update #authorise @project @project = Project.find(params[:id]) @project.creator_id = current_user.id respond_to do |format| if @project.update(project_params) format.html { redirect_to @project } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @project.errors, status: :unprocessable_entity } end end end # DELETE /projects/1 # DELETE /projects/1.json def destroy #authorise @project @project.destroy respond_to do |format| format.html { redirect_to projects_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[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def project_params params.require(:project).permit( :id, :title, :description, :video_proposal, :link_to_video_proposal, :expiry_date_for_sponsor_interest, :motivation, :approach, :completion_date, :start_date, :industry_id, :recurring_project, :frequency, :date_for_student_invitation, :date_for_student_interest, :closed, :student_objective, :industry_relevance, :hero_image, :project_id, project_question_attributes: [:title, :content, :user_id, :project_id, project_answer_attributes: [:answer, :project_question_id]], scope_attributes: [:id, :project_id, :data, :material, :mentoring, :participant, :funding, :ethic, :group, :result, :disclosure, :finalise, :if_mentoring, :if_participant, :if_funding, :if_ethic, :if_group_research, :if_backgroundip, :if_datum, :if_material, datum_attributes: [:id, :prim_sec, :qual_quant, :survey, :survey_link, :experiment, :other_type, :other_description, :confidential, :data_description, :scope_id], material_attributes: [:id, :mattype, :description, :scope_id], mentoring_attributes: [:id, :frequency, :description, :scope_id], funding_attributes: [:id, :expenses, :honorarium, :financing, :currency, :size, :amount_expenses, :amount_honorarium, :comment, :amount_principal_financing, :return_on_finance, :period_of_return, :expense_description, :amount_expenses_pennies, :amount_honorarium_pennies, :amount_principal_financing_pennies, :amount_expenses_currency, :scope_id], participant_attributes: [:id, :title, :description, :location, :costs, :participation_cost, :eligibility, :eligibility_criteria, :currency, :participation_cost_pennies, :participation_cost_currency, :location_specific ], group_research_attributes: [:id, :number_of_group_members, :scope_id], ethic_attributes: [:id, :obtained, :date_expected, :ethics_comment, :ethics_policy_link, :scope_id], result_attributes: [:id, :report, :standard_licence, :bespoke_licence, :option, :assignment, :other_outcome, :consulting, :link_to_bespoke_licence, :description], disclosure_attributes: [:id, :allusers, :publicity, :limitedorganisation, :limitedindustry, :limiteduser, :approveddisclosure], backgroundip_attributes: [:id, :scope_id, :copyright, :design, :patent, :trademark, :geographical_indication, :trade_secret, :other, :identifier_copyright, :identifier_design, :identifier_patent, :identifier_trademark, :identifier_geographical_indication, :identifier_trade_secret, :identifier_other, :description, :registered_owner, :unregistered_interest, :conditions, :pbr, :identifier_pbr ], finalise_attributes: [:id, :draft, :reminder, :reminder_date, :finalised_at, :scope_id] ] ) end
The error message says:
No route matches [POST] "/projects/70/project_questions/new"
I don't know where to make a route or how to make a route for new. I thought that was part of the automated process when you generate scaffolding. Can anyone see what has gone wrong?
I found another user on SO who has problems with nested routes. They changed their controller show action as follows:
def show @user = User.find(params[:user_id]) @album = @user.albums.find(params[:id]) @photo = @album.photos.build end
At the moment, my show action in project questions is empty. However, it belongs to projects which I thought would cover it. Is there a series of steps to follow to make a nested controller work?
Thank you
原文:https://stackoverflow.com/questions/30940737
最满意答案
使用
position:absolute
而不是relative,因为z-index不适用于relative。Use
position:absolute
instead of relative as z-index wont work with relative.
相关问答
更多-
这不是坏主意,但我认为这也不是一个好主意。 几周前我曾想过类似的东西。 我想制作CSS,它将通过JS在几个循环中在浏览器中编译,然后附加到head部分。 但经过深思熟虑,我决定不这样做。 我知道你可以制作后备和一些奇特的东西,所以它适用于每个浏览器有或没有js,但我看了我的网格。 它只需不到10KB。 这是一个有趣的想法,但它并不需要花费太多工作。 保持HTML的有序性,你不会遇到太多css类的问题。 您必须让使用“框架”的人在某些断点处定义宽度。 最简单的方法就是写一个类名。 考虑到当你使用例如boots ...
-
CSS Div布局或表格布局(CSS Div layout or table layout)[2022-04-21]
为了方便制作网格 http://www.pagecolumn.com/grid_layout_generator_wrapper_float.htm For easy to make grid http://www.pagecolumn.com/grid_layout_generator_wrapper_float.htm -
CSS布局中的问题(Issue in CSS layout)[2020-10-07]
background也会覆盖background-color 。 请改用background-image 。 用这个: .box { background-image: url('img/mike.png'); background-repeat: no-repeat; background-position: center; } 的jsfiddle background overrides background-color too. use background-image ... -
在CSS中创建2列布局(Creating a 2 Column Layout in CSS)[2022-10-10]
对于每个浮动左边,添加clear:left,对于每个浮动右边,添加clear:right。 它取决于每个浮动容器的顺序。 另一种选择是尝试从一列保留一组浮动(即浮动左侧或浮动权限),并从另一列中的其他列中移除浮动属性,以便它们包裹到浮动框的侧面。 编辑:一个工作示例: http : //cssdesk.com/Xan5j For each float left, add clear:left, for each float right, add clear: right. It'd depend on th ... -
CSS中的表单布局(Form layout in CSS)[2023-03-28]
使每一行包含
-
这种布局可以在CSS中完成吗?(Can this layout be done in CSS?)[2024-04-08]
使用position:absolute而不是relative,因为z-index不适用于relative。 Use position:absolute instead of relative as z-index wont work with relative. -
CSS中的砖块布局(Brick layout in CSS)[2023-08-22]
使用display:flex您可以更容易地创建像墙一样的“砖”,并且您可以使用flex-grow属性来比使用百分比更容易地控制容器宽度。 只是为了踢,这是一个你可以玩的例子。 在任何砖块类型上更改flex-grow值,您也可以创建更随机的模式。 它非常灵活。 使用Flex框,您可以使用align-items和“砖块”上的justify-content ,更轻松地控制文本对齐。 在提供的示例中,我们基本上将所有砖块设置为flex-grow: 2 。 因此,它们都会弯曲到相同的尺寸并在每行中占据相同的空间。 然 ... -
我刚刚使用这个源代码解决了这个问题。 替代文字http://rabu4g.bay.livefilestore.com/y1p4IhbA7NzQWn7G3wY8dkhOEGawswOMPZQ8MXUotkWtx9ppwfcILzwVb3xXKL19d1J-U5d6G7jaftIHjdcwU8_fTFYyJWtaj6t/css.png 更新 你能解释浏览器如何渲染浮动项目吗? PS。 我的主要技巧是左边距和分组div。 无需使用相对和绝对位置就可以解决此问题的单一方法。 I just solved this p ...
-
根据你最近的答案 ,我认为你不需要整个宽度的页脚(只有粘性,虽然你的不是),而且我认为你知道你的版本只有你知道它的高度才有效“foo - 不是那么重要的内容”,因为你需要那个高度来设置侧边栏的顶部坐标。 你的版本下降,当你缩小窗口内容消失了两侧..但基于它背后的想法 - 我已经使用你的逻辑扩展它,并建立在粘性页脚,顶级菜单 - 原始的一切示例链接。 页脚不是全宽, 但是你可以通过在 html元素上放置一个背景图像来使它看起来像是在我的小提琴中有一个简单的虚拟图像但它没有显示出来,无论如何你会使图像的高度相同 ...
-
布局问题 - CSS(Layout Issue - CSS)[2023-11-16]
小提琴 你必须在css中改变它: nav ul ul { visibility: hidden; position: absolute; top: 100%; z-index: 598; max-width: 100%; <-- from width to max-width max-height: 100%; <--- from height to max-height bottom: 0; margin-top: 0; text-t ...