【发布时间】:2015-07-20 17:45:58
【问题描述】:
我正在尝试制作乐队音乐会议程,但在 set_agenda 方法中出现此错误。
ActiveRecord::RecordNotFound at /agendas/concerts
Couldn't find Agenda with 'id'=concerts
private
# Use callbacks to share common setup or constraints between actions.
def set_agenda
@agenda = Agenda.find(params[:id])
end
我之前生成了一个议程脚手架:
$ rails g scaffold Agenda title info:text date_concert:date begin_time:time end_time:time
然后一切正常,但是当我尝试添加一个名为 Concerts 的方法时,我有一个错误,我不知道该怎么做。我是 Rails 的初学者,我需要帮助。
这是我的议程控制器:
class AgendasController < ApplicationController
before_action :set_agenda, only: [:show, :edit, :update, :destroy, :concerts]
def concerts
@user= User.find(params[:id])
@agendas= @user.agendas.paginate(page: params[:page])
end
# GET /agendas
# GET /agendas.json
def index
@agendas = Agenda.all
end
# GET /agendas/1
# GET /agendas/1.json
def show
end
# GET /agendas/new
def new
@agenda = Agenda.new
end
# GET /agendas/1/edit
def edit
end
# POST /agendas
# POST /agendas.json
def create
@agenda = Agenda.new(agenda_params)
respond_to do |format|
if @agenda.save
format.html { redirect_to @agenda, notice: 'Agenda was successfully created.' }
format.json { render :show, status: :created, location: @agenda }
else
format.html { render :new }
format.json { render json: @agenda.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /agendas/1
# PATCH/PUT /agendas/1.json
def update
respond_to do |format|
if @agenda.update(agenda_params)
format.html { redirect_to @agenda, notice: 'Agenda was successfully updated.' }
format.json { render :show, status: :ok, location: @agenda }
else
format.html { render :edit }
format.json { render json: @agenda.errors, status: :unprocessable_entity }
end
end
end
# DELETE /agendas/1
# DELETE /agendas/1.json
def destroy
@agenda.destroy
respond_to do |format|
format.html { redirect_to agendas_url, notice: 'Agenda was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_agenda
@agenda = Agenda.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def agenda_params
params.require(:agenda).permit(:title, :info, :date_concert, :begin_time, :end_time)
end
end
我的模型 Agenda.rb
class Agenda < ActiveRecord::Base
belongs_to :user
default_scope -> { order(date_concert: :desc) }
validates :title, presence: true
validates :info, presence: true, length: { maximum: 140 }
end
My view concerts.html.erb(我想展示乐队的下一场音乐会!)
<h1> Agenda Concerts </h1>
<ol>
<li>
<%= form_for(@agendas) do |agenda| %>
<span class="title"><%= link_to agenda.title, agenda_path %></span>
<span class="info"><%= agenda.info %></span>
<span class="date"><%= agenda.date_concert %></span>
<span class="begin_time"><%= agenda.begin_time %></span>
<span class="end_time"><%= agenda.end_time %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(agenda.created_at) %> ago.
</span>
<% end %>
</li>
</ol>
<%= will_paginate @agendas %>
我的模型 user.rb
class User < ActiveRecord::Base
has_many :agendas
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
=begin
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :profile_name, :full_name
=end
def full_name
"#{first_name}" + " " + "#{last_name}"
end
end
我的路线.rb
Rails.application.routes.draw do
get 'session/new'
get 'session/create'
get 'gallery/lightbox'
get 'gallery/caroussel'
devise_for :users, :skip => :registrations
resources :agendas do
member do
get :concerts
end
end
end
【问题讨论】:
-
没有足够的答案...如果我是正确的,标准的 URL 结构是
/agendas/:id/concerts,类似于编辑的工作原理 (/agenda/:id/edit) -
尝试将此
<%= link_to agenda.title, agenda_path %>更改为<%= link_to agenda.title, concerts_agenda_path(agenda) %> -
这不起作用。我需要在我使用的这周内完成这个网站:
标签: ruby ruby-on-rails-4 routes