我们有一个表单来创建一个带分支的约会,这个表单需要保存当前用户的id和约会所在的分支的id。但是,我们无法弄清楚如何保存branch_id。
visits_controller.rb:
class VisitsController < ApplicationController
before_filter :authenticate_user!, except: [ :new]
before_action :set_visit, only: [:show, :edit, :update, :destroy]
# GET /visits
# GET /visits.json
def index
@visits = Visit.all
@visits_by_date = @visits.group_by(&:date_from)
@date = params[:date] ? Date.parse(params[:date]) : Date.today
@users = User.all
end
# GET /visits/1
# GET /visits/1.json
def show
end
# GET /visits/new
def new
@visit = Visit.new
@branch = Branch.all
end
# GET /visits/1/edit
def edit
end
# POST /visits
# POST /visits.json
def create
@visit = Visit.new(visit_params)
@visit.branch_id = Branch.where(:id => params[:branch_id])
@visit.user_id = current_user.id if current_user
respond_to do |format|
if @visit.save
format.html { redirect_to @visit, notice: 'Visit was successfully created.' }
format.json { render :show, status: :created, location: @visit }
else
format.html { render :new }
format.json { render json: @visit.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /visits/1
# PATCH/PUT /visits/1.json
def update
respond_to do |format|
if @visit.update(visit_params)
format.html { redirect_to @visit, notice: 'Visit was successfully updated.' }
format.json { render :show, status: :ok, location: @visit }
else
format.html { render :edit }
format.json { render json: @visit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /visits/1
# DELETE /visits/1.json
def destroy
@visit.destroy
respond_to do |format|
format.html { redirect_to visits_url, notice: 'Visit was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_visit
@visit = Visit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def visit_params
params.require(:visit).permit(:location, :date_from, :time_from, :date_to, :time_to, :comment, :branch_id, :user_id)
end
end
visit.rb:
class Visit < ActiveRecord::Base
belongs_to :branch
belongs_to :user #:as => 'created_by'
validates_uniqueness_of :time_from, :scope => [:date_from, :location], :message=>"slot is already taken on selected date"
end
new.html.erb:
<h1>New Visit</h1>
<%= render 'form' %>
<%= link_to 'Back', visits_path %>
_form.html.erb:
<% if user_signed_in? && current_user.user_type == 'client' %>
<%= form_for @visit do |f| %>
<% if @visit.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@visit.errors.count, "error") %> prohibited this visit from being saved:</h2>
<ul>
<% @visit.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :location, "Please Pick Service Provider/Branch you would like to visit:" %><br>
<%= f.collection_select(:location, Branch.all, :branch_name, :branch_name_select, {prompt: "Select the Branch/Service"}, {:required => true}) %>
</div>
<div class="field">
<%= f.label :date_from, "Please Pick Visit Date From:" %><br>
<%= f.text_field :date_from, :required => true %>
</div>
<div class="field">
<%= f.label :time_from, "Please Pick Visit Start Time:" %><br>
<%= f.time_select :time_from, {:start_hour => 9, :end_hour => 20, :minute_step => 15, :ampm => true}, :required => true %>
</div>
<div class="field">
<%= f.label :date_to, "Please Pick Visit Date From:" %><br>
<%= f.text_field :date_to %>
</div>
<div class="field">
<%= f.label :time_to, "Please Pick Visit End Time:" %><br>
<%= f.time_select :time_to, {:start_hour => 9, :end_hour => 20, :minute_step => 15, :ampm => true} %>
</div>
<div class="field">
<%= f.label :comment, "If you have any specific requirements for your visit please let us now and leave the comment below:" %><br>
<%= f.text_area :comment %>
</div>
<div class="field">
<%= f.label :branch_id %><br>
<%= f.hidden_field :branch_id %>
</div>
<div class="field">
<%= f.label :user_id %><br>
<%= f.hidden_field :user_id, :value => current_user.id %>
</div>
<div class="actions">
<%= f.submit "Submit Visit Request"%>
</div>
<% end %>
<% end %>
答案 0 :(得分:0)
忘记“其他控制器”。您可以在参数中发送一些值,并使用这些值来制作@visit对象。您在控制器的创建操作中所需要的只是:
@visit = current_user.visits.new(visit_params)
if @visit.save
...etc
您将用于构建@visit对象的所有数据都可以通过params获得。如果您想设置branch_id,请确保从表单中将params[:visit][:branch_id]
设置为正确的值。