在过去几天里,我遇到了一个问题,无法正确创建和显示嵌套资源。StackOverflow上有很多类似的问题,也有很多关于这方面的博客文章,但它们似乎都是针对旧版本的Rails或不同的问题。一旦我最终修复了某个问题,就会弹出另一个错误。我把它缩小到我犯了一个愚蠢的错误或打字错误,因为我太缺乏经验而没有注意到。
我有一个属于Venue模型的Jobs模型。场地运行良好,我甚至可以转到每个场地下的嵌套作业索引,并显示"新建"one_answers"编辑"表单,但转到"显示"或创建新作业会导致undefined method
错误。经过大量搜索,我发现了很多相同的问题,并试图实现他们的修复,但现在我遇到了路由错误。
我最困惑的是什么时候去掉@,什么时候在params中使用:venue_id而不是:id,等等。我看到的每个例子似乎都有不同的方式,我似乎无法让它们中的任何一个为我工作。
任何正确方向的颠簸都会非常有帮助。
路由错误
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>}
路线.rb
Twist::Application.routes.draw do
resources :users
devise_for :users
resources :venues do
resources :jobs
end
end
jobs_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
venues_controller.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
job.rb
class Job < ActiveRecord::Base
attr_accessible :description, :name, :requirement, :venue_id
validates :name, :presence => true, :length => { :minimum => 3 }
belongs_to :venue
end
场地.rb
class Venue < ActiveRecord::Base
attr_accessible :areacode, :avatar, :city, :name, :state
has_many :jobs
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
/jobs/show.html.erb
<p id="notice"><%= notice %></p>
<% @job = Job.find(param[:venue_id]) %>
<p>
<b>Name:</b>
<%= @job.name %>
</p>
<p>
<b>Company:</b>
<p><%= @venue.name %></p>
<p><%= link_to @job.venue.name, venue_path(@venue) %></p>
<p>
<b>Job:</b>
<%= @job.job_id %>
</p>
<p>
<b>Description:</b>
<%= @job.description %>
</p>
<p>
<b>Requirement:</b>
<%= @job.requirement %>
</p>
<%= link_to 'Edit', edit_venue_job_path(@venue, @job) %> |
<%= link_to 'Back', venue_jobs_path(@venue, @job) %>
**/jobs/index.html.erb**
<div class="usergrid">
<% jobs = @venue.jobs %>
<% @venue.jobs.each do |job| %>
<div class = "user venue">
<p>
<h2><%= link_to job.name, venue_job_path(@venue) %></h2>
<%= link_to 'Edit', edit_venue_job_path(@venue, job) %><br/>
<%= link_to 'Delete', venue_jobs_path(@venue, @job), :confirm => 'Are you sure?', :method => :delete %>
</div>
<% end %>
<div style="clear:both"></div>
</div>
<%= link_to 'New Job', new_venue_job_path(@venue) %>
我意识到。。。
- 这可能是一个非常明显的解决方案,但作为Rails的新手,肯定还有一些基本原理我还不完全理解
- 如果我发布的代码太多或太少,请告诉我,我不完全确定多少是最不必要的
- 可能会有不止一个错误,可能会有很多错误,其中许多错误可能是由于我在真的不知道自己在做什么的时候试图解决这个问题造成的。我想自己解决这个问题,但当我真的只是让事情变得更糟的时候,我再也无法忍受了
我试过扰乱路线,改变实际的链接和路线,扰乱范围,以及我能找到的针对这些错误的许多常见修复方法,但似乎都没有帮助。
谢谢!
错误表示给定参数没有路由:
{:action=>"show", :controller=>"jobs", :venue_id=> "an_id"}
您可以通过运行rake路由来检查这一点,您会看到,由于作业嵌套在场所下,jobs#show
控制器操作需要两个参数:venue_id
(它是作业的"父")和id
,它是作业id。
我很快检查了你的代码,我认为导致错误的原因之一是这一行:
<h2><%= link_to job.name, venue_job_path(@venue) %></h2>
这应该是
<h2><%= link_to job.name, venue_job_path(@venue, @job) %></h2>
基本上,每当你试图在不提供地点的情况下呈现作业的链接时,你都会遇到这种错误。
如果你需要更多的细节或信息,请告诉我。