简体   繁体   English

Ruby on Rails:如果帖子没有保存,我将如何留在同一页面?

[英]Ruby on Rails: How would i stay on the same page if the post is not saved?

def create
    @addpost = Post.new params[:data]
    if @addpost.save
        flash[:notice] = "Post has been saved successfully."
        redirect_to posts_path
    else
        flash[:notice] = "Post can not be saved, please enter information."
    end
end

If the post is not saved then it redirects to http://0.0.0.0:3000/posts , but i need to stay on the page, with text input fields so that user can input data. 如果帖子没有保存,那么它会重定向到http://0.0.0.0:3000/posts ,但我需要留在页面上,带有文本输入字段,以便用户可以输入数据。

post model 发布模型

class Post < ActiveRecord::Base

    has_many :comments
    validates :title, :presence => true
    validates :content, :presence => true
    validates :category_id, :presence => true
    validates :tags, :presence => true
end

new method 新方法

def new
    @arr_select = { 1=>"One",2=>"Two" ,3=>"Three" }
    @categories_select = Category.all.collect {|c| [ c.category_name, c.id ] }
end

new.html.erb new.html.erb

<h3>Add post</h3>

<%= form_tag :controller=>'posts', :action=>'create' do %>
    <%= label :q, :Title %>
    <%= text_field :data, :title, :class => :addtextsize %><br/>
    <%= label :q, :Content %>
    <%= text_area  :data, :content, :rows=>10 , :class => :addtextarea %><br/>
    <%= label :q, :Category %>
    <%= select :data, :category_id, @categories_select %><br/>
    <%= label :q, :Tags %>
    <%= text_field :data, :tags, :class => :addtextsize %><br/>
    <%= label :q, :Submit %>
    <%= submit_tag "Add Post" %>
<% end %>

What should i do ? 我该怎么办 ?

flash.now with render is what you're looking for. flash.now with render是你正在寻找的。

flash.now[:notice] = "Post can not be saved, please enter information."
render :new

Also instead of 而不是

flash[:notice] = "Post has been saved successfully."
redirect_to posts_path

you can just write 你可以写

redirect_to posts_path, :notice => "Post has been saved successfully."

and it will do the same thing. 它会做同样的事情。 It works only with redirect_to though, not with render! 它只适用于redirect_to ,而不适用于渲染!

Something like this should do what you want: 这样的事情应该做你想要的:

flash[:notice] = "Post can not be saved, please enter information."
render :new

UPDATE : You updated your question so I have to update my answer. 更新 :您更新了您的问题,所以我必须更新我的答案。 Render is the right way to do this. 渲染执行此操作的正确方法。 However, it looks like you load some categories and some other collection of stuff in your new method. 但是,您似乎在new方法中加载了一些类别和其他一些东西。 Those same instance variables should be available to your create method. 您的create方法应该可以使用这些相同的实例变量。 The cleanest way to do this is put them into another method and have that method used as a before_filter applied to both create and new . 最简洁的方法是将它们放入另一个方法中,并将该方法用作before_filter ,同时应用于createnew Something like this: 像这样的东西:

before_filter :load_stuff, :only => [:create, :new]

def load_stuff
  @arr_select = { 1=>"One",2=>"Two" ,3=>"Three" }
  @categories_select = Category.all.collect {|c| [ c.category_name, c.id ] }
end

Then your new method is pretty much blank and calling render :new in your create method should work. 然后你的new方法几乎是空白的并且调用render :new create方法中的render :new应该可以工作。

Hey this answer is super late but thought I'd add it for anyone that comes across it. 嘿,这个答案已经很晚了,但我想我会把它添加到遇到它的任何人身上。 Probably the most simple solution for what you want to achieve is to add required: true to all of the form inputs you want filled out. 对于您想要实现的目标,最简单的解决方案可能是为所有要填写的表单输入添加required:true。 Eg 例如

f.text_field :title, required: true, class: "whateverclassyouwant" 

This way the form will ONLY be submitted if these fields have been filled in correctly and if not an error flash message will pop up on the field that it needs to be completed. 这样,只有在正确填写这些字段的情况下才会提交表单,如果没有,则会在需要完成的字段上弹出错误的flash消息。 The default flash messages that pop up can be custom styled also, Google how to do so. 弹出的默认Flash消息也可以自定义样式,Google如何这样做。

This way you can remove the else redirect all together in your create method as it will never get to that point, and just have the if save, flash success etc. 这样你就可以在你的create方法中一起删除else重定向,因为它永远不会到达那一点,只有if保存,flash成功等。

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

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