未定义的方法' _path'有参考

时间:2017-03-05 13:07:09

标签: ruby-on-rails-4 reference

我正在寻找带有不同图库链接的绘制课程,每个课程都可以为用户提供上传工作。

我有一个名为" lesson"的控制器,模型和视图。链接的图库是" illst_repot" 我已将illust_repot与引用相关联。 课程:参考和用户:参考

我在索引页面上没有问题,但我在新页面上遇到了问题 未定义的方法`illust_reports_path' 本地主机:3000 /经验/ 1 / illust_reports /新

这是我的路线

lesson_illust_reports GET        /lessons/:lesson_id/illust_reports(.:format)             illust_reports#index
                                              POST       /lessons/:lesson_id/illust_reports(.:format)             illust_reports#create
                     new_lesson_illust_report GET        /lessons/:lesson_id/illust_reports/new(.:format)         illust_reports#new
                    edit_lesson_illust_report GET        /lessons/:lesson_id/illust_reports/:id/edit(.:format)    illust_reports#edit
                         lesson_illust_report GET        /lessons/:lesson_id/illust_reports/:id(.:format)         illust_reports#show
                                              PATCH      /lessons/:lesson_id/illust_reports/:id(.:format)         illust_reports#update
                                              PUT        /lessons/:lesson_id/illust_reports/:id(.:format)         illust_reports#update
                                              DELETE     /lessons/:lesson_id/illust_reports/:id(.:format)         illust_reports#destroy
                                      lessons GET        /lessons(.:format)                                       lessons#index
                                              POST       /lessons(.:format)                                       lessons#create
                                   new_lesson GET        /lessons/new(.:format)                                   lessons#new
                                  edit_lesson GET        /lessons/:id/edit(.:format)                              lessons#edit
                                       lesson GET        /lessons/:id(.:format)                                   lessons#show
                                              PATCH      /lessons/:id(.:format)                                   lessons#update
                                              PUT        /lessons/:id(.:format)                                   lessons#update
                                              DELETE     /lessons/:id(.:format)                                   lessons#destroy

的routes.rb

  resources :lessons do
    resources :illust_reports
  end

illust_reports_controller.rb

class IllustReportsController < ApplicationController
  before_action :set_illust_report, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :illust_report_owner, only: [:edit, :update, :destroy]

  # GET /illust_reports
  # GET /illust_reports.json
  def index
    @illust_reports = IllustReport.all
  end

  def illust_report_owner
    unless @illust_report.user_id == current_user.id
    flash[:notice] = "Accès refusé car tu n'es pas l'auteur de ce billet."
    redirect_to illust_reports_path
    end
  end

  # GET /illust_reports/1
  # GET /illust_reports/1.json
  def show
  end

  # GET /illust_reports/new
  def new
    @illust_report = current_user.illust_reports.build
  end

  # GET /illust_reports/1/edit
  def edit
  end

  # POST /illust_reports
  # POST /illust_reports.json
  def create
    @illust_report = current_user.illust_reports.build(illust_report_params)

    respond_to do |format|
      if @illust_report.save
        format.html { redirect_to @illust_report, notice: 'Illust report was successfully created.' }
        format.json { render :show, status: :created, location: @illust_report }
      else
        format.html { render :new }
        format.json { render json: @illust_report.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /illust_reports/1
  # PATCH/PUT /illust_reports/1.json
  def update
    respond_to do |format|
      if @illust_report.update(illust_report_params)
        format.html { redirect_to @illust_report, notice: 'Illust report was successfully updated.' }
        format.json { render :show, status: :ok, location: @illust_report }
      else
        format.html { render :edit }
        format.json { render json: @illust_report.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /illust_reports/1
  # DELETE /illust_reports/1.json
  def destroy
    @illust_report.destroy
    respond_to do |format|
      format.html { redirect_to illust_reports_url, notice: 'Illust report was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_illust_report
      @illust_report = IllustReport.find_by(id: params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def illust_report_params
      params.require(:illust_report).permit(:title, :image, :commentaire, :lesson_id, :user_id)
    end
end

_form.html.erb

<%= form_for(@illust_report) do |f| %>
  <% if @illust_report.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@illust_report.errors.count, "error") %> prohibited this illust_report from being saved:</h2>

      <ul>
      <% @illust_report.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

该模型已链接。 我是rails的初学者,我在google上搜索了很长时间,然后堆叠,并在写完这条消息之前做了很多测试。 问题出在哪儿 ? 非常感谢你的时间。

2 个答案:

答案 0 :(得分:0)

lesson_illust_reports_path是否有效?

由于您的illust_report资源在lessons资源中已命名空间,因此您必须添加lesson

答案 1 :(得分:0)

  

未定义的方法`illust_reports_path'

您拥有 嵌套资源 。所以form_for应该如下所示

<%= form_for [@lesson, @illust_report] do |f| %>

并更改illust_reports_controller的{​​{1}}方法,如下所示

new