用模态更改link_to

时间:2016-04-04 13:46:24

标签: ruby-on-rails bootstrap-modal link-to simple-form-for

我正在开展一个项目,人们可以在工作中雇用一个活动。 实际上他们只能“假设”但我不想添加他们可以在申请时加价的选项。

所以这是我的古代代码[工作]:

<% 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 %>

但是我在自动化方面有错误,也许你可以帮我用simple_form_for切换这个link_to。

提前谢谢。

编辑:消息错误You are not authorize to perform this action +终端

    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

POSTULANT CONTROLLER

    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

1 个答案:

答案 0 :(得分:0)

尝试将表单更改为 -

<%= 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 %>