在过去的几天里,我在让我的嵌套资源正确创建和显示时遇到了问题。StackOverflow 上有很多类似的问题,也有很多关于此的博客文章,但它们似乎都处理的是旧版本的 Rails 或不同的问题。我正处于一旦我最终修复某些东西,就会弹出另一个错误的地步。我把它缩小到我犯了一个愚蠢的错误或错字太缺乏经验而没有注意到。
我有一个属于 Venue 模型的 Jobs 模型。场地运行良好,我什至能够进入每个场地下的嵌套作业索引并调出新建和编辑表单,但是去“显示”或创建新作业会导致undefined method
错误。经过大量搜索后,我发现了很多相同的问题并尝试实施他们的修复,但现在我遇到了路由错误。
我的大部分困惑来自何时离开@,何时在参数中使用 :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
作业控制器.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
场地控制器.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
工作.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 的新手来说,肯定还有一些我不完全理解的基础知识。
- 如果我发布了太多或太少的代码,请告诉我,我不完全确定最少需要多少。
- 可能有不止一个错误,可能有很多错误,其中许多可能是由于我在我真的不知道自己在做什么时试图修复这个问题造成的。我想自己解决这个问题,但是当我真的让它变得更糟时,我无法再处理它了。
我尝试过弄乱路线,更改实际的链接和路线,弄乱范围,以及我能找到的针对这些错误的许多常见修复程序,但似乎都没有帮助。
谢谢!