We have a form to create appointments with a branch, this form needs to save the id of the current user and the id of the branch that the appointment is with. However, we can't figure out how to get the branch_id to be saved.
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 %>
Forget about "other controllers". You send some values through in params and use those values to make your @visit object. All you need in your controller's create action is this:
@visit = current_user.visits.new(visit_params)
if @visit.save
...etc
All the data you're going to use to build the @visit object can come through in params. If you want to set the branch_id, just make sure that params[:visit][:branch_id]
is set to the right value, from your form.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.