嵌套路由无法索引所有
Nested Routes can not index all
我有如下嵌套路由;
resources :boats, except: :destroy do
resources :pictures
end
所以用户可以上传图片,一切正常。但是picture/index.html.erb
。我看不到所有的图片。它 returns 零。但是我可以在数据库中看到图片。可能是因为我试图检索错误的参数。
当我取出 if 语句时 <% if @pictures.present? %>
它抛出错误;
NoMethodError in PicturesController#create
undefined method `each' for nil:NilClass
<% @pictures.each do |pic| %>
这里是#index view
;
<div class="container">
<h1>Pictures#index!</h1>
<p>Find me in app/views/pictures/index.html.erb</p>
<% if @pictures.present? %> <!-- Returns nil-->
<% @pictures.each do |pic| %>
</br>
<%= pic.name %>
<%= image_tag pic.image_url(:thumb).to_s %>
<%= link_to "edit", edit_boat_picture_path(@boat, @picture) %> |
<td><%= link_to 'Destroy', [@boat, @picture], confirm: 'Are you sure?', method: :delete %></td> |
</br>
<% end %>
<% end %>
<%= link_to "edit", edit_boat_picture_path(@boat, @picture) %> |
</br>
</br>
<%= link_to "add", new_boat_picture_path(@boat, @picture) %>
</div>
所以这里,picture.present returns总是nil,所以我无法显示任何图像。
这里是pictures controller
;
class PicturesController < ApplicationController
before_action :logged_in_user
before_filter :load_parent
def index
@pictures = @boat.pictures.all
end
def new
@picture = @boat.pictures.new
end
def show
@picture = @boat.pictures.find(params[:id])
end
def create
@picture = @boat.pictures.new(picture_params)
if @picture.save
#flash[:success] = "Continue from here"
render 'index'
#redirect_to boat_path(@boat)
else
render 'new'
end
end
def edit
@picture = Picture.find(params[:id])
end
def update
@picture = @boat.pictures.find(params[:id])
if @picture.update_attributes(picture_params)
flash[:notice] = "Successfully updated picture."
render 'index'
else
render 'edit'
end
end
def destroy
@picture = @boat.pictures.find(params[:id])
@picture.destroy
flash[:notice] = "Successfully destroyed picture."
redirect_to boat_path(@boat)
end
private
def picture_params
params.require(:picture).permit(:name, :image)
end
def load_parent
@boat = Boat.find(params[:boat_id])
end
end
编辑 1:
日志;
{"utf8"=>"✓", "authenticity_token"=>"i3FW1zbhoGW2vavipN9NJ2Fvi9R1Lk/CKDsAttuqHWb8rFNmJgXpjE2D25oAqJ3xp9BXAnd0kDmrdIxhn1Qrpw==", "picture"=>{"name"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fb4a5f50f30 @tempfile=#<Tempfile:/var/folders/w0/703ccggs56l3hrc79h3rdylm0000gn/T/RackMultipart20150423-5028-1rbpgnj.jpg>, @original_filename="imgres-4.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"picture[image]\"; filename=\"imgres-4.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Done", "controller"=>"pictures", "action"=>"create", "boat_id"=>"114"}
编辑 2:
如果我添加@pictures = @boat.pictures.all
创建就可以了,但是所有图片都有自己的销毁button.And当我看它们时都显示相同的地址,所以点击销毁,销毁所有他们;
所有的销毁id都是一样的。我虽然索引操作列出了所有并单独销毁擦除。这种情况对于编辑操作也是相同的
编辑 3:
#boats controller
class BoatsController < ApplicationController
before_action :logged_in_user, only: [:new, :show, :edit, :update]
def new
@boat = Boat.new
end
def create
@boat = current_user.boats.new(boat_params) if logged_in?
if @boat.save
#flash[:success] = "Continue from here"
render 'edit'
else
render 'new'
end
end
def show
@boat = Boat.find(params[:id])
end
def edit
@boat = Boat.find(params[:id])
end
def update
@boat = Boat.find(params[:id])
if @boat.update_attributes(boat_params)
flash[:success] = "The Boat Saved"
redirect_to root_path
else
render 'edit'
end
end
def update_years
# updates year and model based on brand selected
brand = Brand.find_by_name(params[:brand_name])
# map to name and id for use in our options_for_select
@years = brand.years.map{|a| [a.name, a.name]}.insert(0, "Select a Year") #use a.name here instead of a.id
@models = brand.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model")#use s.name here instead of s.id
end
def update_models
# updates model based on year selected
year = Year.find_by_name(params[:year_name])
@models = year.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model") #use s.name here instead of s.id
end
private
def boat_params
params.require(:boat).permit(:brand, :year, :model, :captained, :boat_type, :daily_price, :boat_length, :listing_tagline, :listing_description, :boat_category, :hull_material, :mast_material)
end
end
您的创建操作中没有 @pictures
。
你可以试试:
def create
@picture = @boat.pictures.new(picture_params)
if @picture.save
#flash[:success] = "Continue from here"
@pictures = @boat.pictures.all
render 'index'
#redirect_to boat_path(@boat)
else
render 'new'
end
end
对于按钮:
<% @pictures.each do |pic| %>
</br>
<%= pic.name %>
<%= image_tag pic.image_url(:thumb).to_s %>
<%= link_to "edit", edit_boat_picture_path(@boat, pic) %> |
<td><%= link_to 'Destroy', boat_picture_path(@boat, pic), confirm: 'Are you sure?', method: :delete %></td> |
</br>
<% end %>
我有如下嵌套路由;
resources :boats, except: :destroy do
resources :pictures
end
所以用户可以上传图片,一切正常。但是picture/index.html.erb
。我看不到所有的图片。它 returns 零。但是我可以在数据库中看到图片。可能是因为我试图检索错误的参数。
当我取出 if 语句时 <% if @pictures.present? %>
它抛出错误;
NoMethodError in PicturesController#create
undefined method `each' for nil:NilClass
<% @pictures.each do |pic| %>
这里是#index view
;
<div class="container">
<h1>Pictures#index!</h1>
<p>Find me in app/views/pictures/index.html.erb</p>
<% if @pictures.present? %> <!-- Returns nil-->
<% @pictures.each do |pic| %>
</br>
<%= pic.name %>
<%= image_tag pic.image_url(:thumb).to_s %>
<%= link_to "edit", edit_boat_picture_path(@boat, @picture) %> |
<td><%= link_to 'Destroy', [@boat, @picture], confirm: 'Are you sure?', method: :delete %></td> |
</br>
<% end %>
<% end %>
<%= link_to "edit", edit_boat_picture_path(@boat, @picture) %> |
</br>
</br>
<%= link_to "add", new_boat_picture_path(@boat, @picture) %>
</div>
所以这里,picture.present returns总是nil,所以我无法显示任何图像。
这里是pictures controller
;
class PicturesController < ApplicationController
before_action :logged_in_user
before_filter :load_parent
def index
@pictures = @boat.pictures.all
end
def new
@picture = @boat.pictures.new
end
def show
@picture = @boat.pictures.find(params[:id])
end
def create
@picture = @boat.pictures.new(picture_params)
if @picture.save
#flash[:success] = "Continue from here"
render 'index'
#redirect_to boat_path(@boat)
else
render 'new'
end
end
def edit
@picture = Picture.find(params[:id])
end
def update
@picture = @boat.pictures.find(params[:id])
if @picture.update_attributes(picture_params)
flash[:notice] = "Successfully updated picture."
render 'index'
else
render 'edit'
end
end
def destroy
@picture = @boat.pictures.find(params[:id])
@picture.destroy
flash[:notice] = "Successfully destroyed picture."
redirect_to boat_path(@boat)
end
private
def picture_params
params.require(:picture).permit(:name, :image)
end
def load_parent
@boat = Boat.find(params[:boat_id])
end
end
编辑 1: 日志;
{"utf8"=>"✓", "authenticity_token"=>"i3FW1zbhoGW2vavipN9NJ2Fvi9R1Lk/CKDsAttuqHWb8rFNmJgXpjE2D25oAqJ3xp9BXAnd0kDmrdIxhn1Qrpw==", "picture"=>{"name"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fb4a5f50f30 @tempfile=#<Tempfile:/var/folders/w0/703ccggs56l3hrc79h3rdylm0000gn/T/RackMultipart20150423-5028-1rbpgnj.jpg>, @original_filename="imgres-4.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"picture[image]\"; filename=\"imgres-4.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Done", "controller"=>"pictures", "action"=>"create", "boat_id"=>"114"}
编辑 2:
如果我添加@pictures = @boat.pictures.all
创建就可以了,但是所有图片都有自己的销毁button.And当我看它们时都显示相同的地址,所以点击销毁,销毁所有他们;
所有的销毁id都是一样的。我虽然索引操作列出了所有并单独销毁擦除。这种情况对于编辑操作也是相同的
编辑 3:
#boats controller
class BoatsController < ApplicationController
before_action :logged_in_user, only: [:new, :show, :edit, :update]
def new
@boat = Boat.new
end
def create
@boat = current_user.boats.new(boat_params) if logged_in?
if @boat.save
#flash[:success] = "Continue from here"
render 'edit'
else
render 'new'
end
end
def show
@boat = Boat.find(params[:id])
end
def edit
@boat = Boat.find(params[:id])
end
def update
@boat = Boat.find(params[:id])
if @boat.update_attributes(boat_params)
flash[:success] = "The Boat Saved"
redirect_to root_path
else
render 'edit'
end
end
def update_years
# updates year and model based on brand selected
brand = Brand.find_by_name(params[:brand_name])
# map to name and id for use in our options_for_select
@years = brand.years.map{|a| [a.name, a.name]}.insert(0, "Select a Year") #use a.name here instead of a.id
@models = brand.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model")#use s.name here instead of s.id
end
def update_models
# updates model based on year selected
year = Year.find_by_name(params[:year_name])
@models = year.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model") #use s.name here instead of s.id
end
private
def boat_params
params.require(:boat).permit(:brand, :year, :model, :captained, :boat_type, :daily_price, :boat_length, :listing_tagline, :listing_description, :boat_category, :hull_material, :mast_material)
end
end
您的创建操作中没有 @pictures
。
你可以试试:
def create
@picture = @boat.pictures.new(picture_params)
if @picture.save
#flash[:success] = "Continue from here"
@pictures = @boat.pictures.all
render 'index'
#redirect_to boat_path(@boat)
else
render 'new'
end
end
对于按钮:
<% @pictures.each do |pic| %>
</br>
<%= pic.name %>
<%= image_tag pic.image_url(:thumb).to_s %>
<%= link_to "edit", edit_boat_picture_path(@boat, pic) %> |
<td><%= link_to 'Destroy', boat_picture_path(@boat, pic), confirm: 'Are you sure?', method: :delete %></td> |
</br>
<% end %>