将自定义_form文本字段参数传递给第二页Rails

时间:2013-06-04 18:53:43

标签: ruby-on-rails ruby forms parameter-passing

我需要从不具有数据库值的表单传递参数(因此表单将只有该页面使用的参数,而不再使用)。提交表单后,我想将这些参数传递给一个改编成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)

1 个答案:

答案 0 :(得分: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与数据库无关,则可以使用POST 然后,您还可以将此模型用于表单并使用普通表单帮助程序。

附录

您的日志显示,您错过了pages#post_formpost 'pages/post_form' 的路线 添加您的routes.rb

POST

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

在Rails中,您应该对POST和{{1}}采取不同的操作。他们真的做了一些完全不同的事情。