show action中没有路由匹配 - 缺少必需的键:[:id]

时间:2017-01-03 01:11:09

标签: ruby-on-rails parameters

我看了几个类似于这个问题的不同答案,但是他们都将id传递给params哈希,它只是作为零值返回。我似乎无法将id列入白名单,我无法弄清楚原因。

这是我的创建和展示动作,还包括参数。我明确地将id列入白名单,虽然我不知道是否有必要。

  def show
    @board = Board.find(params[:id])
  end

  def create
    @board = Board.new(board_params)

    if @board.save
      # flash[:notice] = 'You successfully created your post!'
      redirect_to board_path
    else
      render 'new'
      # flash[:danger] = 'One or more errors occurred when processing your post'
    end
  end

  private

  def board_params
    # params.require(:board).permit!
    params.require(:board).permit(:id, :project_name, :project_description, :postition_title, :project_url, :project_headquarters, :application_process)
  end

我的路线输出显示动作

board GET /boards/:id(.:format) boards#show

params hash的输出在#draw

Parameters: {"utf8"=>"✓", "authenticity_token"=>"k+KKRhBk4Dnba1vxtFCanI2grfhNXFSgJpfEBLnhPdablfOsXRi1wBehPpM1qgx1pySrqxVHTeiwkneluXwRIQ==", "board"=>{"project_name"=>"sdf", "postition_title"=>"sdf", "project_headquarters"=>"sdf", "project_url"=>"fsd", "project_description"=>"dfs", "application_process"=>"dfs"}, "commit"=>"Create Board"}

任何人都可以看到我需要更改的内容才能正确传入ID吗?

2 个答案:

答案 0 :(得分:1)

试试这个。

Take:id of params。

def show
end

def create
    @board = Board.new(board_params)

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

答案 1 :(得分:0)

create行动......

更改..

redirect_to board_path

以...

redirect_to board_path(@board)
###OR
### this calls the show action
redirect_to @board