【问题标题】:Rails 4 - How to render the same view after submit formRails 4 - 如何在提交表单后呈现相同的视图
【发布时间】:2016-03-17 04:45:26
【问题描述】:

提交表单后如何渲染相同的视图?

我无法呈现自定义的部分错误消息。我想这是因为我需要渲染相同的视图,但我不知道该怎么做。我尝试了所有注释的行,因为我需要

   def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = @micropost.comments.create(comment_params)
    @comment.user_id = current_user.id #or whatever is you session name
    if @comment.save
      flash[:success] = "Comment created!"
      redirect_to :back
    else
      @new_comment = @micropost.comments.new
      #flash[:danger] = "Max 140 caracteres - No puede estar en blanco!" #works but is static single comment.
      #render 'microposts/show', locals: {micropost: @micropost} # I need to define all variables to in this controller again and pass to the partial (It's not DRY) 
      #redirect_to micropost_url(@micropost)  # works but not show partial errors messages
    end
  end

路线:

  resources :microposts do
    member do
      get :likes
    end
    [:create, :destroy, :show, :index]
    resources :comments
  end

【问题讨论】:

    标签: validation ruby-on-rails-4 activerecord


    【解决方案1】:

    因为闪存消息是简单的名称值对散列。 我认为您应该像这样在视图中或部分给出 Flash 消息:-

    <% flash.each do |key, value| %>
            <div class="alert alert-<%= key %>"><%= value %></div>
          <% end %>
    

    您应该在控制器操作中定义闪存消息。你已经完成了。

    希望它有效!

    【讨论】:

      【解决方案2】:

      在你的创建

      def create
          @micropost = Micropost.find(params[:micropost_id])
          @comment = @micropost.comments.create(comment_params)
          @comment.user_id = current_user.id #or whatever is you session name
          respond_to do |format|
              if @comment.save
                  flash[:success] = "Comment created!"
                  redirect_to :back
                  format.html { redirect_to @comment, notice: 'Comment created.' }
                  format.json { render :show, status: :created, location: @comment }
              else
                  format.html { render :new }
                  format.json { render json: @comment.errors, status: :unprocessable_entity }
                  #flash[:danger] = "Max 140 caracteres - No puede estar en blanco!" #works but is static single comment.
                  #render 'microposts/show', locals: {micropost: @micropost} # I need to define all variables to in this controller again and pass to the partial (It's not DRY) 
                  #redirect_to micropost_url(@micropost)  # works but not show partial errors messages
              end
          end
      end
      

      【讨论】:

      • 谢谢,“新”模板中需要什么?使用您的解决方案,我得到:“缺少模板 cmets/new,application/new”
      【解决方案3】:

      阅读我可以看到这是custom validations errors form controller inside other parent controller rails 3.1 的副本。

      但是,我看到的唯一解决方案是违反 DRY 并在 Comment#create 中定义父视图所需的所有变量。

      这也会导致在另一个不正确的 url /post/:id/comment 中显示帖子页面。

      现在我明白为什么其他人建议不要使用嵌套资源

      我不敢相信没有针对此类常见问题的简单、开箱即用的解决方案。

      【讨论】:

        猜你喜欢
        • 2018-11-17
        • 2022-10-13
        • 2017-01-20
        • 2014-10-07
        • 1970-01-01
        • 2020-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多