0
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 (i.e., test)

Prabhakaran
  • 3,900
  • 15
  • 46
  • 113
  • Are you trying to make a JSON request to that action? Check out the `:formats` option in your error message. – Marcelo De Polli Oct 02 '13 at 04:39
  • Yea it is an ajax request with `json` datatype – Prabhakaran Oct 02 '13 at 04:40
  • Perhaps this will help you: http://stackoverflow.com/questions/2566759/how-can-i-generate-json-from-respond-to-method-in-rails – Marcelo De Polli Oct 02 '13 at 04:42
  • You have named your view wrongly. Instead of `_some_action.json.erb` you need `some_action.json.erb` (no initial underscore - only the names of partials, not view templates, begin with an underscore). Also it doesn't make sense to call the variable to store @posts 'post' - keep it plural. Also the two dots in `_information..js.erb` should be just one. Typos, perhaps? Maybe you need to slow down as you type :) – micapam Oct 02 '13 at 13:05
  • @micapam Did all changes as you mentioned. It is just responding with json in inspect element. BUt the actual `test` text not showing – Prabhakaran Oct 02 '13 at 13:13

2 Answers2

1

well, IMHO you need to name your partial like

_things.js.erb

or no?

Jakub Kuchar
  • 1,665
  • 2
  • 23
  • 39
  • Why? it's another thing... partial is `_name.html.erb` or `_name.html.haml` and there is js & ruby file `name.js.erb` – user2503775 Oct 02 '13 at 06:11
  • convention over configuration idea is presented here. Basically, you can render 'whatever you want' with :format option, but since you dont specify :format and making AJAX call rails rendering mechanism expecting js format in name of partial. – Jakub Kuchar Oct 02 '13 at 06:18
  • @JakubKuchar Can u please check my `edit-1`. Can you please briefly explain with code it I am unable to get you. – Prabhakaran Oct 02 '13 at 07:44
1

As Jacub Kuchar mentions, if you're trying to provide a JSON response, your partial should be named accordingly (i.e. _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.

micapam
  • 762
  • 5
  • 12
  • as @micapam said that should be it – Jakub Kuchar Oct 02 '13 at 08:42
  • Ok now i am getting static texts from `things partial`. But now i need to list all posts in my `things partial view and render it`. How can i do it. Can you add a sample things view. – Prabhakaran Oct 02 '13 at 14:56
  • I suggest you take a look at the Rails guide to layouts and rendering: http://guides.rubyonrails.org/layouts_and_rendering.html Your question here is about missing partials. Since that's fixed, I suggest you accept the answer. If you need more help with ERB, try and get as far as you can with the guide and then, if you get stuck, ask another (_specific_) question on SO. – micapam Oct 02 '13 at 20:48