繁体   English   中英

Rails 4如何从另一个控制器中获取ID

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM