在Rails 4

时间:2015-07-19 20:03:37

标签: ruby-on-rails controller

我正在使用回形针和Rails 4,在访问我的画廊索引页面时出现此错误。

这是错误: enter image description here

我对索引页面的代码是:

<h3> for <%= Property.name %></h3>

<table>
  <thead>
    <tr>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @galleries.property.each do |gallery| %>
      <tr>
        <td><%= link_to 'Show', property_gallery_path(params[:property_id], gallery) %></td>
        <td><%= link_to 'Edit', edit_property_gallery_path(params[:property_id], gallery) %></td>
        <td><%= link_to 'Destroy', property_gallery_path(params[:property_id], gallery), method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

我的画廊控制器页面就是这个

 class GalleriesController < ApplicationController
  before_action :set_imageable
  before_action :set_property, only: [:show, :edit, :update, :destroy]
  before_action :set_gallery, only: [:show, :edit, :update, :destroy]

  # GET /galleries
  # GET /galleries.json
  def index
    @galleries = Gallery.all
  end

  # GET /galleries/1
  # GET /galleries/1.json
  def show
  end

  # GET /galleries/new
  def new
    @gallery = Gallery.new
  end

  # GET /galleries/1/edit
  def edit
  end

  # POST /galleries
  # POST /galleries.json
  def create
    @gallery = @imageable.galleries.new(gallery_params)


    respond_to do |format|
      if @gallery.save

        if params[:images]
          params[:images].each { |image|
            @gallery.pictures.create(image: image)
          }
        end

        format.html { redirect_to @imageable, notice: 'Gallery was successfully created.' }
        format.json { render :show, status: :created, location: @gallery }
      else
        format.html { render :new }
        format.json { render json: @gallery.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /galleries/1
  # PATCH/PUT /galleries/1.json
  def update
    respond_to do |format|
      if @gallery.update(gallery_params)
        format.html { redirect_to property_gallery_path, notice: 'Gallery was successfully updated.' }
        format.json { render :show, status: :ok, location: @gallery }
      else
        format.html { render :edit }
        format.json { render json: @gallery.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /galleries/1
  # DELETE /galleries/1.json
  def destroy
    @gallery.destroy
    respond_to do |format|
      format.html { redirect_to galleries_url, notice: 'Gallery was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_gallery
      @gallery = Gallery.find(params[:id])
    end

    def set_property
      @property = Property.find(params[:id])
    end

    def set_imageable
      klass = [Property, Space].detect { |c| params["#{c.name.underscore}_id"]}
      puts "Klass is #{klass.inspect}"
      @imageable = klass.find(params["#{klass.name.underscore}_id"])
      puts "Imageable is #{@imageable.inspect}"
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def gallery_params
      params.permit(:gallery)
    end


end

现在在考虑之后,我去了我的画廊控制器并查看了索引线。在那里,有一个@galleries = Gallery.all,这当然是拉动所有与该物业相关的画廊。我知道它正在拉动所有与该属性相关的画廊,因为我有before_action:set_property和before_action:set_gallery。

我想知道no方法错误是否与设置属性有关。我不确定。但是我的property_id在我的请求参数中被设置为“1”。所以价值正在传递。

任何显而易见的事我都不见了?而且我一般都在想这个正确的方法吗?

这是我的整个画廊_控制器.rb

    class GalleriesController < ApplicationController
  before_action :set_imageable
  before_action :set_property, only: [:index, :show, :edit, :update, :destroy]
  before_action :set_gallery, only: [:show, :edit, :update, :destroy]

  # GET /galleries
  # GET /galleries.json
  def index
    @galleries = @property.galleries
  end

  # GET /galleries/1
  # GET /galleries/1.json
  def show
  end

  # GET /galleries/new
  def new
    @gallery = Gallery.new
  end

  # GET /galleries/1/edit
  def edit
  end

  # POST /galleries
  # POST /galleries.json
  def create
    @gallery = @imageable.galleries.new(gallery_params)


    respond_to do |format|
      if @gallery.save

        if params[:images]
          params[:images].each { |image|
            @gallery.pictures.create(image: image)
          }
        end

        format.html { redirect_to @imageable, notice: 'Gallery was successfully created.' }
        format.json { render :show, status: :created, location: @gallery }
      else
        format.html { render :new }
        format.json { render json: @gallery.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /galleries/1
  # PATCH/PUT /galleries/1.json
  def update
    respond_to do |format|
      if @gallery.update(gallery_params)
        format.html { redirect_to property_gallery_path, notice: 'Gallery was successfully updated.' }
        format.json { render :show, status: :ok, location: @gallery }
      else
        format.html { render :edit }
        format.json { render json: @gallery.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /galleries/1
  # DELETE /galleries/1.json
  def destroy
    @gallery.destroy
    respond_to do |format|
      format.html { redirect_to galleries_url, notice: 'Gallery was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_gallery
      @gallery = Gallery.find(params[:id])
    end

    def set_property
      @property = Property.find(params[:id])
    end

    def set_imageable
      klass = [Property, Space].detect { |c| params["#{c.name.underscore}_id"]}
      puts "Klass is #{klass.inspect}"
      @imageable = klass.find(params["#{klass.name.underscore}_id"])
      puts "Imageable is #{@imageable.inspect}"
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def gallery_params
      params.permit(:gallery)
    end


end

2 个答案:

答案 0 :(得分:1)

首先

   @galleries = Gallery.all
无论属性如何,

都会返回所有画廊

如果你想获取任何属性的画廊,请执行此操作

@galleries = @property.galleries

其次,你不能从画廊中获取单个属性,因为画廊是你在这里做的一个数组

<% @galleries.property.each do |gallery| %>

你需要做

<% @galleries.each do |gallery| %>
 <%property = gallery.property %>
 #what you want to do
<% end %>

还会更新您的before_action

 before_action :set_property, only: [:index, :show, :edit, :update, :destroy]

并更改此

def set_property
  @property = Property.find(params[:id])
end

def set_property
  @property = Property.find(params[:property_id])
end

答案 1 :(得分:0)

@galleries是复数 - 基本上是一个gallery对象数组,所以你不能直接在它上面调用一个方法。您需要遍历每个,然后在单个对象上调用该方法。

您可以通过以下方式实现类似的目标:

<tbody>
  <% @galleries.each do |gallery| %>
    <tr>
      <td><%= link_to 'Show', property_gallery_path(params[:property_id], gallery) %></td>
      <td><%= link_to 'Edit', edit_property_gallery_path(params[:property_id], gallery) %></td>
      <td><%= link_to 'Destroy', property_gallery_path(params[:property_id], gallery), method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</tbody>