[英]Rails 4 How to get id from another controller in form
我們有一個使用分支創建約會的表單,該表單需要保存當前用戶的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 %>
忘記“其他控制器”。 您通過params發送一些值,並使用這些值創建@visit對象。 您需要在控制器的create動作中執行以下操作:
@visit = current_user.visits.new(visit_params)
if @visit.save
...etc
您將用於構建@visit對象的所有數據都可以通過params傳遞。 如果要設置branch_id,只需確保從表單params[:visit][:branch_id]
設置為正確的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.