参数缺失错误(rails)

时间:2015-04-02 23:40:02

标签: ruby-on-rails ruby ruby-on-rails-4

我遇到一个错误,当我尝试调用函数finalized_request时,它会抛出一个错误,说“param丢失或值为空:finalizedeal”。由于我是新手,我无法弄清楚我做错了什么(我是ROR的新手)。

Request_controller.rb

class RequestsController < ApplicationController
  before_action :set_request, only: [:show, :edit, :update, :destroy]

  # GET /requests
  # GET /requests.json
  def index
    @categories_list = Category.getAll()
  end
  def active
    user      = session[:user] 
    @requests = Array.new
    @tag = Array.new
    @requests = Request.getRequestByUser(user)
      @requests.each.with_index do |request, index|
      if request != nil
        @tag[index] = Array.new
        request[:tag_id].each do |t|
          @tag[index] << Tag.getTag(t)
        end
      end
    end
  end
  # GET /requests/1
  # GET /requests/1.json
  def show
    @user     = User.getUser(@request[:user_id])
    @tag = Array.new
    @request[:tag_id].each do |cate|
      @tag << Tag.getTag(cate)
    end
  end

  # GET /requests/1/edit
  def edit
    @tag = Array.new
    @request[:tag_id].each do |cate|
        @tag << Tag.getTag(cate)
    end
  end
  # POST /requests
  def post_request
    tags_arr         = params[:tags] ;
    #=begin
      #categories   = Array.new ;
      #if tags != nil
      #  tags.each do |tag|
      #    category  = Category.createCategoryIfNotExist(tag)
      #    if(category != nil)
      #      categories << category[:_id] 
      #    end
      #  end
      #end
    #=end
    tags   = Array.new ;
    if tags_arr != nil
      tags_arr.each do |t|
        tag  = Tag.createTagIfNotExist(t)
        if(tag != nil)
          tags << tag[:_id] 
        end
      end
    end
    request_data                = request_params
    user_id                     = session[:user] 
    request_data[:tag_id]  = tags 
    request_data[:user_id]      = user_id
    @request_                   = Request.createRequest(request_data)
    if @request_
      flash[:notice] = "Request Post successfully."
      redirect_to :action => "active"
    end


  end


  # PATCH/PUT /requests/1
  # PATCH/PUT /requests/1.json
  def update
    #tags         = params[:tags] ;
    #categories   = Array.new ;
    #if tags != nil
    #  tags.each do |tag|
    #    category  = Category.createCategoryIfNotExist(tag)
    #    if(category != nil)
    #      categories << category[:_id] 
    #    end
    #  end
    #end
    tags_arr         = params[:tags] ;
    tags   = Array.new ;
    if tags_arr != nil
      tags_arr.each do |t|
        tag  = Tag.createTagIfNotExist(t)
        if(tag != nil)
          tags << tag[:_id] 
        end
      end
    end
    Rails.logger.info("RequestsParams: #{request_params.inspect}")
    request_data                = request_params
    if request_data[:is_service] != "on"
       request_data[:is_service] = "off" 
    end
    user_id                     = session[:user] 
    request_data[:tag_id]  = tags 
    request_data[:user_id]      = user_id
    if Request.updateRequest(@request,request_data)
      flash[:notice] = "Request has been Edited successfully."
      redirect_to :action => "active"
    end
  end

  def delete_request ()
    if Request.delete_request(params[:id])
      flash[:notice] = "Request has been Deleted successfully."
      render :json => "great"
    end
  end

  # GET /requests
  def finalize_request()
    finalizedrequest = finalizedRequest_params
    request = Request.getRequest(finalizedrequest[:title])
    finalizedrequest[:title] = request[:title]
    Request.delete_request(request[:_id])
    FinalizedDeal.createFinalizedRequest(finalizedrequest)

    redirect_to :action => "bookmark"
  end 

  # GET /requests
  def bookmark
    user      = session[:user] 
    @requests = Array.new
    @tag = Array.new
    @requests = Request.getRequestByUser(user)
      @requests.each.with_index do |request, index|
      if request != nil
        @tag[index] = Array.new
        request[:tag_id].each do |t|
          @tag[index] << Tag.getTag(t)
        end
      end
    end
  end 



  # GET /requests
  def bookmark_request
    data = params[:d]
    bookmarked_against_Request = Request.getRequest(1)
    request_bookmarked = Request.getRequest(data)
    request_bookmarked_2 = request_bookmarked
    bookmarked_against_Request_2 = bookmarked_against_Request
    Rails.logger.info("Bookmark 2: #{bookmarked_against_Request_2.inspect}")
    #bookmarked_against_Request_2[:favourites] << request_bookmarked[:id]
    #request_bookmarked_2[:favourites_of] << bookmarked_against_Request[:id]
    #hello
    #Request.updateRequest(request_bookmarked , request_bookmarked_2)
    #Request.updateRequest(bookmarked_against_Request , bookmarked_against_Request_2)
    redirect_to :action => "bookmark"
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_request
      @request = Request.getRequest(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def request_params
      params.require(:request).permit(:title, :description, :type , :is_service , :required_user_location , :required_product_location ,:Upper_price_range , :lower_price_range , :negotiable , :type , :tags , :category_id)
    end

    def finalizedRequest_params
      params.require(:finalizedeal).permit(:title , :description)
    end
end

finalized_deal.rb

class FinalizeDeal
  include Mongoid::Document

  field :deal_details, type: String
  field :categories, type: Array
  field :owner_user, type: MongoId
  field :corsponing_user, type: MongoId

  field :title, type: String

  field :corresponding_product, type: String
  field :meeting_date, type: String
  field :date_finalized, type: String
  field :description, type: String

class << self
    def getRequestByUser(user_id)
      requests = where(user_id: user_id).to_a
      if requests
        requests
      end
    end

    def getFinzlizedRequest(req)
      request = find(req)
      if request
        request
      end
    end
    def createFinalizedRequest(req_data)
      request = self.new(req_data)
      if request.save
        request
      end
    end
    def updateFinalizedRequest(request,req_data)
      if request.update(req_data)
        request
      end
    end
    def delete_FinalizedRequest(req_id)
      if find(req_id).delete
        true
      end
    end
  end

end

request.html.erb

div id="form-details-modal-lbms" class="reveal-modal" data-reveal>
      <h3>Enter Contract Details:</h3>
      <!--<form>-->
      <%= form_tag({controller: "requests", action: "finalize_request"}, method: "GET",id:"post-form-lbms" ,data: {abide: ''}) %>
        <input type="hidden" id="currect_opened_req_id" value="" name="FinalizeDeal[title]"/>
        <select name="meeting-id">
          <option value="1">Meeting 1</option>
          <option value="2">Meeting 2</option>
        </select>
        <label for="details-lbms">Details</label>
        <textarea id="details-lbms" name="FinalizeDeal[description]"></textarea>
        <button class="button tiny">
          Submit
        </button>
      </form>
      <a class="close-reveal-modal">&#215;</a>
    </div>

请告诉我我做错了什么。我还发布了错误屏幕截图的链接

http://tinypic.com/r/n6t8w2/8
http://tinypic.com/r/33kdq1k/8

1 个答案:

答案 0 :(得分:1)

代码是抱怨的,因为您需要:finalizedeal参数(但显然您没有将其传递),只需将此.require(:finalizedeal)添加到此代码中:

def finalizedRequest_params
  params.require(:finalizedeal).permit(:title , :description)
end

因此,一种解决方案是简单地删除require部分。像这样:

params.permit(:title , :description)

#require source

  

确保存在参数。