How do we show dynamic data in .erb file like from a json file?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a view file index.html.erb in my view folder and there I am showing some data in the form of Table(bootstrap table) and this is a static table so I had to write so many lines of HTML for this table.



Now, I want to a show the data in Table in a dynamic form, means I want to keep all the data in a JSON file and want to parse that data in my view file and fetch all the data by having a loop over it so I just have one block of HTML and all the table data should create through that block of code.



Or is there any other method to do this means getting the data dynamically in ruby on rails?



I did google, but everywhere I found this with ruby, but I don't know how to do this in rails, I followed this https://hackhands.com/ruby-read-json-file-hash/ in .erb file this does not work, it works only with .rb file.



Please If you have any tutorial suggestion give me the link.










share|improve this question


















  • 1





    Can you please share some code, your example data in json and html form ?

    – t s
    Jan 3 at 14:04











  • @ts Thanks for the reply, Bro I have written everything in question, where you did not understand.?

    – shashi verma
    Jan 3 at 14:13


















1















I have a view file index.html.erb in my view folder and there I am showing some data in the form of Table(bootstrap table) and this is a static table so I had to write so many lines of HTML for this table.



Now, I want to a show the data in Table in a dynamic form, means I want to keep all the data in a JSON file and want to parse that data in my view file and fetch all the data by having a loop over it so I just have one block of HTML and all the table data should create through that block of code.



Or is there any other method to do this means getting the data dynamically in ruby on rails?



I did google, but everywhere I found this with ruby, but I don't know how to do this in rails, I followed this https://hackhands.com/ruby-read-json-file-hash/ in .erb file this does not work, it works only with .rb file.



Please If you have any tutorial suggestion give me the link.










share|improve this question


















  • 1





    Can you please share some code, your example data in json and html form ?

    – t s
    Jan 3 at 14:04











  • @ts Thanks for the reply, Bro I have written everything in question, where you did not understand.?

    – shashi verma
    Jan 3 at 14:13














1












1








1








I have a view file index.html.erb in my view folder and there I am showing some data in the form of Table(bootstrap table) and this is a static table so I had to write so many lines of HTML for this table.



Now, I want to a show the data in Table in a dynamic form, means I want to keep all the data in a JSON file and want to parse that data in my view file and fetch all the data by having a loop over it so I just have one block of HTML and all the table data should create through that block of code.



Or is there any other method to do this means getting the data dynamically in ruby on rails?



I did google, but everywhere I found this with ruby, but I don't know how to do this in rails, I followed this https://hackhands.com/ruby-read-json-file-hash/ in .erb file this does not work, it works only with .rb file.



Please If you have any tutorial suggestion give me the link.










share|improve this question














I have a view file index.html.erb in my view folder and there I am showing some data in the form of Table(bootstrap table) and this is a static table so I had to write so many lines of HTML for this table.



Now, I want to a show the data in Table in a dynamic form, means I want to keep all the data in a JSON file and want to parse that data in my view file and fetch all the data by having a loop over it so I just have one block of HTML and all the table data should create through that block of code.



Or is there any other method to do this means getting the data dynamically in ruby on rails?



I did google, but everywhere I found this with ruby, but I don't know how to do this in rails, I followed this https://hackhands.com/ruby-read-json-file-hash/ in .erb file this does not work, it works only with .rb file.



Please If you have any tutorial suggestion give me the link.







ruby-on-rails json ruby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 13:56









shashi vermashashi verma

297




297








  • 1





    Can you please share some code, your example data in json and html form ?

    – t s
    Jan 3 at 14:04











  • @ts Thanks for the reply, Bro I have written everything in question, where you did not understand.?

    – shashi verma
    Jan 3 at 14:13














  • 1





    Can you please share some code, your example data in json and html form ?

    – t s
    Jan 3 at 14:04











  • @ts Thanks for the reply, Bro I have written everything in question, where you did not understand.?

    – shashi verma
    Jan 3 at 14:13








1




1





Can you please share some code, your example data in json and html form ?

– t s
Jan 3 at 14:04





Can you please share some code, your example data in json and html form ?

– t s
Jan 3 at 14:04













@ts Thanks for the reply, Bro I have written everything in question, where you did not understand.?

– shashi verma
Jan 3 at 14:13





@ts Thanks for the reply, Bro I have written everything in question, where you did not understand.?

– shashi verma
Jan 3 at 14:13












1 Answer
1






active

oldest

votes


















2














In rails you don't do logic such as parsing JSON in a view (.erb) file.
Since you have a view, you should also have a corresponding controller.
You should do whatever parsing you need to do in the controller file, then set controller instance variables with the data that you want to display, those variable will then be available in the view (.erb) file.



Here is a very simple example from a version of the rails tutorial.



# controller code
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
@gravatar_url = "https://secure.gravatar.com/avatar/#{current_user.gravatar_id}?s=36"
@markers = current_user.followed_users_map_markers
end
end

def help
end

def about
end

def contact
end
end


# partial view code (showing how to use the variable set in the controller)
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>


So I think the answer to how do I parse the JSON in my view (.erb file) is you don't, you parse it in a controller which is a pure ruby (.rb) file, then make the parsed data available to the view (.erb file) by setting controller instance variables which will also be available in the view (.erb file).






share|improve this answer
























  • You should accept the answer if it answers your question.

    – nPn
    Jan 7 at 13:04











  • ok, I will, but if you like then please upvote my question

    – shashi verma
    Jan 8 at 3:41












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023725%2fhow-do-we-show-dynamic-data-in-erb-file-like-from-a-json-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














In rails you don't do logic such as parsing JSON in a view (.erb) file.
Since you have a view, you should also have a corresponding controller.
You should do whatever parsing you need to do in the controller file, then set controller instance variables with the data that you want to display, those variable will then be available in the view (.erb) file.



Here is a very simple example from a version of the rails tutorial.



# controller code
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
@gravatar_url = "https://secure.gravatar.com/avatar/#{current_user.gravatar_id}?s=36"
@markers = current_user.followed_users_map_markers
end
end

def help
end

def about
end

def contact
end
end


# partial view code (showing how to use the variable set in the controller)
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>


So I think the answer to how do I parse the JSON in my view (.erb file) is you don't, you parse it in a controller which is a pure ruby (.rb) file, then make the parsed data available to the view (.erb file) by setting controller instance variables which will also be available in the view (.erb file).






share|improve this answer
























  • You should accept the answer if it answers your question.

    – nPn
    Jan 7 at 13:04











  • ok, I will, but if you like then please upvote my question

    – shashi verma
    Jan 8 at 3:41
















2














In rails you don't do logic such as parsing JSON in a view (.erb) file.
Since you have a view, you should also have a corresponding controller.
You should do whatever parsing you need to do in the controller file, then set controller instance variables with the data that you want to display, those variable will then be available in the view (.erb) file.



Here is a very simple example from a version of the rails tutorial.



# controller code
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
@gravatar_url = "https://secure.gravatar.com/avatar/#{current_user.gravatar_id}?s=36"
@markers = current_user.followed_users_map_markers
end
end

def help
end

def about
end

def contact
end
end


# partial view code (showing how to use the variable set in the controller)
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>


So I think the answer to how do I parse the JSON in my view (.erb file) is you don't, you parse it in a controller which is a pure ruby (.rb) file, then make the parsed data available to the view (.erb file) by setting controller instance variables which will also be available in the view (.erb file).






share|improve this answer
























  • You should accept the answer if it answers your question.

    – nPn
    Jan 7 at 13:04











  • ok, I will, but if you like then please upvote my question

    – shashi verma
    Jan 8 at 3:41














2












2








2







In rails you don't do logic such as parsing JSON in a view (.erb) file.
Since you have a view, you should also have a corresponding controller.
You should do whatever parsing you need to do in the controller file, then set controller instance variables with the data that you want to display, those variable will then be available in the view (.erb) file.



Here is a very simple example from a version of the rails tutorial.



# controller code
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
@gravatar_url = "https://secure.gravatar.com/avatar/#{current_user.gravatar_id}?s=36"
@markers = current_user.followed_users_map_markers
end
end

def help
end

def about
end

def contact
end
end


# partial view code (showing how to use the variable set in the controller)
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>


So I think the answer to how do I parse the JSON in my view (.erb file) is you don't, you parse it in a controller which is a pure ruby (.rb) file, then make the parsed data available to the view (.erb file) by setting controller instance variables which will also be available in the view (.erb file).






share|improve this answer













In rails you don't do logic such as parsing JSON in a view (.erb) file.
Since you have a view, you should also have a corresponding controller.
You should do whatever parsing you need to do in the controller file, then set controller instance variables with the data that you want to display, those variable will then be available in the view (.erb) file.



Here is a very simple example from a version of the rails tutorial.



# controller code
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
@gravatar_url = "https://secure.gravatar.com/avatar/#{current_user.gravatar_id}?s=36"
@markers = current_user.followed_users_map_markers
end
end

def help
end

def about
end

def contact
end
end


# partial view code (showing how to use the variable set in the controller)
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>


So I think the answer to how do I parse the JSON in my view (.erb file) is you don't, you parse it in a controller which is a pure ruby (.rb) file, then make the parsed data available to the view (.erb file) by setting controller instance variables which will also be available in the view (.erb file).







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 14:41









nPnnPn

8,37362346




8,37362346













  • You should accept the answer if it answers your question.

    – nPn
    Jan 7 at 13:04











  • ok, I will, but if you like then please upvote my question

    – shashi verma
    Jan 8 at 3:41



















  • You should accept the answer if it answers your question.

    – nPn
    Jan 7 at 13:04











  • ok, I will, but if you like then please upvote my question

    – shashi verma
    Jan 8 at 3:41

















You should accept the answer if it answers your question.

– nPn
Jan 7 at 13:04





You should accept the answer if it answers your question.

– nPn
Jan 7 at 13:04













ok, I will, but if you like then please upvote my question

– shashi verma
Jan 8 at 3:41





ok, I will, but if you like then please upvote my question

– shashi verma
Jan 8 at 3:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023725%2fhow-do-we-show-dynamic-data-in-erb-file-like-from-a-json-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith