在同一视图中为模态定义为变量

时间:2017-01-27 19:23:26

标签: javascript ruby-on-rails ruby

我目前正面临一个愚蠢的问题,我想要一个按钮新帖子和另一个用于编辑这篇文章。新操作没问题,但我无法访问我的编辑视图。该操作正在呈现新表单,而不是编辑或更新。

我的代码:

(新按钮)=

<button type='button' class='btn btn-default' data-toggle='modal' data-target='#MyNewStaff' aria-hidden="true" remote="true"><i class="fa fa-user-plus"></i></button>

(新模态)=

 <div id='MyNewStaff' class='modal fade' role='dialog' aria-hidden="true">
      <div class="modal-dialog">
        <div class='content'>
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      <h3 class="modal-title">New Staff</h3>
          </div>
          <div class="modal-body">
            <%= render 'form'%>

          </div>
        </div>
      </div>
    </div>

*****现在问题(编辑按钮)= ********

<button type='button' class="staff_icon" data-toggle='modal' data-target='#MyEditStaff' aria-hidden="true" remote="true" style="background-color:<%=staff.color%>; margin-top:5px; max-heigth: 10px;"><strong><p><%= staff.name[0]%></p></strong></i> 

(编辑行动)=

<div id='MyEditStaff' class='modal fade' role='dialog' aria-hidden="true">
      <div class="modal-dialog">
        <div class='content'>
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      <h3 class="modal-title">Edit Staff</h3>
          </div>
          <div class="modal-body">
            <%= render 'form'%>
          </div>
        </div>
      </div>
    </div>

(控制器)=

class StaffsController < ApplicationController
  before_action :set_staff, only: [:show, :edit, :update, :destroy]

  # GET /staffs
  # GET /staffs.json
  def index
    @staffs = current_user.staffs
    @staff = Staff.new
  end

  # GET /staffs/1
  # GET /staffs/1.json
  def show
  end

  # GET /staffs/new
  def new
    @staff = Staff.new
  end

  # GET /staffs/1/edit
  def edit
  end

  # POST /staffs
  # POST /staffs.json
  def create
    @staff = Staff.new(staff_params)

    respond_to do |format|
      if @staff.save
        @staff.update(user_id: current_user.id)
        format.html { redirect_to staffs_path, notice: 'Staff was successfully created.' }
        format.json { render :show, status: :created, location: @staff }
      else
        format.html { render :new }
        format.json { render json: @staff.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /staffs/1
  # PATCH/PUT /staffs/1.json
  def update
    respond_to do |format|
      if @staff.update(staff_params)
        format.html { redirect_to @staff, notice: 'Staff was successfully updated.' }
        format.json { render :index, status: :ok, location: @staff }
      else
        format.html { render :edit }
        format.json { render json: @staff.errors, status: :unprocessable_entity }
      end
    end
  end



  private
    # Use callbacks to share common setup or constraints between actions.
    def set_staff
      @staff = Staff.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def staff_params
      params.require(:staff).permit(:name, :user_id, :color)
    end
end

提前致谢!

0 个答案:

没有答案