0

I'm new to Ruby. Im working on a project and i got stuck at someplace. I have two html pages, company_profile and job. This job page is rendered inside company_profile page and there is an add job button which renders this. This is inside company_profile.html.erb

<% @job_count = 0 %>
<script type="text/html" id="new_company_job_div">
 <%= render 'job', job: Job.new(company_id: @company.id), f: f %>
</script>

and inside my job.html.erb i've

<% @job_count += 1 %>
<%= @job_count %>

and some other codes too.. My issue: First time when i add a job, job_count goes to 1 and from there that job_count value is not increasing. It is stuck there.

I have also written a code to render the jobs page if its present in database.. this is that part of code in comapny_profile that loads the existing columns.

 <% if @company.jobs.present? %>                    
                        <% @company.jobs.each do |job| %>
                            <%= render 'job', job: job, f: f %>
                        <% end %>                       
                    <% end 

%>

That is if there are 2 columns inside jobs table with matching id those 2 will be present in the page as soon as it loads and also with add job we can add more. In this case if 2 jobs are in table job_count will be 2 and on pressing add_count count value will be 3 and from there onwards count_value is not increased. Appending and all are working fine.. pls help

Nidhin S G
  • 1,685
  • 2
  • 15
  • 45

2 Answers2

1
My issue at first time when i add a job job_count goes to 1 and from there taht job_count value is not increasing

The problem is probably because you're using an instance variable

From what I can see, you're reloading the page each time you wish to load job (it's a one time thing); meaning the data is not going to persist between requests

If you want to store a variable over different requests, you'll either have to populate it continuously in the backend, or use a persistent data store - such as a cookie or session


Try this:

<% session[:job_count] = 0 %>
<script type="text/html" id="new_company_job_div">
 <%= render 'job', job: Job.new(company_id: @company.id), f: f %>
</script>

<% session[:job_count] += 1 %>
<%= session[:job_count] %>

This is the best I can do without any further context

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • undefined method `+' for nil:NilClass :( – Nidhin S G Mar 26 '14 at 09:40
  • 1
    Okay! Means `session[:job_count] is not set - try with my updated code – Richard Peck Mar 26 '14 at 09:42
  • No luck.. Count is not increasing. I have 3 columns in my table corresponding to this id and hence count is 3 at first. and then first time when i choose add account the count increased and moved to 4. From there on its not increasing.. – Nidhin S G Mar 26 '14 at 09:47
  • Okay okay okay. You have the value you require in your db? If this is the case, I'll change my answer for you – Richard Peck Mar 26 '14 at 09:48
  • You need to remember I only have your question to work with. Your data in your question is not persistent (which means it will rewrite each time you load the page). If it's in the DB, it makes a different solution – Richard Peck Mar 26 '14 at 09:49
  • I have some values in my db.. its like updating the page... When i take the page if there are 3 columns in my table 3 times this job page will be rendered while loading and again i can add more jobs to the page by clicking add job which has to be saved into the db. – Nidhin S G Mar 26 '14 at 09:55
  • I don't understand - what do you want to do? Each time you iterate through the `jobs` in your DB, you wish to say `Job x`? If that's the case, you could use an `.each_with_index` loop. Can you explain more clearly please? – Richard Peck Mar 26 '14 at 09:58
  • Still don't get it - you've added another thing now? Where did `company_accomplishments` come from? Why do you want to show `@job_count +1` each time? Why does that feature in your application? – Richard Peck Mar 26 '14 at 10:03
  • inside this job page i need to append id for many fields.. n i want these ids like abc_1 abc_2 abc_3... thats what this count is for – Nidhin S G Mar 26 '14 at 10:13
  • You should use [child_index](http://stackoverflow.com/questions/2879208/rails-fields-for-child-index-option-explanation) ;) – Richard Peck Mar 26 '14 at 10:15
  • can u gmme a link where some has used child_index – Nidhin S G Mar 27 '14 at 08:03
  • 1
    Sure! http://pikender.in/2013/04/20/child-forms-using-fields_for-through-ajax-rails-way/ this is where I learnt it – Richard Peck Mar 27 '14 at 09:09
1

Use collection partial:

In app/views/companies/show.html.erb

<%= render @company.jobs %>

In app/views/jobs/_job.html.erb

<%= job_counter %> # value based on the number of jobs you have
<%= job.foo %>
Pierre-Louis Gottfrois
  • 17,561
  • 8
  • 47
  • 71