为什么我的链接集合包含空对象?

时间:2016-10-27 13:36:44

标签: ruby-on-rails collections views controllers

为什么我得到带有nil参数的集合对象,我没有建议?

的routes.rb

devise_for :users
  root to: "posts#index"

  resources :posts do
    resources :comments, only: [:new, :create]

    resources  :images do
      resources :comments, only: [:new, :create]
    end

    resources  :links do
      resources :comments, only: [:new, :create]
    end

  end

post_controller:

 class PostsController < ApplicationController

      before_filter :authenticate_user!, :only => [:new, :create]

      def index
        @posts = Post.all
      end

      def show
        @post = Post.find(params[:id])
      end

      def new
        @post = Post.new
      end

      def create
        @post = Post.new(post_params)

        respond_to do |format|
          if @post.save
            format.html { redirect_to @post, notice: 'Post was successfully   created.' }
            format.json { render :show, status: :created, location: @post }
          else
            format.html { render :new }
            format.json { render json: @post.errors, status: :unprocessable_entity }
          end
        end
      end

links_controller:

class LinksController < ApplicationController
  before_action :set_link, only: [:show, :edit, :update, :destroy]

  def index
    @links = Link.all
  end

  def show
  end

  def new
    @post = Post.find(params[:post_id])
    @link = @post.links.new
  end

  def edit
  end

  def create
    @post = Post.find(params[:post_id])
    @link = @post.links.new(link_params)

    respond_to do |format|
      if @link.save
        format.html { redirect_to post_path(@post,@link), notice: 'Link was successfully created.' }
        format.json { render :show, status: :created, location: @link }
      else
        format.html { render :new }
        format.json { render json: @link.errors, status: :unprocessable_entity }
      end
    end
  end

我在 / posts /:id 中获得 @ post.links ,我已在views / posts / show.html.erb上写过:

<%= @post.links %>

我收集了空参数:

#<ActiveRecord::Associations::CollectionProxy 
[#<Link id: nil, url: nil, created_at: nil, updated_at: nil, post_id: 6>]

为什么吗

1 个答案:

答案 0 :(得分:0)

据我所知,你不要在控制器的任何地方定义Dim sh as Worksheet, sh2 as Worksheet, r as Long Set sh = Sheets("Sheet1") Set sh2 = Sheets("Sheet2") With sh r = .Cells(1, i).Value2 + 1 .Cells(i, 7).Value = sh2.Cells(r, 4).Value2 .Cells(i, 15).Value = sh2.Cells(r, 5).Value2 End With 。你需要做这样的事情

link_params