我正在使用html / css模板制作应用程序。当我添加单选按钮时 - 它们看起来效果很好,但在创建模型后 - 所选属性为nil
。所有属性都是布尔值,默认为false。
装运控制器:
class ShipmentsController < ApplicationController
helper_method :shipment, :user
before_action :set_shipment, only: [:show]
before_action :authenticate_user!
before_action :require_same_user, only: [:show]
def index
@shipments = Shipment.all
end
def new
@shipment = Shipment.new
end
def create
@shipment = Shipment.new(shipment_params)
@shipment.user = current_user
if @shipment.save
flash[:success] = "Shipment added."
redirect_to shipment_path(@shipment)
else
render 'new'
end
end
def show
@shipment = Shipment.find(params[:id])
end
private
def user
@user = current_user
end
def shipment
@shipment = user.shipments.new
end
def shipment_params
params.require(:shipment).permit(:name, :three_days,
:seven_days, :two_weeks)
end
def set_shipment
@shipment = Shipment.find(params[:id])
end
def require_same_user
if current_user != @shipment.user
flash[:alert] = "Restricted."
redirect_to root_path
end
end
end
装运表格:
<%= form_for(shipment, html: { multipart: true }) do |f| %>
<%= f.radio_button :three_days, "3", type: 'radio', name: 'demo-priority' %>
<%= f.label :three_days, "3 Days", :value => "3" %>
<%= f.radio_button :seven_days, "7", type: 'radio', name: 'demo-priority' %>
<%= f.label :seven_days, "7 Days", :value => "7" %>
<%= f.radio_button :two_weeks, "14", name: 'demo-priority' %>
<%= f.label :two_weeks, "14 Days", :value => "14" %>
<% end %>
有什么想法吗?