【发布时间】:2016-09-01 21:51:11
【问题描述】:
我有一个带有 2 个控制器 instructors 和 requests 的小型 Rails 站点。 instructors 只能由站点管理员访问以管理和管理它们(所有方法都通过设备进行身份验证)。第二个控制器 instructors 是访问访问站点以查看列出的讲师并为他们选择的讲师创建请求的方式。我在创建应保存 name、phone 和 email message 以及从请求显示页面中选择 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
【问题讨论】:
-
显示错误内容比转储代码更有帮助。请注意,您的代码中没有任何地方调用
.model_name -
你能把错误贴在这里吗?
-
没有代码可以在请求和讲师之间建立关联。您还将
@instructor传递给simple_form_for,您似乎打算在其中创建Request而不是Instructor。请更清楚错误消息和触发它的操作。 -
@Pramod 目前我有这个 { :action => "create" }) 做|f| %> 它没有返回错误,但没有提交 instructor_id 。
-
@Owen 没错。我正在传递 @instructor 和 { :action => "create" }) do |f | %> 但它仍然没有提交它。我现在做错了什么?
标签: ruby-on-rails ruby