渲染不渲染正确的页面

发布于 2024-09-10 13:40:50 字数 2178 浏览 9 评论 0原文

这可能无法回答,因为这里可能有太多变量,但我想我应该尝试一下,因为我总是在这里找到其他答案。我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凝望流年 2024-09-17 13:40:50

从命令行运行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 模板的内容。 :new - 您实际上并未运行该操作。

run a rake:routes from your command line and you will see how they map.

    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"}

The RESTful resources take a little getting used to but in your case \bills with a post method goes to the create action. You are specifying in the create action to render the contents of the new template when you call render :action => :new - you do not actually run the action.

剧终人散尽 2024-09-17 13:40:50

试试这个:

render :new

来自文档:

将 render 与 :action 结合使用是 Rails 新手经常感到困惑的一个原因。指定的操作用于确定要呈现哪个视图,但 Rails 不会在控制器中运行该操作的任何代码。在调用渲染之前,必须在当前操作中设置视图中所需的任何实例变量。

尝试一下,让我们知道进展如何。另外,如果您渲染“新”,请记住您的新操作会创建一个新的 Bill 对象,并且不会有任何旧值可供填充。我认为您真正想做的是渲染:编辑。在您的编辑操作中,找到带有传递给该操作的参数的 Bill 对象。

Try this:

render :new

From the docs:

Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文