i have little rails site 2 controllers instructors , requests. instructors accessible site admin manage , manage them (all methods authenticated device). second controller instructors how visits access site view listed instructors , create requests instructor choose. i'm having trouble creating new requests should save name, phone, , email message chosen instructor_id request show page. hope description clear.
thanks.
requests controller
class requestscontroller < applicationcontroller def index if params[:search].present? @instructors = instructor.near(params[:search], 50) else # shows listed instructors created date. @instructors = instructor.order('created_at desc') end end def show @instructor = instructor.find(params[:id]) end def new @request = request.new end def create @request = request.new(request_params) #@request = request.new(request_params) if @request.save flash[:success] = "request has been submited!" else flash[:alert] = "something went wrong!" end end private def request_params params.require(:request).permit(:name, :email, :phone, :message) end end
requests show
<header><h1 class="display-4"><%= @instructor.name %></h1></header> <h3>address: <%= @instructor.address %></h3> <h3>car model: <%= @instructor.car %></h3> <hr> <%= simple_form_for(@instructor, :url =>{ :action => "create" }) |f| %> <%= f.input :name, label: "your name" %> <%= f.input :email %> <%= f.input :phone, label: "phone number" %> <%= f.input :message, as: :text %> <br> <%= f.button :submit, class: "btn btn-danger" %> <% end %> <br> <br>
instructors controller
class instructorscontroller < applicationcontroller # admin required login access action on controller before_action :authenticate_admin! # except: [:action, :action] unlock action def index @instructors = instructor.order('created_at desc') end def show find_instructor end def new @instructor = instructor.new end def update find_instructor if @instructor.update(instructor_params) redirect_to @instructor flash[:notice] = "instructor changes has been saved." else render 'edit' end end def create @instructor = instructor.new(instructor_params) if @instructor.save flash[:notice] = "instructor has been saved!" redirect_to instructors_path else render 'new' flash_error('new instructor hasn\'t been saved.') end end def edit find_instructor end def destroy find_instructor if @instructor.destroy redirect_to root_path flash[:notice] = "the instructor has been deleted." else flash_error('instructor hasn\'t been deleted.') end end private def flash_error(message) # takes of error message argument flash[:alert] = "something went wrong!, #{message} please make sure submitting valid data , try again" end def find_instructor @instructor = instructor.find(params[:id]) end def instructor_params params.require(:instructor).permit(:name, :car, :address, :manual, :auto) end end flash_error('instructor hasn\'t been deleted.') end end private def flash_error(message) # takes of error message argument flash[:alert] = "something went wrong!, #{message} please make sure submitting valid data , try again" end def find_instructor @instructor = instructor.find(params[:id]) end def instructor_params params.require(:instructor).permit(:name, :car, :address, :manual, :auto) end end
instructor model
class instructor < applicationrecord has_many :requests geocoded_by :address after_validation :geocode, if: ->(obj){ obj.address.present? , obj.address_changed? } end
request model
class request < applicationrecord belongs_to :instructor end
i believe error coming simple_form_for
in show view because @instructor
nil.
you got variable from:
@instructor = instructor.find(params[:id])
you should make test making sure there exists instructor id sent params.
Comments
Post a Comment