如何继续接受和拒绝铁轨的功能

发布于 2025-01-23 11:37:35 字数 1104 浏览 2 评论 0原文

我正在尝试一项功能,用户可以在其中要求提供报价,并且可以接受或拒绝,我是Rails的新手。我不知道继续这样做的好方法。 优惠创建方法

def create
@offer = Offer.new(offer_params)
pp offer_params
@barter = Barter.find(params[:barter_id])
@offer = Offer.new(offer_params)
@offer.barter = @barter
@offer.user = current_user
respond_to do |format|
  if @offer.save
    format.js do
      @barter = Barter.find(params[:barter_id])
    end
  else
    format.html { render :new, status: :unprocessable_entity }
    format.json { render json: @review.errors, status: :unprocessable_entity }
  end
end

终端

提交

  <%= form_for([ @barter, @barter.offers.new] ) do |form| %>

            <%= form.text_area :message %><br>
            <%= form.submit "leave" %>

      <% end %>

在这里我想让它被接受或拒绝,我给出了布尔值,而在被拒绝时简单地将其变为错误

  <%= form_tag([ @barter, @barter.offers.new] ) do  %>

            <%= hidden_field_tag :reject, :value => true %><br>
            <%= submit_tag "reject" %>

          <% end %>

?当我接受它时,我该如何使它消失。

I'm trying a feature where user can request for offer and it can be accepted or rejected , I'm new to rails. i can't figure out what's the good way to proceed this.
offer create method

def create
@offer = Offer.new(offer_params)
pp offer_params
@barter = Barter.find(params[:barter_id])
@offer = Offer.new(offer_params)
@offer.barter = @barter
@offer.user = current_user
respond_to do |format|
  if @offer.save
    format.js do
      @barter = Barter.find(params[:barter_id])
    end
  else
    format.html { render :new, status: :unprocessable_entity }
    format.json { render json: @review.errors, status: :unprocessable_entity }
  end
end

end

offer submission

  <%= form_for([ @barter, @barter.offers.new] ) do |form| %>

            <%= form.text_area :message %><br>
            <%= form.submit "leave" %>

      <% end %>

here I want to make it accepted or rejected , I've given a boolean value and simply make it false when rejected

  <%= form_tag([ @barter, @barter.offers.new] ) do  %>

            <%= hidden_field_tag :reject, :value => true %><br>
            <%= submit_tag "reject" %>

          <% end %>

is there a good way to do this? and how can i make it disappear when i accept this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾旎 2025-01-30 11:37:35

抱歉,那甚至还没有关闭。当您应该做的是更新现有记录时,您只是在形式中创建新的要约记录 - 而您有可能通过patch/gita/gesl/gesl/:id进行此操作。就意图而言。

我认为处理方法的最简单方法是简单地添加两个附加休息的路线更新优惠。

首先添加路线:

resources :offers, only: [] do
  patch :accept
  patch :decline
end

并将枚举归因于模型:

class AddStatusToOffers < ActiveRecord::Migration[7.0]
  def change
    add_column :offers, :status, :integer, default: 0, index: true
  end
end
class Offer < ApplicationRecord
  # ...
  enum status: {
    pending: 0,
    accepted: 1,
    rejected: 2
  }
end

这是一个更好的主意,然后添加布尔值,因为您的布尔值要么需要是一个三态布尔(无效),因此被认为是非常糟糕的练习,或者是默认的练习或错误,在这种情况下,您无法区分用户已回复或不回复的报价。

然后为新端点添加控制器方法:

class OffersController
  before_action :set_coffer, only: %i{ show edit update destroy accept decline }

  # ...

  # PATCH /offers/:id/accept
  # @TODO authorize that the user should actually be allowed the offer
  def accept
    if @offer.accepted!
      redirect_to @offer, notice: 'Offer accepted'
    else
      redirect_to @offer, notice: 'Offer could not be accepted - please try again' 
    end
  end

  # PATCH /offers/:id/reject
  # @TODO authorize that the user should actually be reject the offer
  def reject
    if @offer.rejected!
      redirect_to @offer, notice: 'Offer rejected'
    else
      redirect_to @offer, notice: 'Offer could not be rejected - please try again' 
    end
  end

  private

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

然后,您可以简单地添加发送请求以更新报价的按钮/链接:

<%= button_to "Accept", accept_offer_path(offer), method: :patch %> 
<%= button_to "Reject", reject_offer_path(offer), method: :patch %> 

这不是解决问题的唯一方法。例如,如果您想记录一条消息,用户可以在其中说出他们为什么拒绝要约的原因,我会将报价的答复模型为完全独立的资源。

Sorry but thats not even close. You're just creating a new offer record in the form when what you should be doing is to update an existing record - and while you potentially do this through PATCH /offers/:id its going to be very ambigeuos in terms of intent.

The simplest way I cn think of handle this would be to simply add two additional RESTful routes to update the offers.

Start by adding the routes:

resources :offers, only: [] do
  patch :accept
  patch :decline
end

And en enum attribute to the model:

class AddStatusToOffers < ActiveRecord::Migration[7.0]
  def change
    add_column :offers, :status, :integer, default: 0, index: true
  end
end
class Offer < ApplicationRecord
  # ...
  enum status: {
    pending: 0,
    accepted: 1,
    rejected: 2
  }
end

This is a better idea then adding a boolean since your boolean would either need to be a tri-state boolean (nullable) which is regarded as a very bad practice or default to false in which case you can't differentiate between the offers a users has replied to or not.

Then add the controller methods for your new endpoints:

class OffersController
  before_action :set_coffer, only: %i{ show edit update destroy accept decline }

  # ...

  # PATCH /offers/:id/accept
  # @TODO authorize that the user should actually be allowed the offer
  def accept
    if @offer.accepted!
      redirect_to @offer, notice: 'Offer accepted'
    else
      redirect_to @offer, notice: 'Offer could not be accepted - please try again' 
    end
  end

  # PATCH /offers/:id/reject
  # @TODO authorize that the user should actually be reject the offer
  def reject
    if @offer.rejected!
      redirect_to @offer, notice: 'Offer rejected'
    else
      redirect_to @offer, notice: 'Offer could not be rejected - please try again' 
    end
  end

  private

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

You can then simply add buttons/links that send the request to update the offer:

<%= button_to "Accept", accept_offer_path(offer), method: :patch %> 
<%= button_to "Reject", reject_offer_path(offer), method: :patch %> 

This is not the only way to solve the issue. If you for example want to record a message where the user can say why they rejected an offer I would model replies to an offer as a completely seperate resource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文