Home > front end >  Rails app: I am having trouble creating joint instance
Rails app: I am having trouble creating joint instance

Time:05-03

I want to create a simple app where admins can post their job offers and regular users can apply to those job offers. For that I have created a joint table:

My user model:

class User < ApplicationRecord
  before_save { self.email = email.downcase }

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :user_offers, dependent: :destroy
  has_many :offers, through: :user_offers, dependent: :destroy
  # Validations
  validates :name, :surname,  presence: true, length: { maximum: 50 }

end

My offer model:

class Offer < ApplicationRecord
  has_many :user_offers, dependent: :destroy
  has_many :users, through: :user_offers, dependent: :destroy
  validates :name, :number_of_candidates, :number_of_steps, presence: true
end

The model that links both:

class UserOffer < ApplicationRecord
  belongs_to :user
  belongs_to :offer
  has_many :steps, dependent: :destroy
  has_many :comments, through: :steps, dependent: :destroy
end

I want to be able to have a button in each offer's show page where it says "apply". When I click the button, I want the user-offer instance to be created, with the corresponding user (current_user) and offer (params[:id]). My problem is that I dont know how to connect the Apply button with the corresponding controller:

The SHOW in the offers controller:

def show
    @offer = Offer.find(params[:id])

    # The button "apply" will be here.
    # We need to create a new useroffer and steps
    @user_offer = UserOffer.new
    @step = Step.new

  end

The CREATE in the UserOffers controller:

def create
    # This method will be accessed from the Offers Show page.
    # The params[:id] show the offer id
    # First, we get the corresponding offer:
    offer = Offer.find(params[:id])
    # Then, we asign the attributes of the new UserOffer:
    @user_offer = UserOffer.new
    @user_offer.user = current_user # setting the user of the useroffer
    @user_offer.offer = offer # setting the offer of the useroffer
    @user_offer.date_of_application = Date.today
    @user_offer.date_of_hiring = nil
    if @user_offer.save
      flash[:success] = "Congratulations on your application!"
      create_steps(@user_offer)
      # REDIRECT TO MY APPLICATIONS --> The route is not there yet...
    else
      flash[:danger] = "There was a problem with your application."
  end

The view of the show:

<% unless current_user.is_admin %>
  <% if current_user.offers.include?(@offer) %>
    <%= render 'offers/unapply' %>
  <% else %>
    <%= render 'offers/apply' %>
  <% end %>
<% end %>

The _apply.html.erb partial:

<%= simple_form_for [@offer, @user_offer] do |f| %>
  <%= f.button :submit %>
<% end %>

I am having a lot of trouble with this simple issue...

CodePudding user response:

I think you are missing a route to the UserOffers Create action

post 'offers/:id/apply', to: 'user_offers#create', :as => 'apply_for_offer'

Then in apply.html.erb partial you need something like

<%= simple_form_for ([@offer, @user_offer], method: :post, url: apply_for_offer_path) do |f| %>
  <%= f.button :submit %>
<% end %>
  • Related