我正在使用rails 4中的一个项目,我正在创建一些动态下拉列表。以下是我为此下拉菜单所做的所有代码。下降正在出现,但不会保存到表中并且不会出现在Show中(是的,我有正确的ruby即{。{1}}。
我不知道该怎么做,我是一个铁杆菜鸟,但已经通过语言的教程和课程。
我的表格选择框看起来像这样:
<%= @reports.site_id %>
上面的选择框剂量从站点表中获取数据,但不会将该数据保存到报告表中?`
我的控制器看起来像这样:
`<%= collection_select( :site, :site_id, Site.all, :id, :site_call_sign, {}, { :multiple => false %>`
我的模型看起来像这样:
class ReportsController < ApplicationController
before_action :set_report, only: [:show, :edit, :update, :destroy]
# GET /reports
# GET /reports.json
def index
@search = ReportDateSearch.new(params[:search])
@reports = @search.scope
@reports = Report.all
# Adds CSV Downloader to Residents
respond_to do |format|
format.html
format.csv { render text: @reports.to_csv }
end
end
# GET /reports/1
# GET /reports/1.json
def show
end
# GET /reports/new
def new
@report = Report.new
end
# GET /reports/1/edit
def edit
end
# POST /reports
# POST /reports.json
def create
@report = Report.new(report_params)
respond_to do |format|
if @report.save
format.html { redirect_to @report, notice: 'Report was successfully created.' }
format.json { render :show, status: :created, location: @report }
else
format.html { render :new }
format.json { render json: @report.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reports/1
# PATCH/PUT /reports/1.json
def update
respond_to do |format|
if @report.update(report_params)
format.html { redirect_to @report, notice: 'Report was successfully updated.' }
format.json { render :show, status: :ok, location: @report }
else
format.html { render :edit }
format.json { render json: @report.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reports/1
# DELETE /reports/1.json
def destroy
@report.destroy
respond_to do |format|
format.html { redirect_to reports_url, notice: 'Report was successfully destroyed.' }
format.json { head :no_content }
end
end
# Adds CSV Uploader Method to Application
def import
Report.import(params[:file])
redirect_to reports_path, notice: "Report(s) Added Successfully"
end
private
# Use callbacks to share common setup or constraints between actions.
def set_report
@report = Report.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def report_params
params.require(:report).permit(:date, :site_id, :user_id, :type_of_report, :type_of_incident, :report)
end
end
我的展示页面看起来像:
class Report < ActiveRecord::Base
belongs_to :user
belongs_to :site
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Report.create! row.to_hash
end
end
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |report|
csv << report.attributes.values_at(*column_names)
end
end
end
end
不太确定我在哪里出错了。非常感谢任何帮助
答案 0 :(得分:1)
如果您的collection_select从:site
致电:report
,请尝试更改第一个参数:
<%= collection_select( :report, :site_id, Site.all, :id, :site_call_sign, {}, { :multiple => false } %>
我最初的猜测是,您的表单将site_id作为params[:site_id]
而不是params[:report][:site_id]
传递,这是您根据控制器中的强参数方法所需的格式:
def report_params
params.require(:report).permit(:date, :site_id, :user_id, :type_of_report, :type_of_incident, :report)
end
如果它不起作用,你可以做的一件事就是查看你的rails控制台,看看你的表单参数从视图发送到控制器的格式。在您的操作中添加一些声明可以帮助您更好地比较params
与report_params
:
def create
puts "Params: #{params.inspect}"
puts "Report Params: #{report_params.inspect}"
@report = Report.new(report_params)
respond_to do |format|
if @report.save
format.html { redirect_to @report, notice: 'Report was successfully created.' }
format.json { render :show, status: :created, location: @report }
else
format.html { render :new }
format.json { render json: @report.errors, status: :unprocessable_entity }
end
end
end
在服务器控制台中查找这两个put语句的输出。如果您在params而不是report_params中看到site_id,则您会知道您的强参数方法已将其过滤掉。