Rails:我无法在重定向中传递验证错误 [英] Rails: I cant pass a validation error in a redirect

查看:80
本文介绍了Rails:我无法在重定向中传递验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这是一个简单的项目,其中包含产品,您可以竞标它们。想想eBay。

So this is a simple project where there are products and you can bid for them. Think eBay.

我按如下方式构建了项目:

I built the project as follows:

$ rails new routetest
$ rails g scaffold product productname reserveprice:integer
$ rails g scaffold bid bidprice:integer product_id

在每个产品的显示视图中,我包括

On the 'show' view for each product I include


  • 产品详细信息(由支架产生) )

  • 到目前为止的出价列表

  • 使用form_for辅助工具汇总新出价的表单

<p id="notice"><%= notice %></p>
<h2>Normal Products\Show Part</h2>
<p>
  <strong>Productname:</strong>
  <%= @product.productname %>
</p>
<p>
  <strong>Reserveprice:</strong>
  <%= @product.reserveprice %>
</p>
<%= link_to 'Back', products_path %>
<br /><br />

<h2>List of bids</h2>
<table>
  <thead>
    <tr>
      <th>Product ID</th>
      <th>Bid Price</th>
    </tr>
  </thead>
  <tbody>
    <% @bids.each do |bid| %>
      <tr>
        <td><%= bid.product_id %></td>
        <td><%= bid.bidprice %></td>
      </tr>
    <% end %>       
  </tbody>
</table>
<br /><br />

<h2>Form For Bids</h2>
<h4>Make a Bid</h4>
<%= form_for(@bid) do |b| %>
  <div class="field">
    <%= b.number_field :bidprice %>
  </div>
  <div>
    <%= b.hidden_field :product_id, :value => (params[:id]) %>
  </div>
  <div class="actions">
    <%= b.submit %>
  </div>
<% end %>

在单击提交后,将调用出价控制器#create操作。

On clicking 'submit' the bids controller#create action is called.

  def create
    @bid = Bid.new(bid_params)
      if @bid.save
        redirect_to "/products/#{@bid.product_id}", notice: 'Bid was successfully created.' 
      else
        render action: 'new' 
      end
  end

如此成功的出价使用户返回到产品展示视图,并将该记录添加到页面的出价部分。

So a sucessful bid returns the user to the product show view and adds the record to the bids section of the page.

我在出价模型中有一个validates_presence_of验证。因此,如果用户将出价字段留空,则验证将失败,并且出价控制器的板条箱操作将呈现新的出价视图。包括错误消息。出价新视图包括脚手架生成的代码 <%,如果@ bid.errors.any?%>等

I have a validates_presence_of validation in the bids model. So if the user leaves the bids field empty the validation fails and the bids controller crate action renders the bids new view. Including the error messages. The bids new view includes the scaffold generated code "<% if @bid.errors.any? %> etc"

我实际上想做的而不是呈现新的操作是使用户回到产品展示页面,就像成功竞标时只在产品展示页面上显示错误消息一样。基本上,这使用户有一种停留在产品展示页面上的感觉,而不是将它们扔到出价新页面上。

What I actually want to do instead of rendering the new action is to throw the user back to the products show page as it does when a successful bid is made only to show the error messages on the products show page. Basically this gives the user the sense of staying on the products show page instead of throwing them to the bids new page.

我的问题是如何得到错误消息传递回产品显示页面?如果我编辑了出价控制器,请在上方创建操作代码,并用

The problem I have is how do I get the error message to pass back to the products show page? If I edit the bids controller create action code above and replace the "render action: 'new'" line with

redirect_to "/products/#{@bid.product_id}"

然后将用户返回到页面,但错误消息未通过。我认为这是因为错误消息是作为(出价控制器的)新操作呈现的一部分存储的

then the user is returned to the page but the error message isn't passed. I think this is because the error message is stored as part of the rendering of the new action (of the bids controller)

所以我的问题是如何实现自己的目标想做?要返回产品显示视图,并显示失败的验证产生的错误消息。谢谢。

So my question is how do I achieve what I want to do? To return to the products show view and show the error messages that come from the failed validation. Thank you.

PS-我想我需要添加支架生成的代码<%if @ bid.errors.any?%> ...产品显示视图,但在上面的代码中我没有这样做,因为它会导致NilMethod错误,因为没有为该方法创建错误而存在。

PS - I kow I need to add the scaffold generated code "<% if @bid.errors.any? %> ..." code to the products show view but I haven't done so in the code above because it will cause a NilMethod error because the error isnt created for the method to exist on it.

推荐答案

您想要这样的东西

redirect_to product_path(@bid.product_id), :flash => { :error => @bid.errors.full_messages.join(', ') }

这将重定向到产品页面但是将错误作为即时消息传递。

This will redirect to the product page but pass the errors as a flash message.

这篇关于Rails:我无法在重定向中传递验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆