Ajax bootstrap modal in rails 4.. Newbie here!

83 views
Skip to first unread message

Pepeng Agimat

unread,
Oct 18, 2014, 10:09:01 AM10/18/14
to rubyonra...@googlegroups.com
Good day, I'm a beginner in rails I already develop a simple app in rails but i want to upgrade the app to be an ajax, I already created the first model customer but in the second model project where customer has many projects and project belongs_to customer, those ajax form modal that i copy in a tutorial didn't work. I experiencing some error in CRUD in project part.. I'm not familiar in nested resources and the Ajax part.

this is the error: "ActiveRecord::RecordNotFound (Couldn't find Customer without an ID):
  app/controllers/projects_controller.rb:14:in `create'"


Project Controller
******************************************************
class ProjectsController < ApplicationController
respond_to :html, :js
def index
@customer = Customer.find(params[:customer_id])
end

def new
customer = Customer.find(params[:customer_id])
end

def create
@customer = Customer.find(params[:customer_id])
@project = Customer.projects.create(project_params)
end

private

def project_params
params.require(:project).permit(:project_name, :project_description, :project_started, :project_estimated_to_end, :project_status)
end

end
***************************************************
Project model and Customer model
**************************************************
class Project < ActiveRecord::Base
belongs_to :customer
end

class Customer < ActiveRecord::Base
has_many :projects
validates :customer_name, presence: true
validates :customer_company, presence: true
validates :customer_email, presence: true
validates :customer_desc, presence: true
end

****************************************************
projects/_form.html.erb

***************************************************

<%= form_for @project, remote: true, html: { class: "form-horizontal", style: "display:inline;" } do |f| %>

  <div class="modal-body">
    <ul class="errors"></ul>

    <div class="control-group">
      <%= f.label :project_name, class:"control-label" %>
      <div class="controls">
        <%= f.text_field :project_name %>
      </div>
    </div>
    <div class="control-group">
      <%= f.label :project_started, class: "control-label" %>
      <div class="controls">
        <%= f.date_select :project_started %>
      </div>
    </div>
    <div class="control-group">
      <%= f.label :project_estimated_to_end, class: "control-label" %>
      <div class="controls">
        <%= f.date_select :project_estimated_to_end %>
      </div>
    </div>
    <div class="control-group">
      <%= f.label :project_status, class: "control-label" %>
      <div class="controls">
        <%= f.select :project_status,['Doing','Done'], prompt: true %>
      </div>
    </div>
    <div class="control-group">
      <%= f.label :project_description, class: "control-label" %>
      <div class="controls">
        <%= f.text_field :project_description %>
      </div>
    </div>
  </div>
  <div class="modal-footer">
    <%= f.submit class: "btn btn-primary" %>
    <%= link_to "Cancel", "#", class: "btn", data: {dismiss: "modal"} %>
  </div>
<% end %>

*********************************************
projects/_index.html.erb
*********************************************

<% @customer.projects.each do |project| %>
  <tr>
    <td><%= project.project_name %></td>
<td><%= project.project_description %></td>
<td><%= project.project_started %></td>
<td><%= project.project_estimated_to_end %></td>
<td><%= project.project_status %></td>
  </tr>
<% end %>

***********************************************

projects/_new.html.erb
***********************************************
<div class="modal-header">
<h3>New Project</h3>
</div>
<%= render "form" %>

**********************************************
projects/_save.html.erb

*********************************************
$("ul.errors").html("")
<% if @project.errors.any? %>
  <% @project.errors.full_messages.each do |message| %>
    $("ul.errors").append($("<li />").html("<%= message.html_safe %>"))
  <% end %>
<% else %>
  $(".product-index").html("<%= escape_javascript(render 'index') %>")
  $("#product-modal").modal("hide")
<% end %>

******************************************
projects/create.js.erb
******************************************
<%= render "save" %>

******************************************

projects/index.html.erb
************************************

<section class="container">
  <section class="row">
    <h1>Project Table</h1>

    <%= link_to "New Project", new_customer_project_path(@customer), remote: true, class: "btn btn-primary" %>

    <div class="new-product"></div>

    <table class='table' id='people_table'>
      <thead>
        <tr>
          <th>Project Name</th>
          <th>Project Description</th>
          <th>Projct Started</th>
          <th>Project Estimated to end</th>
          <th>Status</th>
          <th>Action</th>
        </tr>
      </thead>

      <tbody class="product-index">
        <%= render "index" %>
      </tbody>

    </table>

    <div id="product-modal" class="modal fade">
      <div class="modal-dialog">
       <div id="inner-product-modal" class="modal-content"></div>
      </div>
    </div>
  </section>
</section>

**********************************

projects/new.js.erb
*******************************************

$("#inner-product-modal").html("<%= escape_javascript(render 'new') %>")
$("#product-modal").modal("show")
*************************************************

routes.rb

  resources :customers do
    get "delete"
    resources :projects
  end

  resources :projects


  root :to => "customers#index"



Hope you can help me.. Thank you!

Colin Law

unread,
Oct 18, 2014, 2:14:12 PM10/18/14
to rubyonra...@googlegroups.com
On 18 October 2014 15:09, Pepeng Agimat <eebas...@gmail.com> wrote:
> Good day, I'm a beginner in rails I already develop a simple app in rails
> but i want to upgrade the app to be an ajax, I already created the first
> model customer but in the second model project where customer has many
> projects and project belongs_to customer, those ajax form modal that i copy
> in a tutorial didn't work. I experiencing some error in CRUD in project
> part.. I'm not familiar in nested resources and the Ajax part.
>
> this is the error: "ActiveRecord::RecordNotFound (Couldn't find Customer
> without an ID):
> app/controllers/projects_controller.rb:14:in `create'"
>
> ...
> def create
> @customer = Customer.find(params[:customer_id])

I guess that params[:customer_id] is null. Check in development.log
and you will see what parameters are being posted.

Colin

Sampath Munasinghe

unread,
Oct 19, 2014, 10:05:58 PM10/19/14
to rubyonra...@googlegroups.com
Use customer has_many: :projects
accepts_nested_attributes_for :projects

and then make customer view to handle project data or  ( if u need above approach ) add customer id to above _form.html. 

if u need further help, please let me know. :)


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvura5TnA%3DZTh6ajPxNcDNiCORnrZUcd4LGmHdB%3DCpbLw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Pepeng Agimat

unread,
Oct 20, 2014, 9:09:25 AM10/20/14
to rubyonra...@googlegroups.com
Thank you so much Mr. Colin I solved the problem. I forget i used the instance of the customer
 [def create
@customer = Customer.find(params[:customer_id])
@project = Customer.projects.create(project_params)
end] .. 

 It should be 

 [def create
@customer = Customer.find(params[:customer_id])
@project = @customer.projects.create(project_params)
end] .. 

:D

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Pepeng Agimat

unread,
Oct 20, 2014, 9:50:00 AM10/20/14
to rubyonra...@googlegroups.com
Thank you so much Mr. Sampath. I solved the problem. I forget that  i should used the instance of the customer
 [def create
@customer = Customer.find(params[:customer_id])
@project = Customer.projects.create(project_params)
end] .. 

 It should be 

 [def create
@customer = Customer.find(params[:customer_id])
@project = @customer.projects.create(project_params)
end] .. 

:D is it okay to you sir if I ask some question if I can't resolve some problem or become my mentor. I want to learn more in rails 4. :)  

Reply all
Reply to author
Forward
0 new messages