(params。[:post])返回null值Rails 4.0

时间:2013-08-04 16:05:38

标签: ruby-on-rails ruby ruby-on-rails-4

似乎我的“创建”操作并未发送我在“新”视图中输入的信息。我有一条名为

的路线
localhost:3000/zips

我有新页面的视图

 <h1>
   Zip Code Eligibility <font color="green">Add Zip Code</font>
</h1>
<p>
   To <span class="hi">ADD</span> a zip code from the local delivery service, please enter the 5 digit zip code (6 in Canada) and click submit.
</p>
<%= form_for @zip do |z| %>
<p><%= z.label :zip %> <%= z.text_field :zip %> |
   <%= z.label :state %> <%= z.text_field :state %> 
</p>
<p><%= z.submit "Add Zip Code" %></p>
<% end %>

这是我的zips_copntroller

class ZipsController < ApplicationController

  def index
    @zips = Zip.all
  end

  def show
    @zip = Zip.find(params[:id])
  end

  def new
    @zip = Zip.new
  end

  def create
    @zip = Zip.new(params[:post])

    if @zip.save
      redirect_to zips_path, :notice => "Zipcode was saved sucessfully"
    else
      render "new"
    end

  end

  def edit
  end

  def update
  end

  def destroy
  end

end

现在,当我输入“91202”然后“CA”时,我点击提交...它提交并重定向到索引视图

<h1>
  Pick-up/Delivery Eligibility
</h1>
<h3>
Based on U.S. Zipcode, Canadian Postal Code
</h3>
<p class="el">
  To find out if you qualify for 4over&#146;s local delivery service,
  please enter your <nobr>U.S. 5 digit zip code, or</nobr> your 
  <nobr>Canadian 6 character Postal Code,</nobr> then click submit.
<h4>Current Eligible Zip Codes</h4>

<!-- Possible sample code for the display page when I get around to the search function -->
<% @zips.each do |zip| %>
 <!--"link_to" creates a link...first param is what you want to "display"...the second parameter
   is what you want to link to here it's a shortcut called "zip" (which passes the entrie POST and Rails will create the page with it's contents)
 -->
  <p><%= link_to zip.zip, zip %>|<%= zip.state %></p>
  <hr />
<% end %>
<p><%= link_to "Add a New Zip", new_zip_path %></p>

但是它没有显示我输入的zip和状态......它显示了这个(附图像)

No Zip added

以下是日志的转储......

Started POST "/zips" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Processing by ZipsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"wP8eOqMzOjLfLZjmq62EXdO0C59fHyRk3C/Y4b+96mA=", "zip"=>{"zip"=>"91202", "state"=>"CA"}, "commit"=>"Add Zip Code"}
   (0.3ms)  BEGIN
  SQL (0.7ms)  INSERT INTO "zips" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", Sat, 03 Aug 2013 19:51:42 UTC +00:00], ["updated_at", Sat, 03 Aug 2013 19:51:42 UTC +00:00]]
   (2.6ms)  COMMIT
Redirected to http://chrish.sbx.4over.com:3000/zips
Completed 302 Found in 8ms (ActiveRecord: 3.6ms)


Started GET "/zips" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Processing by ZipsController#index as HTML
  Zip Load (0.5ms)  SELECT "zips".* FROM "zips"
  Rendered zips/index.html.erb within layouts/application (2.7ms)
Completed 200 OK in 8ms (Views: 6.5ms | ActiveRecord: 0.5ms)


Started GET "/assets/application.css?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700


Started GET "/assets/zips.css?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700


Started GET "/assets/jquery.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700


Started GET "/assets/zips.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700


Started GET "/assets/turbolinks.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700


Started GET "/assets/jquery_ujs.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700


Started GET "/assets/application.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700

任何原因:post没有发送POST信息?

我正在使用Rails 4.0.0和Ruby 1.9.3p448:

[freedel@chrish.sbx.4over.com localdel]$ rails --version
Rails 4.0.0
[freedel@chrish.sbx.4over.com localdel]$ ruby --version
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

2 个答案:

答案 0 :(得分:1)

正如日志所说,您将能够以params [:zip]的形式接收表单参数。所以将params [:post]替换为params [:zip]。

如果您使用的是rails4,则必须在批量分配属性时允许参数。

在您的控制器中:

def create
  @zip = Zip.new(zip_params)

  if @zip.save
    redirect_to zips_path, :notice => "Zipcode was saved sucessfully"
  else
    render "new"
  end
end
...
private
  def zip_params
    params.require(:zip).permit(:zip, :state) 
  end

答案 1 :(得分:0)

由于这些只是两个属性,您可以将创建方法更改为此&amp;它会起作用:

def create
@zip = Zip.new(zip: params[:zip][:zip], state: params[:zip][:state] )

    if @zip.save
      redirect_to zips_path, :notice => "Zipcode was saved sucessfully"
    else
      render "new"
    end
end

但如果你有更多的属性,请使用Strong Params