首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Simple_form嵌套资源:未定义的方法

Simple_form嵌套资源:未定义的方法
EN

Stack Overflow用户
提问于 2020-12-20 15:48:01
回答 1查看 201关注 0票数 0

我对RoR很陌生,我在使用嵌套资源的simple_form上遇到了麻烦。

我有两个模特:面试和实习。面试has_many实践与实践belong_to面试。

当试图使用#<#Class:0x00007fda24ed79d8:0x00007fda24ed5fe8>) ie在interview_id中创建新的实践时,我遇到了Action错误(未定义的方法`practices_path‘)。在这条路线上:/访谈/:访谈_id/实践/新的)

有人能帮我找出我做错了什么吗?提前谢谢你。

控制器

代码语言:javascript
复制
class PracticesController < ApplicationController
  def index
    @practices = Practice.all
  end

  def new
    @practice = Practice.new
  end

  def create
    @interview = Interview.find(params[:interview_id])
    @practice = Practice.new(practice_params)
    @practice.interview = @interview
    if @practice.save
      redirect_to new_interview_practice_path(@interview)
    else
      render 'new'
    end
  end

  # def destroy
  #   @interview = Interview.find(params[:id])
  #   @question.interview = @interview
  #   @question.destroy
  #   redirect_to interview_questions_path(@interview)

  private

  def practice_params
    params.require(:practice).permit(:question, :type, :title)
  end
end
代码语言:javascript
复制
class InterviewsController < ApplicationController

  def index
    @interviews = Interview.all
  end

  def new
    @interview = Interview.new
  end

  def create
    @interview = Interview.new(interview_params)
    @interview.user = current_user
    if @interview.save
      redirect_to interview_path(@interview)
    end
  end

  def show
    @interview = Interview.find(params[:id])
    @question = Question.new
  end

  def destroy
    @interview = Interview.find(params[:id])
    @interview.destroy
    redirect_to interviews_path
  end

private

  def interview_params
    params.require(:interview).permit(:company, :industry, :photo)
  end
end

模型

代码语言:javascript
复制
class Interview < ApplicationRecord
  belongs_to :user
  has_many :videos, dependent: :destroy
  has_many :questions, dependent: :destroy
  has_many :practices, dependent: :destroy
  validates :company, :industry, presence: true
  has_one_attached :photo

  def days_remaining
    -(Date.today - self.final_date.to_date).to_i if self.final_date
  end

  include PgSearch::Model
  pg_search_scope :search_by_name_and_date,
    against: [:name, :open_date, :final_date, :company],
    using: {
      tsearch: { prefix: true }
    }
end
代码语言:javascript
复制
class Practice < ApplicationRecord
  belongs_to :interview
  has_many :questions, through: :interviews
end

视图

代码语言:javascript
复制
<br>
<div class="text-center">
  <h3>Lets create a new practice interview!</h3>
</div>
<div class="form-container">
  <%= simple_form_for [@interview, @practice] do |f| %>
  <div class="flex-container2">
    <div class="form-style-5">
      <legend><span class="number">1</span> Job Info</legend>
      <%= f.input :title, placeholder: "give it a name"%>
      <%= f.input :type, placeholder: "Competency"%>
      <%= f.input :question, placeholder: "add here" %>
    </div>

  </div>
  <div class="row d-flex justify-content-center mb-5">
    <div class="col-md-7">
      <%= f.submit class: "btn btn-primary"%>
    </div>
  </div>
  <% end %>
</div>

路由

代码语言:javascript
复制
Rails.application.routes.draw do
  devise_for :users
  root to: 'pages#home'
  get 'dashboard', to: 'users#dashboard'
  get 'calendar', to: 'pages#calendar'
  get 'video', to: 'pages#video'
  get 'settings', to: 'pages#settings'
  post 'sendsms', to: 'videos#send_sms'

  resources :interviews, only: [:index, :new, :create, :show, :destroy] do
    resources :questions, only: [:index, :new, :create, :show]
    resources :practices, only: [:index, :new, :create, :show, :destroy]
    resources :videos, only: [:index, :new, :create, :show] do
      resources :reviews, only: [:new, :create]
    end
  end
  resources :candidates, only: [:create, :new, :show]
  resources :searches

  get 'thank_you', to: 'videos#thank_you'

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

模式

代码语言:javascript
复制
  create_table "interviews", force: :cascade do |t|
    t.datetime "open_date"
    t.datetime "final_date"
    t.bigint "user_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "name"
    t.string "company"
    t.string "industry"
    t.index ["user_id"], name: "index_interviews_on_user_id"
  end

  create_table "practices", force: :cascade do |t|
    t.string "title"
    t.string "type"
    t.text "question"
    t.bigint "interview_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["interview_id"], name: "index_practices_on_interview_id"
  end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-21 11:28:01

怪人

代码语言:javascript
复制
Action Controller error (undefined method `practices_path' for #<#Class:0x00007fda24ed79d8:0x00007fda24ed5fe8>)

错误仅意味着Rails试图获取[@interview, @practice]的路径(在视图中使用),但@interview是空的或为零,因此创建记录的默认路径(在本例中为@practice)为practices_path (表单为/practices的帖子)。

您想要的是simple_form_for使用interviews_practices_path,它将转换为/访谈/:interview_id/practices。

PracticesController操作new中,您还需要初始化一个新的@interview变量,因为您正在视图中使用该变量。

由于您的实践模型是嵌套的,这意味着当您进入/interviews/:interview_id/practices/new时,您的ID将来自URL,并且在params[:interview_id]中可用。

因此,一个简单的解决方案是添加:

代码语言:javascript
复制
def new
  @interview = Interview.find(params[:interview_id]
  @practice = Practice.new
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65381697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档