Rails: render doesn't work, still get `Template is missing`

后端 未结 5 753
一个人的身影
一个人的身影 2021-02-02 08:59

I am currently learning Rails Guides. I went through the steps but still encountered a mistake.

My Ruby version is ruby 2.1.1p76 and the Rails version is

相关标签:
5条回答
  • 2021-02-02 09:42

    You no need template means you could use render nothing: true

    Try like this:

    class ArticlesController < ApplicationController
     def new
     end
    
     def create
      params[:article].inspect
      render nothing: true
     end
    end
    

    Please refer this link click here

    0 讨论(0)
  • 2021-02-02 09:46

    You may want to read through the following documentation.

    Rendering pure text is most useful when you're responding to Ajax or web service requests that are expecting something other than proper HTML.

    0 讨论(0)
  • 2021-02-02 09:48

    Change Rails version in your Gemfile:

    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails', '4.1.6'
    

    Then run:

    bundle install
    

    Make sure that your Rails version now is > 4.1

    0 讨论(0)
  • 2021-02-02 09:54

    In the render method, plain option was added in Rails 4.1 and you are using Rails 4.0.4. So, rails ignored this option and started looking for a template named articles/create as you are in ArticlesController#create action. Obviously, the template doesn't exist so you get the error Template is missing.

    Refer to the discussion on this topic on Github: Introduce render :plain and render :html, make render :body as an alias to render :text

    Now, for using the below mentioned syntax you would need to upgrade to Rails 4.1:

    render plain: params[:article].inspect
    

    With your current version of Rails 4.0.4, you can go for:

    render text: params[:article].inspect
    
    0 讨论(0)
  • 2021-02-02 09:58

    If you want to see textual information of params[:article] on your page then you can use render text

    try this

    class ArticlesController < ApplicationController
      def new
      end
    
      def create
        render text: params[:article].inspect
      end    
    end
    

    You will get

    {"title"=>"First article!", "text"=>"This is my first article."}
    # i.e. your params(whatever params hash contains)
    
    0 讨论(0)
提交回复
热议问题