我有一个小Rails站点,有2个控制器讲师和请求。站点管理员只能访问讲师以管理和管理它们(所有方法都使用设备进行身份验证)。第二个控制器讲师是访问访问站点以查看列出的教师并为他们选择的教师创建请求的方式。我在创建新请求时遇到问题,这些新请求应该保存名称,手机和电子邮件 消息作为从请求显示页面中选择的 instructor_id 。我希望我的描述清楚。
感谢。
class RequestsController < ApplicationController
def index
if params[:search].present?
@instructors = Instructor.near(params[:search], 50)
else
# Shows all listed instructors by the 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 is went wrong!"
end
end
private
def request_params
params.require(:request).permit(:name, :email, :phone, :message)
end
end
<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" }) do |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>
class InstructorsController < ApplicationController
# Admin is required to login to access any action on this controller
before_action :authenticate_admin! # except: [:action, :action] to unlock any 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 the of the error message as an argument
flash[:alert] = "Something went wrong!, #{message}
Please make sure you submitting valid data and 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 the of the error message as an argument
flash[:alert] = "Something went wrong!, #{message}
Please make sure you submitting valid data and 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
class Instructor < ApplicationRecord
has_many :requests
geocoded_by :address
after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
end
class Request < ApplicationRecord
belongs_to :instructor
end
答案 0 :(得分:-2)
我认为错误来自展示视图中的simple_form_for
,因为@instructor
为零。
你得到的变量是:
@instructor = Instructor.find(params[:id])
你应该做一个测试,确保存在一个带有params发送的id的教师。