渲染不渲染正确的页面
这可能无法回答,因为这里可能有太多变量,但我想我应该尝试一下,因为我总是在这里找到其他答案。我对 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 %>
我在控制器中的创建方法:
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 而不是 /bills/new。它曾经有效,我不知道我做错了什么,但现在不行了。我得到与 render 相同的响应:template => “账单/新”。它通过重定向转到正确的页面,但随后它不会使用旧值填充表单。
日志:
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]
希望有人知道我做错了什么,或者我想我要重新开始。
This might be impossible to answer since there are probably too many variables here, but I thought I'd give it a shot since I always find other answers here. I am still fairly new to rails.
So, I have a bills model/controller/view. I want to create a new bill. I will be editing out the things that shouldn't matter much, but if they are needed I can add them in - just don't want a wall of text.
In the route:
map.resources :bills
My new method in the controller:
def new
@bill = Bill.new
@submit_txt = "Create"
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @bill }
end
end
my form:
<% form_for(@bill) do |f| %>
<%= f.error_messages %>
### form elements here, this all seems fine ####
<p>
<%= f.submit @submit_txt %>
</p>
<% end %>
my create method in the controller:
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
For some reason this always renders /bills instead of /bills/new. It used to work and I don't know what I did wrong, but now it's not. I get the same response with render :template => 'bills/new'. It goes to the right page with a redirect, but then it won't fill in the form with old values.
The log:
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]
Hopefully someone has an idea of what I've done wrong, or I guess I'm starting over.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从命令行运行
rake:routes
,您将看到它们如何映射。RESTful 资源需要一些时间来适应,但在您的情况下,带有
post
方法的\bills
会转到创建操作。您在create
操作中指定在调用render :action =>; 时渲染
- 您实际上并未运行该操作。new
模板的内容。 :newrun a
rake:routes
from your command line and you will see how they map.The RESTful resources take a little getting used to but in your case
\bills
with apost
method goes to the create action. You are specifying in thecreate
action to render the contents of thenew
template when you callrender :action => :new
- you do not actually run the action.试试这个:
来自文档:
尝试一下,让我们知道进展如何。另外,如果您渲染“新”,请记住您的新操作会创建一个新的 Bill 对象,并且不会有任何旧值可供填充。我认为您真正想做的是渲染:编辑。在您的编辑操作中,找到带有传递给该操作的参数的 Bill 对象。
Try this:
From the docs:
Give this a shot and let us know how it goes. Also, if you render "new", remember that your new action creates a new Bill object and there won't be any old values for it to fill in. I think what you really want to do is render :edit. And in your edit action find the Bill object with the params that you pass to the action.