过去几天我遇到了一个问题,即让我的嵌套资源正确创建和显示。 StackOverflow上有很多类似的问题和许多关于此的博客文章,但它们似乎都处理旧版本的Rails或其他问题。我正处于一旦我最终修复某些东西,另一个错误弹出的地步。我把它缩小到让我犯了一个愚蠢的错误或错字太缺乏经验而无法注意到。
我有一个属于Venue模型的Jobs模型。场地工作正常,我甚至可以在每个Venue下找到我的嵌套Jobs索引,并调出New和Edit表单,但是去'Show'或创建一个新的Job导致{{{ 1}}错误。经过大量的搜索,我发现了很多同样的问题,并试图实现他们的修复,但现在我收到了路由错误。
我的大部分混淆来自何时离开@,何时使用:venue_id而不是:id in params等。我看到的每个例子似乎都有不同的方式我似乎无法做任何他们为我工作。
正确方向上的任何碰撞都会非常有用。
路由错误
undefined method
的routes.rb
No route matches {:action=>"show", :controller=>"jobs", :venue_id=>#<Venue id: 1, name: "Burger Chef", city: "Chicago", state: "Illinois", areacode: 60614, created_at: "2013-02-05 21:33:41", updated_at: "2013-02-06 23:01:05", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>}
jobs_controller.rb
Twist::Application.routes.draw do
resources :users
devise_for :users
resources :venues do
resources :jobs
end
end
venues_controller.rb
class JobsController < ApplicationController
# GET /jobs
# GET /jobs.json
before_filter :get_venue
def get_venue
@venue = Venue.find(params[:venue_id])
end
def index
@jobs = @venue.jobs
respond_to do |format|
format.html # index.html.erb
format.json { render json: @jobs }
end
end
# GET /jobs/1
# GET /jobs/1.json
def show
@job = @venue.job.find(params[:id])
if params[:id]
@venue = Venue.where(:id => params[:id]).first
@jobs = @venue.job_url
else
@jobs = Jobs.all
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: @job }
end
end
# GET /jobs/new
# GET /jobs/new.json
def new
@job = @venue.jobs.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @job }
end
end
# GET /jobs/1/edit
def edit
@job = @venue.job.find(params[:venue_id])
end
# POST /jobs
# POST /jobs.json
def create
@job = @venue.jobs.new(params[:job])
respond_to do |format|
if @job.save
format.html { redirect_to :action => :show, :id => @venue.id,
notice: 'Job was successfully created.' }
format.json { render json: [@venue, @job],
status: :created,
location: [@venue, @job] }
else
format.html { render action: "new" }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
# PUT /jobs/1
# PUT /jobs/1.json
def update
@job = Job.find(params[:id])
respond_to do |format|
if @job.update_attributes(params[:job])
format.html { redirect_to @job, notice: 'Job was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jobs/1
# DELETE /jobs/1.json
def destroy
@job = Job.find(params[:id])
@job.destroy
respond_to do |format|
format.html { redirect_to jobs_url }
format.json { head :no_content }
end
end
end
job.rb
class VenuesController < ApplicationController
# GET /venues
# GET /venues.json
def index
@venues = Venue.all
if params[:name]
@user = User.where(:name => params[:name]).first
@venues = @user.venues
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @venues }
end
end
# GET /venues/1
# GET /venues/1.json
def show
@venue = Venue.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @venue }
end
end
# GET /venues/new
# GET /venues/new.json
def new
@venue = Venue.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @venue }
end
end
# GET /venues/1/edit
def edit
@venue = Venue.find(params[:id])
#if session[:user_id] != @venue.user_id
# flash[:notice] = "Sorry, you cannot edit this venue."
# redirect_to(venues_path)
# =>end
end
# POST /venues
# POST /venues.json
def create
@venue = Venue.new(params[:venue_id])
respond_to do |format|
if @venue.save
format.html { redirect_to @venue, notice: 'Venue was successfully created.' }
format.json { render json: @venue, status: :created, location: @venue }
else
format.html { render action: "new" }
format.json { render json: @venue.errors, status: :unprocessable_entity }
end
end
end
# PUT /venues/1
# PUT /venues/1.json
def update
@venue = Venue.find(params[:id])
respond_to do |format|
if @venue.update_attributes(params[:venue])
format.html { redirect_to @venue, notice: 'Venue was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @venue.errors, status: :unprocessable_entity }
end
end
end
# DELETE /venues/1
# DELETE /venues/1.json
def destroy
@venue = Venue.find(params[:id])
@venue.destroy
respond_to do |format|
format.html { redirect_to venues_url }
format.json { head :no_content }
end
end
end
venue.rb
class Job < ActiveRecord::Base
attr_accessible :description, :name, :requirement, :venue_id
validates :name, :presence => true, :length => { :minimum => 3 }
belongs_to :venue
end
/jobs/show.html.erb
class Venue < ActiveRecord::Base
attr_accessible :areacode, :avatar, :city, :name, :state
has_many :jobs
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
我意识到......
我已经尝试弄乱路线,改变实际的链接和路线,搞乱范围,以及我能找到的这些错误的许多常见修复,但似乎没有任何帮助。
谢谢!
答案 0 :(得分:8)
错误是说没有给定参数的路径:
{:action=>"show", :controller=>"jobs", :venue_id=> "an_id"}
您可以通过运行rake路由来检查,并且您会看到,由于作业嵌套在场地下,jobs#show
控制器操作需要两个参数:venue_id
(这是作业的' parent')和作为工作ID的id
。
我快速检查了你的代码,我认为导致错误的原因之一是这一行:
<h2><%= link_to job.name, venue_job_path(@venue) %></h2>
这应该是
<h2><%= link_to job.name, venue_job_path(@venue, @job) %></h2>
基本上,只要您尝试在不提供场地的情况下渲染作业链接,您就会遇到这种错误。
如果您需要更多详细信息或更多信息,请与我们联系。