PortfoliosController #create中的ActiveRecord :: AssociationTypeMismatch

时间:2018-05-07 09:01:24

标签: ruby-on-rails ruby controller

我正在努力解决这个错误,这是我的控制器代码

numpy.fromstring(line[1:-2], dtype=float, sep=' ')

我真的很喜欢编码,我不确定哪个代码对于这个错误至关重要,哪个不是这样我已经在控制器页面中发布了所有代码

 class PortfoliosController < ApplicationController
      before_action :set_portfolio, only: [:show, :edit, :update, :destroy]
      before_action :authenticate_user!, except: [:index]

      # GET /portfolios
      # GET /portfolios.json
      def index
        @portfolios = Portfolio.all
      end

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

我只是将此评论放在这里以分解代码,以便我可以发布

      # GET /portfolios/new
      def new
        @portfolio = Portfolio.new
      end

      # GET /portfolios/1/edit
      def edit
      end

      # POST /portfolios
      # POST /portfolios.json
      def create
        # init_params = portfolio_params
        # init_params[:user] = current_user.id
        puts params
        @portfolio = Portfolio.new(portfolio_params(params[:portfolio]))

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

我已经玩了一下这个最后一节,改变了:user to:user.id但是没有帮助,我也改变了:design to:design_name

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

      # DELETE /portfolios/1
      # DELETE /portfolios/1.json
      def destroy
        @portfolio.destroy
        respond_to do |format|
          format.html { redirect_to portfolios_url, notice: 'Portfolio was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

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

嘿抱歉!完整的错误代码是:

        def portfolio_params(params)
          # params = params[:portfolio]
          params.permit(:design_name, :user)

          # user.id was changed back to user
        end
    end

1 个答案:

答案 0 :(得分:0)

您以两种不同的方式致电portfolio_params。在update中,您没有传递参数;在create中,您传递参数。通常的方法是不传递任何参数。

此外,您允许使用user参数,但似乎您使用的是current_user,因此user不是必需的。

def portfolio_params
  params.require(:portfolio).permit(:design_name)
end

并且一言不发地说:

@portfolio = current_user.portfolios.new(portfolio_params)
#I'm assuming a user has_many portfolios and a portfolio belongs_to user and has a user_id field in the database.