嵌套路由不能全部索引

时间:2015-04-23 12:01:42

标签: ruby-on-rails view controller routes nested

我有如下嵌套路线;

resources :boats, except: :destroy do
      resources :pictures
  end

因此用户可以上传图片,一切正常。但是picture/index.html.erb。我看不到所有的照片。它返回零。但我可以在数据库中看到图片。可能是因为我试图检索错误的参数。

当我取出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总是返回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来创建它是好的,但所有的图片都有自己的销毁按钮。当我看到它们时都显示相同的地址,所以点击销毁,销毁所有这些;

所有的毁灭ID都是一样的。我虽然索引动作列出所有并单独销毁擦除。这种情况也适用于编辑动作

enter image description here

编辑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

1 个答案:

答案 0 :(得分:1)

您的创建操作中没有@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 %>