簡體   English   中英

用模態更改link_to

[英]change a link_to with a modal

我正在開展一個項目,人們可以在工作中雇用一個活動。 實際上他們只能“假設”但我不想添加他們可以為他們的應用程序添加價格的選項。

所以這是我的古代代碼[工作]:

<% if @current_user != @project.user %>
    <% if project_job.users.find { |user| user == current_user } %>
     <%= link_to "Retirer ma candidature", project_project_job_postulant_path(@project, project_job, project_job.postulants.find_by_user_id(current_user.id)), method: :delete, class:"btn btn-primary btn-prostate" %>
    <% else %>
     <%= link_to "Postuler", project_project_job_postulants_path(@project, project_job), method: :post, class:"btn btn-primary btn-prostate" %>
    <% end %>
  <% end %>

我想做這樣的事情:

<% if @current_user != @project.user %>
  <% if project_job.users.find { |user| user == current_user } %>
    <%= link_to "Retirer ma candidature", project_project_job_postulant_path(@project, project_job, project_job.postulants.find_by_user_id(current_user.id)), method: :delete, class:"btn btn-primary btn-prostate" %>
  <% else %>
    <div class="modal-content">
      <%= simple_form_for [@project, @project_job] do |f| %>
        <%= f.input :budget, required: true, autofocus: true, placeholder: "Budget moyen par personne. En Euros - €"%>
        <%= f.button :submit, value: "Add", class: "btn btn-primary" %>
      <% end %>
    </div>
  <% end %>
<% end %>

但我在autorisation上有一個錯誤,也許你可以幫我用simple_form_for切換這個link_to。

先感謝您。

編輯:消息錯誤您沒有授權執行此操作 +終端

    Started POST "/projects/1/project_jobs" for ::1 at 2016-04-04 16:39:41 +0200
    Processing by ProjectJobsController#create as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"Rs6pNdhJyuqfTQWzX2HrBzYRB+dBR3g2ZAOEoqA45pBg+zEHep79ZRTiFvz3HPZkysqUa1vqHcLDZ6neFQiPvQ==", "project_job"=>{"budget"=>"100"}, "commit"=>"Add", "project_id"=>"1"}
      User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 11]]
      Project Load (2.1ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1  [["id", 1]]
      User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
    Redirected to http://localhost:3000/
    Completed 302 Found in 10ms (ActiveRecord: 2.7ms)

路線:

    Rails.application.routes.draw do
      get 'project_jobs/Postulants'
      root to: 'pages#home'
      get '/about', to: 'pages#about'
      get '/manager', to: 'pages#manager'

      devise_for :users, controllers: { registrations: 'users/registrations', omniauth_callbacks: 'users/omniauth_callbacks' }
      # , controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
      resources :users, only: [ :edit, :update, :show, :manager ] do
        resources :skills, only: [ :edit, :create, :show, :destroy ]
      end

      resources :projects , only: [:new, :create, :show, :edit, :destroy, :update, :index] do
        resources :project_jobs , only: [:show, :create, :destroy, :index, :new ] do
          resources :postulants , only: [:show, :destroy, :index, :create]
        end
      end

      post 'projects/:project_id/postulants/:id/accepted', to: 'postulants#accepted', as: 'accepted'
      post 'projects/:project_id/postulants/:id/rejected', to: 'postulants#rejected', as: 'rejected'
      post 'projects/:id/publish', to: 'projects#publish', as: 'publish'


    end

MODEL PROJECT_JOB

class ProjectJob < ActiveRecord::Base
  belongs_to :project
  belongs_to :job

  has_many :postulants, dependent: :destroy
  has_many :users, through: :postulants

  validates :project_id, presence: true
  validates :number, :numericality => { :greater_than => 0 }
  validates :job, presence: true
end

模型發布者

    class Postulant < ActiveRecord::Base
      belongs_to :project_job
      belongs_to :user

      validates :user_id, presence: true, uniqueness: { scope: :project_job,
        message: "You already apply to this job" }

    end

PostulantPolicy

    class PostulantPolicy < ApplicationPolicy
      class Scope < Scope
        def resolve
          scope
        end
      end

      def create?
        true
      end


      def destroy?
        record.user == user
      end

      def accepted?
        true
      end

      def rejected?
        true
      end
    end

POST警控制器

    class PostulantsController < ApplicationController
      before_action :set_project_job, only: [ :create ]


      def create
        @postulant = @project_job.postulants.build(postulant_params)
        authorize @postulant
        @postulant.save
        redirect_to project_path(@project_job.project)
      end

      def destroy
        @postulant = Postulant.find(params[:id])
        authorize @postulant
        @postulant.destroy
        redirect_to project_path(@postulant.project_job.project)
      end

      def new

      end

      def accepted
        @postulant = Postulant.find(params[:id])
        @postulant.status = true
        @postulant.save
        authorize @postulant
        @project_job = @postulant.project_job
        @project_job.number -= 1
        @project_job.save
        redirect_to :back
      end

      def rejected
        @postulant = Postulant.find(params[:id])
        @postulant.status = false
        @postulant.save
        authorize @postulant
        redirect_to :back
      end


      private

      def set_project_job
        @project_job = ProjectJob.find(params[:project_job_id])
      end


      def postulant_params
        params.require(:postulant).permit(:project_jobs_id, :user_id, :budget)
      end

    end

嘗試將表單更改為 -

<%= simple_form_for [@project, project_job, project_job.postulants.build] do |f| %>
 <%= f.hidden_field :user_id, current_user.id %>
 <%= f.input :budget, required: true, autofocus: true, placeholder: "Budget moyen par personne. En Euros - €"%>
 <%= f.button :submit, value: "Add", class: "btn btn-primary" %>
<% end %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM