简体   繁体   English

Ruby on Rails 3:尝试从不同的控制器渲染视图。 但我到同一个控制器

[英]Ruby on Rails 3 : Trying to render a view from a different controller. But I get to the same controller

In my app, I have created a form to create new posts (Post is a model). 在我的应用程序中,我创建了一个表单来创建新帖子(Post是一个模型)。 I have a controller with the name posts. 我有一个名为posts的控制器。 The form is located in the admins controller. 该表单位于管理员控制器中。 The form use the form_for(@post). 表单使用form_for(@post)。 It post to the post controller. 它发布到帖子控制器。 In this method, the post gets created successfully. 在此方法中,帖子成功创建。 But the problem is with failed submissions. 但问题是提交失败。 In the form, I have added the code to show the errors 在表单中,我添加了代码以显示错误

    <% if @post.errors.any? %>
        <div id="error_explanation">
            <div class="alert alert-error">
                The form contains <%= pluralize(@post.errors.count, "error") %>
            </div>
            <ul>
                <% @post.errors.full_messages.each do |msg| %>
                    <li> <%= msg %> </li>
                <% end %>
            </ul>
        </div>
    <% end %>

But in the post controller, if there's a failed submission, I have set it to redirect_to the newpost page in the admins controller. 但是在post控制器中,如果提交失败,我将其设置为redirect_to admins控制器中的newpost页面。 When redirecting, I will loose the error messages. 重定向时,我将丢失错误消息。

So, I added 所以,我补充道

render 'dasharea/newpost'

localhost:3000/dasharea/newpost is the URL of the page that have the form. localhost:3000 / dasharea / newpost是具有该表单的页面的URL。 With this code, it say 有了这段代码,它说

Missing template dasharea/newpost with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.

But Im already in that page? 但我已经在那个页面? Then I tried to add the controller and the action like this (note: im editing the create action of posts controller. I need to render the newpost action in admins controller) : 然后我尝试添加控制器和这样的动作(注意:我正在编辑posts控制器的创建动作。我需要在管理员控制器中渲染newpost动作):

render 'admins/newpost'

Then, it will direct me to the posts : 然后,它将引导我到帖子:

http://localhost:3000/posts

But now, the form is there and I can see the errors. 但现在,表格就在那里,我可以看到错误。 But I need to render the oroginal page which I have the form (dasharea/newpost)! 但是我需要渲染我有表格的原始页面(dasharea / newpost)! How can I do this? 我怎样才能做到这一点? What have I done wrong? 我做错了什么?

My solution: AJAX form. 我的解决方案:AJAX表单。 No more issues. 没有更多的问题。 Update the form on the spot with javascript. 使用javascript在现场更新表单。 You can even redirect using window.location.href = '/your-url' if needed. 如果需要,您甚至可以使用window.location.href = '/your-url'重定向。

If you need to handle html requests, you could always point your form to the url of your choice: 如果您需要处理html请求,您可以随时将表单指向您选择的网址:

form_for @post, url: route_to_current_controller

Handle the create_post action on this controller and make sure your routes are set properly. 处理此控制器上的create_post操作,并确保正确设置路由。 The bad side of it is that you'll be handling posts creation in a different controller than your PostsController. 不好的一面是你将在与PostsController不同的控制器中处理帖子创建。

You might be looking for render template: 'admins/newpost' . 您可能正在寻找render template: 'admins/newpost' If you still get errors regarding missing template, check the format of the request against the available formats for views/admins/newpost. 如果仍然遇到有关丢失模板的错误,请根据views / admins / newpost的可用格式检查请求的格式。

I think you might want to render new , when there are errors present. 我认为你可能想要在出现错误时render new The error message should be populated automagically by rails. 错误消息应由rails自动填充。

Also Can you verify there is a file called newpost.html.erb inside app/views/dasharea ? 你也可以验证app/views/dasharea有一个名为newpost.html.erb的文件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM