【发布时间】:2015-05-24 22:54:40
【问题描述】:
我是 ruby on rails 的新手,非常感谢任何帮助。我不确定如何解决我的错误。我想我应该在我的控制器中做更多的事情,但我很困惑,因为这是我面临的嵌套资源挑战
我试图在我的视图中查看广告(节目)的详细信息,但是当我单击链接 show 时,我收到以下错误:
No route matches {:action=>"new", :controller=>"adverts", :userr_id=>nil} missing required keys: [:userr_id]
ActionController::UrlGenerationError (No route matches {:action=>"new", :controller=>"adverts", :userr_id=>nil} missing required keys: [:userr_id]):
app/views/shared/_header_recruiter.html.erb:6:in `_app_views_shared__header_recruiter_html_erb___2254802171992378619_70258749378740'
app/views/adverts/show.html.erb:1:in `_app_views_adverts_show_html_erb___3448815807687044417_70258749644120'
app/controllers/adverts_controller.rb:26:in `show'
我在 userr(招聘人员)下嵌套了广告 - routes.rb
Rails.application.routes.draw do
devise_for :userrs
resources :userrs do
resources :adverts
end
devise_for :userjs
root 'static_pages#homepg'
get 'search', to: 'static_pages#searchpg'
end
我创建了一个名为 search 的静态页面 - static_pages_controller.rb
class StaticPagesController < ApplicationController
respond_to :html, :xml, :json
def searchpg
@adverts = Advert.all
end
end
我的搜索视图文件中有以下编码 - searchpg.html.erb
<div>
<table>
<thead>
<tr>
<th>Published</th>
<th>Title</th>
<th>Content</th>
<th>City</th>
<th></th>
</tr>
</thead>
<tbody>
<% @adverts.each do |advert| %>
<tr>
<td><%= advert.created_at.strftime("%B %d, %Y") %></td>
<td><%= link_to advert.title, '#' %></td>
<td><%= advert.content %></td>
<td><%= advert.city %></td>
<td><%= link_to 'Show', userr_advert_path(advert.userr, advert) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
这些是我的路线:
userr_adverts GET /userrs/:userr_id/adverts(.:format) adverts#index
POST /userrs/:userr_id/adverts(.:format) adverts#create
new_userr_advert GET /userrs/:userr_id/adverts/new(.:format) adverts#new
edit_userr_advert GET /userrs/:userr_id/adverts/:id/edit(.:format) adverts#edit
userr_advert GET /userrs/:userr_id/adverts/:id(.:format) adverts#show
PATCH /userrs/:userr_id/adverts/:id(.:format) adverts#update
PUT /userrs/:userr_id/adverts/:id(.:format) adverts#update
DELETE /userrs/:userr_id/adverts/:id(.:format) adverts#destroy
我已经有一个广告控制器:
class AdvertsController < ApplicationController
respond_to :html, :xml, :json
before_action :set_advert, only: [:show, :edit, :update, :destroy]
def index
@userr = Userr.find(params[:userr_id])
@adverts = @userr.adverts.order("created_at DESC")
respond_with(@adverts)
end
def show
@userr = Userr.find(params[:userr_id])
@advert = @userr.adverts.find(params[:id])
respond_with(@advert)
end
def new
@userr = Userr.find(params[:userr_id])
@advert = @userr.adverts.build
respond_with(@advert)
end
def edit
@userr = Userr.find(params[:userr_id])
@advert = @userr.adverts.find(params[:id])
end
def create
@userr = Userr.find(params[:userr_id])
@advert = @userr.adverts.create(advert_params)
respond_to do |format|
if @advert.save
format.html { redirect_to([@advert.userr, @advert], notice: 'Advert was successfully created.') }
format.json { render json: @advert, status: :created, location: @advert }
else
format.html { render action: "new" }
format.json { render json: @advert.errors, status: :unprocessable_entity }
end
end
end
def update
@userr = Userr.find(params[:userr_id])
@advert = @userr.adverts.find(params[:id])
respond_to do |format|
if @advert.update_attributes(advert_params)
format.html { redirect_to([@advert.userr, @advert], notice: 'Advert was successfully updated.') }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @advert.errors, status: :unprocessable_entity }
end
end
end
def destroy
@userr = Userr.find(params[:userr_id])
@advert = @userr.adverts.find(params[:id])
@advert.destroy
respond_to do |format|
#1st argument reference the path /recruiters/:recruiter_id/adverts/
format.html { redirect_to(recruiter_adverts_url) }
format.json { head :no_content }
end
end
private
def set_advert
@advert = Advert.find(params[:id])
end
def advert_params
params.require(:advert).permit(:title, :content, :category_jobtype_id, :category_positiontype_id, :salarystart, :salaryend, :category_country_id, :city, :town, :postcode, :category_editorialapproval_id, :category_applicationrequest_id, :category_advert_id, :userr_id )
end
end
【问题讨论】:
-
查看 app/views/shared/_header_recruiter.html.erb:6。尝试使用
new_userr_advert(params[:userr_id])而不是new_userr_advert
标签: ruby-on-rails ruby ruby-on-rails-4