user2369480 user2369480 - 5 months ago 12
Ruby Question

param is missing or the value is empty rails controller

I have a 3 views, with create form, but a sample def new def create works good, but other def not works...

RateFormController.rb



class RateFormController < ApplicationController

def new
@rateform = RateForm.new
end

def create
@rateform = RateForm.new(rate_params)
if @rateform.save
redirect_to :root
RateFormMailer.rate_email(@rateform).deliver_now
end
end

def encreate
@rateform = RateForm.new(rate_params)
RateFormMailer.rate_email(@rateform).deliver_now
# ContactsMailer.sample_email(@contact).deliver_now
end

def rucreate
@rateform = RateForm.new(rate_params)
if @rateform.save
redirect_to :root
RateFormMailer.rate_email(@rateform).deliver_now
end
end



private
def rate_params
params.require(:rate_form).permit(:name, :mail, :comment, :ratename, :phone)
end

end


127.0.0.1:3000/rate_form/encreate



ActionController::ParameterMissing in RateFormController#encreate
param is missing or the value is empty: rate_form
private
def rate_params
params.require(:rate_form).permit(:name, :mail, :comment, :ratename, :phone)
end
end

Answer

I think they are empty because you were using GET method for them

 rate_form_encreate_path    GET /rate_form/encreate(.:format)   rate_form#encreate
 rate_form_rucreate_path    GET /rate_form/rucreate(.:format)   rate_form#rucreate

These are your routes of GET type

Please change them to POST type.

match '/rate_form/encreate' => 'rate_form#encreate', via: [:post], :as => :rate_form_encreate
match '/rate_form/rucreate' => 'rate_form#rucreate', via: [:post], :as => :rate_form_rucreate

Updated:

match '/rate_form/ennew' => 'rate_form#ennew', via: [:get], :as => :rate_form_ennew
match '/rate_form/runew' => 'rate_form#runew', via: [:get], :as => :rate_form_ennew
Comments