-1

I have two JSON file in my rails project

index.json.jbuilder

json.array! @restaurants, partial: 'restaurants/restaurant', as: :restaurant

and _restaurant.json.jbuilder

json.(restraurant, :id, :name, :address, :description)

When i render my component on my index.html.erb

<%= react_component('RestaurantList', {name: raw(render(template: 'restaurants/index.json.jbuilder')) }) %>

I get this error.

NameError in Restaurants#index

undefined local variable or method `restraurant' for #<#<Class:0x007ffd1f6a61b0>:0x007ffd22e2bea0>
Did you mean?  restaurant
               restaurant_url
               restaurant_path
               restaurants_url
               restaurants_path
               @restaurants

Extracted source (around line #1):
1

json.(restraurant, :id, :name, :address, :description)

any idea ?

Cihan Zengin
  • 103
  • 2
  • 12

2 Answers2

1

In your render call, you're doing this:

render(template: 'restaurants/index.json.jbuilder')

and you should be doing this:

render(template: 'restaurants/index', formats: :json)

This tells Rails that you're trying to render a JSON formatted output. Using the filename to communicate intent won't work. Use the format: option, instead.

And in your _restaurant.json.jbuilder file, change the code to this:

json.(restaurant, :id, :name, :address, :description)

That should take care of that undefined local variable or method error.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
  • Can u look my last answer? i change there something – Cihan Zengin May 16 '16 at 01:32
  • I'm happy to look at the information and see what I can find. It needs to be in a new question. Please accept my answer that solved the `undefined local variable or method error` question that you originally asked. You can accept the answer by clicking the checkmark next to the answer, and it will change color to green. – Michael Gaskill May 16 '16 at 03:00
0

yes i did and somethings happened but i can't anyway get data with json. I already change all with new controller so what i did :

Here is my post controller

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

and here is my views/posts/index.html.erb file

<%= react_component('PostList', {name: raw(render(template: 'posts/index', formats: :json)) }) %>

here is views/posts/index.json.jbuilder file

json.array!(@posts) do |post|
  json.extract! post, :id, :title, :body
  json.url post_url(post, format: :json)
end

here is views/posts/show.json.jbuilder file

json.extract! @post, :id, :title, :body, :created_at, :updated_at

here is my react component file assets/javascripts/components/post_list.js.jsx

var PostList = React.createClass({

  render: function() {  
    return (
      <div>
        {JSON.parse(this.props.posts).map(function(post) { 
          return <Post key={post.id} {... post}  />;
        })}
      </div>
      );
  }

});

I can add post with rails-admin all is working i just want to render this posts with json to view files. But this is showing to me nothing

Cihan Zengin
  • 103
  • 2
  • 12
  • You should not add more information to your question by posting an answer. You can use the `edit` button located beneath the question to add more information. Please move this information to the question and delete this answer. – Michael Gaskill May 16 '16 at 01:18
  • Actually, this is a new question. You should create a new question with this information. It's important on StackOverflow to ask one question and let that be answered. New problems need to have new questions. – Michael Gaskill May 16 '16 at 02:58