我有一个侧边栏,显示用户的结构。 我这样显示:
<% current_user.constructions.each do |obra| %>
<li>
<%= link_to construction_path(obra) do %>
<i class="fa fa-home"></i> <%= obra.name %>
<% end %>
<ul class="nav child_menu" style="display: block">
<li>
<%= link_to "Documentos", construction_docs_path(obra) %>
</li>
<li>
<%= link_to "Meus Pagamentos", construction_boletos_path(obra) %>
</li>
</ul>
</li>
<% end %>
它正在处理所有事情,除非我尝试创建一个新建筑,它说:
没有路线匹配{:action =&gt;&#34; show&#34;,:controller =&gt;&#34; constructions&#34;,:id =&gt; nil} 缺少必需的键:[:id]
我想这是因为它失去了对current_user.constructions的追踪,因为我的控制器已经
def new
@construction = current_user.constructions.build
end
...调用 我如何通过当前的结构&#39; ids,在我创建新建筑的同时显示它?
编辑 -
constructions_controller.rb
class constructionsController < ApplicationController
before_filter :authenticate_user!
before_action :set_construction, only: [:show, :edit, :update, :destroy]
# GET /constructions
# GET /constructions.json
def index
@constructions = Construction.all
end
# GET /constructions/1
# GET /constructions/1.json
def show
end
# GET /constructions/new
def new
@construction = current_user.constructions.build
end
# GET /constructions/1/edit
def edit
end
# POST /constructions
# POST /constructions.json
def create
@construction = current_user.constructions.build(construction_params)
respond_to do |format|
if @construction.save
format.html { redirect_to @construction, notice: 'construction was successfully created.' }
format.json { render :show, status: :created, location: @construction }
else
format.html { render :new }
format.json { render json: @construction.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /constructions/1
# PATCH/PUT /constructions/1.json
def update
respond_to do |format|
if @construction.update(construction_params)
format.html { redirect_to @construction, notice: 'construction was successfully updated.' }
format.json { render :show, status: :ok, location: @construction }
else
format.html { render :edit }
format.json { render json: @construction.errors, status: :unprocessable_entity }
end
end
end
# DELETE /constructions/1
# DELETE /constructions/1.json
def destroy
@construction.destroy
respond_to do |format|
format.html { redirect_to constructions_url, notice: 'construction was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_construction
@construction = Construction.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def construction_params
params.require(:construction).permit(:name, :locale, :start_date, :description, :image)
end
end
的routes.rb
Rails.application.routes.draw do
resources :boletos
resources :documentos
resources :constructions do
get 'construction_documents/index', :path => 'docs', :as => 'docs'
get 'construction_boletos/index', :path => 'boletos', :as => 'boletos'
end
devise_for :users
root 'plainpage#index'
end
构造/ new.html.erb
<h1>New Construction</h1>
<%= render 'form' %>
<%= link_to 'Back', constructions_path %>
构造/ _form.html.erb
<%= form_for @construction, html: { multipart: true } do |f| %>
<% if @construction.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@construction.errors.count, "error") %> prohibited this construction from being saved:</h2>
<ul>
<% @construction.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :locale %><br>
<%= f.text_field :locale %>
</div>
<div class="field">
<%= f.label :start_date %><br>
<%= f.date_select :start_date %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>