0

我需要从具有不在数据库中的值的表单传递参数(因此表单将具有仅供该页面使用的参数,并且永远不会再使用)。提交表单后,我想将这些参数传递给适应 html.erb 页面的 ruby​​ 脚本。

到目前为止,我所能做的就是传递一个参数(从具有模型和控制器的对象的早期页面接收),但我不能让表单传递我当前表单的参数。下面的表格会正确显示从上一个link_to获取的ISBN

这是目前我的表格:

<%= form_tag(:controller => "pages", :action => "post_form", :condition =>     params[:condition], :isbn => params[:isbn], :price => params[:price], :description => params[:description], :comment => params[:comment]) do %>
  <div class="field">
    <%= label_tag "Book ISBN" %><br />
    <%= params[:isbn] %>
  </div>

  <div class = "field">
    <%= label_tag "Condition" %>
    <%= select_tag :condition, raw("<option>Brand New'</option><option>Like   New</option><option>Very Good</option><option>Good</option><option>Acceptable</option>")%>
  </div>

  <div class = "field">
    <%= label_tag "Price" %>
    <%= text_field_tag :price %>
  </div>

  <div class = "field">
    <%= label_tag "Description" %>
    <%= text_field_tag :description %>
  </div>

  <div class = "field">
    <%= label_tag :comment %>
    <%= text_field_tag :comment %>
  </div>

  <div class = "actions">
    <%= submit_tag "Submit"%>
  </div>
<% end %>

我检查了我的 tail -f log/production.log,这就是我知道 ISBN 在提交时正确传递到下一页的方式。谁能帮我弄清楚如何传递必须从表单本身获取的其他参数?

=====================================EDIT1============= =============================这是我的post_form代码。它主要处理来自 Half.com 的 API。我们最初通过命令提示符运行的 ruby​​ 脚本成功地测试了这一点

<%
require 'httpclient'
require 'nokogiri'
%>

<% isbn = params[:isbn] %>
<% condition = params[:condition] %>
<% price = params[:price] %>
<% description = params[:description] %>
<% comment = params[:comment] %>

<%
token = 'ourUniqueSellerToken'

builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') { |xml|
xml.AddItem('xmlns' => 'default', 'xmlns' => 'urn:ebay:apis:eBLBaseComponents') do
  xml.RequesterCredentials do
     xml.eBayAuthToken token
  end
  xml.Item do
    xml.AttributeArray do
      xml.Attribute('attributeLabel' => 'Condition') do
        xml.Value do
          xml.ValueLiteral condition
        end
      end
      xml.Attribute('attributeLabel' => 'Notes') do
        xml.Value do
          xml.ValueLiteral comment
        end
      end
    end
    xml.Country 'US'
    xml.Currency 'USD'
    xml.Description description
    xml.ListingDuration 'GTC'
    xml.ListingType 'Half'
    xml.Location 'CO'
    xml.Quantity '1'
    xml.StartPrice price
    xml.ExternalProductID do
      xml.Value isbn
      xml.type 'ISBN'
    end
  end
end
}

client = HTTPClient.new
uri = 'https://api.ebay.com/ws/api.dll'

headers = {
        'X-EBAY-API-COMPATIBILITY-LEVEL' => '823',
        'X-EBAY-API-DEV-NAME' => 'ourDevName',
        'X-EBAY-API-APP-NAME' => 'ourAppName',
        'X-EBAY-API-CERT-NAME' => 'ourCertName',
        'X-EBAY-API-SITEID' => '0',
        'X-EBAY-API-CALL-NAME' => 'AddItem'
}

results = client.post_content(uri, builder.to_xml, headers)

doc = Nokogiri::XML.parse(results)
%>

<b><%= "Returned: "+doc.css("Ack").text %> </b>
<b><%= "Listing ID: "+doc.css("ItemID").text %></b>

表单的控制器只是一个空白函数:

class PagesController < ApplicationController

  def search
  end

  def listing
  end

  def post_form
  end

end

============以下来自我的开发日志(打开表单页面)==============

Started GET "/pages/listing?class=btn+btn-mini&isbn=9780307588364" for 138.67.201.236 at 2013-06-04 20:33:01 +0000
Processing by PagesController#listing as HTML
  Parameters: {"class"=>"btn btn-mini", "isbn"=>"9780307588364"}
  Rendered pages/_form.html.erb (1.4ms)
  Rendered pages/listing.html.erb within layouts/application (1.9ms)
Completed 200 OK in 3ms (Views: 3.2ms | ActiveRecord: 0.0ms)
Started POST "/pages/post_form" for 138.67.201.236 at 2013-06-04 20:33:09 +0000

(提交时)

ActionController::RoutingError (No route matches [POST] "/pages/post_form"):
  actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
  rack-cache (1.2) lib/rack/cache/context.rb:143:in `pass'
  rack-cache (1.2) lib/rack/cache/context.rb:155:in `invalidate'
  rack-cache (1.2) lib/rack/cache/context.rb:71:in `call!'
  rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
  railties (3.2.13) lib/rails/engine.rb:479:in `call'
  railties (3.2.13) lib/rails/application.rb:223:in `call'
  railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
  passenger (4.0.2) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in     `process_request'
  passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:135:in     `accept_and_process_next_request'
  passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:106:in     `main_loop'
  passenger (4.0.2) lib/phusion_passenger/request_handler.rb:449:in `block (4 levels)    in start_threads'
  passenger (4.0.2) lib/phusion_passenger/utils/robust_interruption.rb:108:in   `disable_interruptions'
  passenger (4.0.2) lib/phusion_passenger/request_handler.rb:444:in `block (3 levels)    in start_threads'

(刷新时)

Started GET "/pages/post_form" for 138.67.201.236 at 2013-06-04 20:33:14 +0000
Processing by PagesController#post_form as HTML
  Rendered pages/post_form.html.erb within layouts/application (252.9ms)
Completed 200 OK in 257ms (Views: 256.6ms | ActiveRecord: 0.0ms)
4

1 回答 1

0

您传递了两次参数:一次通过表单操作中的 url 参数,呈现为类似

<form accept-charset="UTF-8" action="/pages?comment=params%5B%3Acomment%5D&amp;condition=cond&amp;description=params%5B%3Adescription%5D&amp;isbn=isbn&amp;price=params%5B%3Aprice%5D" method="post">

第二次作为POST表单中的参数(ISBN 除外)。

删除表单中的参数并将它们设置为文本字段中的默认值:

<%= form_tag(:controller => "pages", :action => "post_form" %>
<%= hidden_field_tag :isbn, params[:isbn] %>
<%= hidden_field_tag :condition, params[:condition] %>

...

<div class = "field">
  <%= label_tag "Description" %>
  <%= text_field_tag :description, param[:description] %>
</div>

让模型完成繁重的工作是一种很好的做法。ActiveModel如果它与数据库无关,您可以使用,。
然后您也可以将此模型用于表单并使用普通表单助手。

附录

您的日志显示,您缺少POSTto的路线pages#post_form
添加你的 routes.rb

post 'pages/post_form'

提交表单是通过POST请求完成的,但请求失败。
“刷新”是由GET请求完成的。

在 Rails 中,您应该对GET和有不同的操作POST。他们真的做了一些完全不同的事情。

于 2013-06-04T19:56:39.213 回答