这可能是不可能回答的,因为这里可能有太多的变量,但我想我应该试一试,因为我总是在这里找到其他答案。我仍然是rails的新手。
因此,我有一个账单模型/控制器/视图。我想创建一个新的帐单。我会编辑掉那些不太重要的东西,但如果需要的话,我可以把它们加进去--只是不想要一堵墙的文字。
在路由中:
map.resources :bills我在控制器中的新方法:
def new
@bill = Bill.new
@submit_txt = "Create"
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @bill }
end
end我的表单:
<% form_for(@bill) do |f| %>
<%= f.error_messages %>
### form elements here, this all seems fine ####
<p>
<%= f.submit @submit_txt %>
</p>
<% end %>我在控制器中的create方法:
def create
is_weekly = false
is_monthly = false
@bill = current_user.recurring_bills.build(params[:bill])
@bill.year = @current_year
@errors = 'checking this out'
if @errors.blank?
logger.info "no errors, supposedly; going to save"
### do saving stuff here####
else
logger.info "errors not blank"
render :action => :new
end
end由于某些原因,这总是呈现新的而不是/ /bills/new。它以前是有效的,我不知道我做错了什么,但现在不是了。我得到了与render :template => 'bills/new‘相同的响应。它将重定向到正确的页面,但不会使用旧值填充表单。
日志:
Processing BillsController#create (for 127.0.0.1 at 2010-07-21 21:00:47) [POST]
Parameters: {"commit"=>"Create", "action"=>"create", "authenticity_token"=>"Kc7/iPKbfJBKHHVARuN7K6207tW6Jx4OUn7Xb4uSB8A=", "bill"=>{"name"=>"rent", "month"=>"", "amount"=>"200", "alternator"=>"odd", "day"=>"35", "frequency"=>"monthly", "weekday"=>""}, "controller"=>"bills"}
User Load (0.6ms) SELECT * FROM "users" WHERE ("users"."remember_token" = 'dd7082c56f5a252d14e4e68c528eb26551875c647f998c15d16a064cb075d63c') LIMIT 1
errors not blank
Rendering template within layouts/application
Rendering bills/new
Rendered bills/_form (14.5ms)
Rendered layouts/_stylesheets (3.3ms)
Rendered layouts/_header (5.7ms)
Rendered layouts/_footer (0.3ms)
Completed in 174ms (View: 30, DB: 1) | 200 OK [http://localhost/bills]希望有人知道我做错了什么,或者我猜我正在重新开始。
发布于 2010-07-22 14:26:03
从命令行运行rake:routes,您将看到它们是如何映射的。
bills GET /bills(.:format) {:controller=>"bills", :action=>"index"}
POST /bills(.:format) {:controller=>"bills", :action=>"create"}
new_bill GET /bills/new(.:format) {:controller=>"bills", :action=>"new"}
edit_bill GET /bills/:id/edit(.:format) {:controller=>"bills", :action=>"edit"}
bill GET /bills/:id(.:format) {:controller=>"bills", :action=>"show"}
PUT /bills/:id(.:format) {:controller=>"bills", :action=>"update"}
DELETE /bills/:id(.:format) {:controller=>"bills", :action=>"destroy"}RESTful资源需要一点时间来适应,但在本例中,带有post方法的\bills将转到create操作。当您调用render :action => :new时,您在create操作中指定呈现new模板的内容-您实际上并没有运行该操作。
发布于 2010-07-22 13:42:07
试试这个:
render :new从文档中:
使用render with :action是Rails新手经常感到困惑的地方。指定的操作用于确定要呈现哪个视图,但Rails不会在控制器中运行该操作的任何代码。在调用render之前,必须在当前操作中设置视图中所需的任何实例变量。
试一试,让我们知道它的进展。此外,如果您呈现" new ",请记住,您的新操作将创建一个新的Bill对象,并且没有任何旧值可供其填充。我认为你真正想做的是渲染:编辑。在您的编辑操作中,找到带有您传递给该操作的参数的Bill对象。
https://stackoverflow.com/questions/3305717
复制相似问题