Rails 应用程序:我在创建联合实例时遇到问题
Rails app: I am having trouble creating joint instance
我想创建一个简单的应用程序,管理员可以在其中 post 他们的工作机会,普通用户可以申请这些工作机会。为此,我创建了一个关节 table:
我的用户模型:
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
我的报价型号:
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
链接两者的模型:
class UserOffer < ApplicationRecord
belongs_to :user
belongs_to :offer
has_many :steps, dependent: :destroy
has_many :comments, through: :steps, dependent: :destroy
end
我希望能够在每个报价的显示页面上都有一个按钮,上面写着“申请”。当我单击该按钮时,我希望使用相应的用户 (current_user) 和报价 (params[:id]) 创建用户报价实例。我的问题是我不知道如何将 Apply 按钮连接到相应的控制器:
优惠控制器中的显示:
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
UserOffers 控制器中的 CREATE:
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
演出观点:
<% unless current_user.is_admin %>
<% if current_user.offers.include?(@offer) %>
<%= render 'offers/unapply' %>
<% else %>
<%= render 'offers/apply' %>
<% end %>
<% end %>
_apply.html.erb部分:
<%= simple_form_for [@offer, @user_offer] do |f| %>
<%= f.button :submit %>
<% end %>
我在这个简单的问题上遇到了很多麻烦...
我认为您缺少指向 UserOffers 创建操作的路径
post 'offers/:id/apply', to: 'user_offers#create', :as => 'apply_for_offer'
然后在 apply.html.erb 部分你需要像
<%= simple_form_for ([@offer, @user_offer], method: :post, url: apply_for_offer_path) do |f| %>
<%= f.button :submit %>
<% end %>
我想创建一个简单的应用程序,管理员可以在其中 post 他们的工作机会,普通用户可以申请这些工作机会。为此,我创建了一个关节 table:
我的用户模型:
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
我的报价型号:
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
链接两者的模型:
class UserOffer < ApplicationRecord
belongs_to :user
belongs_to :offer
has_many :steps, dependent: :destroy
has_many :comments, through: :steps, dependent: :destroy
end
我希望能够在每个报价的显示页面上都有一个按钮,上面写着“申请”。当我单击该按钮时,我希望使用相应的用户 (current_user) 和报价 (params[:id]) 创建用户报价实例。我的问题是我不知道如何将 Apply 按钮连接到相应的控制器:
优惠控制器中的显示:
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
UserOffers 控制器中的 CREATE:
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
演出观点:
<% unless current_user.is_admin %>
<% if current_user.offers.include?(@offer) %>
<%= render 'offers/unapply' %>
<% else %>
<%= render 'offers/apply' %>
<% end %>
<% end %>
_apply.html.erb部分:
<%= simple_form_for [@offer, @user_offer] do |f| %>
<%= f.button :submit %>
<% end %>
我在这个简单的问题上遇到了很多麻烦...
我认为您缺少指向 UserOffers 创建操作的路径
post 'offers/:id/apply', to: 'user_offers#create', :as => 'apply_for_offer'
然后在 apply.html.erb 部分你需要像
<%= simple_form_for ([@offer, @user_offer], method: :post, url: apply_for_offer_path) do |f| %>
<%= f.button :submit %>
<% end %>