Rails控制器触发If-Else语句的两个分支

时间:2021-05-26 19:54:34

I'm new here so I hope it's appropriate for new accounts to ask questions straight away. I've been working with Rails for sometime now and usually I'm pretty good at researching and solving my own problems - but this one has had me stumped for a few days now.

我是新来的,所以我希望新账户能够立即提出问题。我一直在和Rails合作已经有一段时间了,通常我很擅长研究和解决自己的问题 - 但是这个问题已让我难倒了好几天了。

I have a create action being called in a controller that contains an if-else statement that is conditional based on a check_box post parameter. I can see that the parameter is being posted to the controller so the statement should be able to correctly branch but something strange is happening. The controller executes both branches of the statement, but because my parameters are trimmed depending on that check_box, the second branch always errors out. I'm fairly confident this has little to do with routes.

我在控制器中调用了一个create动作,该控制器包含一个基于check_box post参数的条件if-else语句。我可以看到参数被发布到控制器,所以语句应该能够正确分支,但发生了一些奇怪的事情。控制器执行语句的两个分支,但由于我的参数根据check_box进行修剪,因此第二个分支总是出错。我相信这与路线没什么关系。

Please see my code below:

请参阅下面的代码:

Controller:

  def create
@quote = Quote.find(params[:quote_id])

if params[:quote_item][:custom] == 1
  @quote_item = @quote.quote_items.new(quote_item_params)
  @quote_item.rate = nil
  @quote_item.custom = true
  @quote_item.unit_price = params[:quote_item][:unit_price]
  @quote_item.rate_name = params[:quote_item][:title]
else
  @quote_item = @quote.quote_items.new(quote_item_params)
  @quote_item.custom = false
  @rate = Rate.find(params[:quote_item][:rate_id])
    @quote_item.unit_price = @rate.price
  @quote_item.rate_name = @rate.product.name
end

respond_to do |format|
  if @quote.save
    format.html { redirect_to @quote, notice: 'Quote item was successfully added.' }
    format.json { render :show, status: :created, location: @quote }
  else
    format.html { redirect_to @quote }
    format.json { render json: @quote.errors, status: :unprocessable_entity }
  end
end

View:

    <% if @rates.any? %>
<%= bootstrap_form_for([@quote, @quote_item], layout: :inline) do |f| %>
    <%= f.text_field :title, class: 'input-sm', hide_label: true, :placeholder => "Line Title" %>
  <%= f.select(:rate_id, @rates.collect {|r| [r.select_summary_text, r.id ] }, { hide_label: true }, { :class => 'input-sm' }) %>
    <%= f.number_field :quantity, class: 'input-sm', hide_label: true, :min => 1, :length => 2,  :value => 1 %>
  <%= f.text_field :unit_price, class: 'input-sm', hide_label: true, :min => 1, :length => 2, :prepend => "$" %>
    <%= f.text_field :note, class: 'input-sm', hide_label: true, :placeholder => "Note (Optional)" %>
  <%= f.submit "Add Quote Item", class: 'btn btn-sm btn-default' %>
  <%= f.check_box :custom, label: "Override" %>
<% end %>
<% else %>
<div class="well well-sm"><i class="fa fa-usd"></i> No pricing for this customer has been set. Set product pricing for this jobs customer <%= link_to user_path(@quote.job.user) do %>here.<% end %></div>
<% end %>

My create method errors out here:

我的create方法错误在这里:

@rate = Rate.find(params[:quote_item][:rate_id])

With:

ActiveRecord::RecordNotFound (Couldn't find Rate with 'id'=):

app/controllers/quote_items_controller.rb:19:in `create'

This error is correct though, because the rate ID isn't processed in first branch of the if-else statement. I've tried different check_box form fields, directly overriding 'custom' and both branches still run.

但是,此错误是正确的,因为在if-else语句的第一个分支中不处理速率ID。我尝试了不同的check_box表单字段,直接覆盖'custom',两个分支仍然运行。

Any help here is greatly appreciated!

非常感谢任何帮助!

1 个解决方案

#1


0  

The params hash contains strings.

params哈希包含字符串。

Try swapping:

if params[:quote_item][:custom] == '1'

for your original:

为你的原件:

if params[:quote_item][:custom] == 1

#1


0  

The params hash contains strings.

params哈希包含字符串。

Try swapping:

if params[:quote_item][:custom] == '1'

for your original:

为你的原件:

if params[:quote_item][:custom] == 1