我的参数怎么没有到达控制器? (Rails)
How is my parameter not getting to the controller? (Rails)
我可以清楚地看到我的参数在路由中传递给了控制器,但不知何故我的控制器没有接收到它。
供参考:
应用 has_many bills
/ belongs_to app
用户 has_many bills
/ belongs_to user
我在我的数据库中添加了对 :app_id
和 :user_id
的引用。
我的 bills
表单(即 BillsController#new
)的 URL 看起来像这样 apps/1/bills/new
(1
是 :app_id
)
应用程序显示视图:
<%= link_to "New Bill", new_app_bill_path(app_id: @app.id) %>
bills_controller
def new
@app = App.find(params[:app_id])
@bill = Bill.new
end
(以上作品)
def create
@app = App.find(params[:app_id])
@bill.app = @app
@user = @app.user
@bill.user = @user
@bill = Bill.new(bill_params)
@bill.save
respond_to do |format|
if @bill.save
format.html { redirect_to @bill, notice: 'Bill was successfully created.' }
format.json { render :show, status: :created, location: @bill }
else
format.html { render :new }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
(以上不起作用,returns nil
for app_id
)
日志:
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 09:47:20 -0400
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 09:47:20 -0400
Processing by BillsController#new as HTML
Processing by BillsController#new as HTML
Parameters: {"app_id"=>"1"}
Parameters: {"app_id"=>"1"}
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
Rendered bills/_form.html.erb (6.0ms)
Rendered bills/_form.html.erb (6.0ms)
Rendered bills/new.html.erb within layouts/application (7.2ms)
Rendered bills/new.html.erb within layouts/application (7.2ms)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 199ms (Views: 189.3ms | ActiveRecord: 1.9ms)
Completed 200 OK in 199ms (Views: 189.3ms | ActiveRecord: 1.9ms)
Started POST "/bills" for 127.0.0.1 at 2015-07-14 09:47:24 -0400
Started POST "/bills" for 127.0.0.1 at 2015-07-14 09:47:24 -0400
Processing by BillsController#create as HTML
Processing by BillsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Vjqma/mhU/RKFCd71VtLyUsvgamzaHqpFsWb56aSkfCz6/5YXXEn+DmNUiyPVR/MphTHJkiCTjUHyIKSEmUQCg==", "bill"=>{"item"=>"gfdsg", "price"=>"fdsgdfg", "status"=>"0"}, "commit"=>"Create Bill"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Vjqma/mhU/RKFCd71VtLyUsvgamzaHqpFsWb56aSkfCz6/5YXXEn+DmNUiyPVR/MphTHJkiCTjUHyIKSEmUQCg==", "bill"=>{"item"=>"gfdsg", "price"=>"fdsgdfg", "status"=>"0"}, "commit"=>"Create Bill"}
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.6ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.6ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
我知道我做了一些愚蠢的事情并遗漏了一些东西,但我不确定是什么或在哪里。如果我遗漏了我应该 post.
的任何代码,请告诉我
EDIT:
表单视图:
<%= form_for(@bill) do |f| %>
<% if @bill.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@bill.errors.count, "error") %> prohibited this bill from being saved:</h2>
<ul>
<% @bill.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :item %><br>
<%= f.text_field :item %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :status %><br>
<%= f.check_box :status %>
<%= f.hidden_field :app_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
重组#create
:
@app = App.find(params[:app_id])
@bill = Bill.new(bill_params)
@bill.app = @app
@user = @app.user
@bill.user = @user
@bill.save
日志输出:
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 11:41:04 -0400
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 11:41:04 -0400
Processing by BillsController#new as HTML
Processing by BillsController#new as HTML
Parameters: {"app_id"=>"1"}
Parameters: {"app_id"=>"1"}
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
Rendered bills/_form.html.erb (6.3ms)
Rendered bills/_form.html.erb (6.3ms)
Rendered bills/new.html.erb within layouts/application (7.3ms)
Rendered bills/new.html.erb within layouts/application (7.3ms)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 200ms (Views: 190.7ms | ActiveRecord: 2.1ms)
Completed 200 OK in 200ms (Views: 190.7ms | ActiveRecord: 2.1ms)
Started POST "/bills" for 127.0.0.1 at 2015-07-14 11:41:07 -0400
Started POST "/bills" for 127.0.0.1 at 2015-07-14 11:41:07 -0400
Processing by BillsController#create as HTML
Processing by BillsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Cv4VZRgbw74uExB8/2Hn+aCF98ejKQW+X/3xOsaXHQzvL01WvMu3sl2KZSulb7P8Tb6xSFjDMSJO8OhPcmCc9g==", "bill"=>{"item"=>"fdasfsa", "price"=>"dsafsad", "status"=>"0", "app_id"=>""}, "commit"=>"Create Bill"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Cv4VZRgbw74uExB8/2Hn+aCF98ejKQW+X/3xOsaXHQzvL01WvMu3sl2KZSulb7P8Tb6xSFjDMSJO8OhPcmCc9g==", "bill"=>{"item"=>"fdasfsa", "price"=>"dsafsad", "status"=>"0", "app_id"=>""}, "commit"=>"Create Bill"}
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
EDIT 2:
日志:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jnC5MC04LO691dQOgzkTNk6Bbkq3o5M97b5BnBq5dTproeEDiehY4s5MoVnZN0czo7ooxUxJp6H8s1jprk70wA==", "bill"=>{"item"=>"kjhgkhgk", "price"=>"jhgkhj", "status"=>"0", "app_id"=>"1"}, "commit"=>"Create Bill"}
App Load (0.2ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
App Load (0.2ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.2ms)
Completed 404 Not Found in 1ms (ActiveRecord: 0.2ms)
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
在您的表单中,因为您已经拥有
<%= f.hidden_field :app_id %>
在控制器中试试这个。
def create
@app = App.find(params['bill']['app_id'])
# rest of the code
end
你正在做...
<%= form_for(@bill) do |f| %>
这会为 new_bill_path
创建一个 post
,默认情况下不包含 app_id(这就是为什么有些人使用 'hidden field' 方法)
但是,如果您这样做...
<%= form_for([@app, @bill]) do |f| %>
这将为您提供 new_app_bill_path
,这意味着您将免费获得 params[:app_id]
。
我可以清楚地看到我的参数在路由中传递给了控制器,但不知何故我的控制器没有接收到它。
供参考:
应用 has_many bills
/ belongs_to app
用户 has_many bills
/ belongs_to user
我在我的数据库中添加了对 :app_id
和 :user_id
的引用。
我的 bills
表单(即 BillsController#new
)的 URL 看起来像这样 apps/1/bills/new
(1
是 :app_id
)
应用程序显示视图:
<%= link_to "New Bill", new_app_bill_path(app_id: @app.id) %>
bills_controller
def new
@app = App.find(params[:app_id])
@bill = Bill.new
end
(以上作品)
def create
@app = App.find(params[:app_id])
@bill.app = @app
@user = @app.user
@bill.user = @user
@bill = Bill.new(bill_params)
@bill.save
respond_to do |format|
if @bill.save
format.html { redirect_to @bill, notice: 'Bill was successfully created.' }
format.json { render :show, status: :created, location: @bill }
else
format.html { render :new }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
(以上不起作用,returns nil
for app_id
)
日志:
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 09:47:20 -0400
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 09:47:20 -0400
Processing by BillsController#new as HTML
Processing by BillsController#new as HTML
Parameters: {"app_id"=>"1"}
Parameters: {"app_id"=>"1"}
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
Rendered bills/_form.html.erb (6.0ms)
Rendered bills/_form.html.erb (6.0ms)
Rendered bills/new.html.erb within layouts/application (7.2ms)
Rendered bills/new.html.erb within layouts/application (7.2ms)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 199ms (Views: 189.3ms | ActiveRecord: 1.9ms)
Completed 200 OK in 199ms (Views: 189.3ms | ActiveRecord: 1.9ms)
Started POST "/bills" for 127.0.0.1 at 2015-07-14 09:47:24 -0400
Started POST "/bills" for 127.0.0.1 at 2015-07-14 09:47:24 -0400
Processing by BillsController#create as HTML
Processing by BillsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Vjqma/mhU/RKFCd71VtLyUsvgamzaHqpFsWb56aSkfCz6/5YXXEn+DmNUiyPVR/MphTHJkiCTjUHyIKSEmUQCg==", "bill"=>{"item"=>"gfdsg", "price"=>"fdsgdfg", "status"=>"0"}, "commit"=>"Create Bill"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Vjqma/mhU/RKFCd71VtLyUsvgamzaHqpFsWb56aSkfCz6/5YXXEn+DmNUiyPVR/MphTHJkiCTjUHyIKSEmUQCg==", "bill"=>{"item"=>"gfdsg", "price"=>"fdsgdfg", "status"=>"0"}, "commit"=>"Create Bill"}
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.6ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.6ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/nick/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
我知道我做了一些愚蠢的事情并遗漏了一些东西,但我不确定是什么或在哪里。如果我遗漏了我应该 post.
的任何代码,请告诉我EDIT:
表单视图:
<%= form_for(@bill) do |f| %>
<% if @bill.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@bill.errors.count, "error") %> prohibited this bill from being saved:</h2>
<ul>
<% @bill.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :item %><br>
<%= f.text_field :item %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :status %><br>
<%= f.check_box :status %>
<%= f.hidden_field :app_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
重组#create
:
@app = App.find(params[:app_id])
@bill = Bill.new(bill_params)
@bill.app = @app
@user = @app.user
@bill.user = @user
@bill.save
日志输出:
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 11:41:04 -0400
Started GET "/apps/1/bills/new" for 127.0.0.1 at 2015-07-14 11:41:04 -0400
Processing by BillsController#new as HTML
Processing by BillsController#new as HTML
Parameters: {"app_id"=>"1"}
Parameters: {"app_id"=>"1"}
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", 1]]
Rendered bills/_form.html.erb (6.3ms)
Rendered bills/_form.html.erb (6.3ms)
Rendered bills/new.html.erb within layouts/application (7.3ms)
Rendered bills/new.html.erb within layouts/application (7.3ms)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 200ms (Views: 190.7ms | ActiveRecord: 2.1ms)
Completed 200 OK in 200ms (Views: 190.7ms | ActiveRecord: 2.1ms)
Started POST "/bills" for 127.0.0.1 at 2015-07-14 11:41:07 -0400
Started POST "/bills" for 127.0.0.1 at 2015-07-14 11:41:07 -0400
Processing by BillsController#create as HTML
Processing by BillsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Cv4VZRgbw74uExB8/2Hn+aCF98ejKQW+X/3xOsaXHQzvL01WvMu3sl2KZSulb7P8Tb6xSFjDMSJO8OhPcmCc9g==", "bill"=>{"item"=>"fdasfsa", "price"=>"dsafsad", "status"=>"0", "app_id"=>""}, "commit"=>"Create Bill"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Cv4VZRgbw74uExB8/2Hn+aCF98ejKQW+X/3xOsaXHQzvL01WvMu3sl2KZSulb7P8Tb6xSFjDMSJO8OhPcmCc9g==", "bill"=>{"item"=>"fdasfsa", "price"=>"dsafsad", "status"=>"0", "app_id"=>""}, "commit"=>"Create Bill"}
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
EDIT 2:
日志:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jnC5MC04LO691dQOgzkTNk6Bbkq3o5M97b5BnBq5dTproeEDiehY4s5MoVnZN0czo7ooxUxJp6H8s1jprk70wA==", "bill"=>{"item"=>"kjhgkhgk", "price"=>"jhgkhj", "status"=>"0", "app_id"=>"1"}, "commit"=>"Create Bill"}
App Load (0.2ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
App Load (0.2ms) SELECT "apps".* FROM "apps" WHERE "apps"."id" = LIMIT 1 [["id", nil]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.2ms)
Completed 404 Not Found in 1ms (ActiveRecord: 0.2ms)
ActiveRecord::RecordNotFound (Couldn't find App with 'id'=):
app/controllers/bills_controller.rb:28:in `create'
在您的表单中,因为您已经拥有
<%= f.hidden_field :app_id %>
在控制器中试试这个。
def create
@app = App.find(params['bill']['app_id'])
# rest of the code
end
你正在做...
<%= form_for(@bill) do |f| %>
这会为 new_bill_path
创建一个 post
,默认情况下不包含 app_id(这就是为什么有些人使用 'hidden field' 方法)
但是,如果您这样做...
<%= form_for([@app, @bill]) do |f| %>
这将为您提供 new_app_bill_path
,这意味着您将免费获得 params[:app_id]
。