简体   繁体   中英

missing partials with rails

def some_action
  @posts = Post.all
  render partial: 'layouts/things'
end

In my layouts directory I have things as partial ( _things.html.erb )

My other partials work fine. But it doesn't render partial throwing exception as

Missing partial layouts/things with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/my/path/appname/app/views"

Edit-1

  def some_action
    @posts = Post.all
    respond_to do |format|
      format.json
    end
  end

Ok i changed my controller link this but then too same exception.

Edit-2

I have created in my views/controller_name/_some_action.json.erb

This is the controller

  def some_action
    @posts = Post.all
    respond_to do |format|
      format.json { render json: @posts }
    end
  end

My _some_action.json.erb file has

<%= render partial: 'shared/information', post: @posts.name %>

And created _information..js.erb in views shared directory

MY _information.js.erb has sample text as

test

I am getting json response from controller i checked with inspect element. But it is not render actual text i need (ie, test )

well, IMHO you need to name your partial like

_things.js.erb

or no?

As Jacub Kuchar mentions, if you're trying to provide a JSON response, your partial should be named accordingly (ie _things.js.erb ). You should also put it in the 'shared' directory rather than 'layouts' as it's a partial, not a layout.

However, I'd also keep the logic of which view to render of the controller and let the view itself handle it.

So your controller can simply say:

respond_to :json # (and whatever other response types you want to support: xml, etc)

def some_action
  respond_with @posts = Post.all
end

And then having a matching view, under views/{controller name}/some_action.js.erb, which says:

<%= render partial: 'shared/things', posts: @posts %>

That way your controller isn't polluted with view logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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