1
Rails 5.2
datatables

I am following a mini tutorial, on implementing databables with Rails. The table

 class BlogDatatable < AjaxDatatablesRails::ActiveRecord

  def view_columns
    @view_columns ||= {
      id:          { source: "Blog.id" },
      user:        { source: "Blog.user_id"}
      title:       { source: "Blog.title" },
    }
  end

  def data
    records.map do |record|
      {
          id:          record.id,
          title:       record.title,
          DT_RowId:    record.id,
      }
    end
  end

  def get_raw_records
    Blog.all
  end

end

What I really want to display for the column user, is the user email, but the user email is in the table Users. How do I implement this with datatables, and stil be able to do sorting, based on the email, not the user_id that's in the blogs table?

6
  • You have a better option to implement the Datatable using json and jbuilder in rails Commented Oct 28, 2019 at 11:32
  • @AbhishekAravindan Would you be able to expand on that? Commented Oct 28, 2019 at 11:47
  • I assume you need a datatable about about blogs and you need it on Blogs index page...is it?? Commented Oct 28, 2019 at 12:26
  • Yes, this will be for the index view Commented Oct 28, 2019 at 12:28
  • did you installed gem 'jquery-datatables' ?? Commented Oct 28, 2019 at 12:31

1 Answer 1

1

Try this method:

Assuming you have the routes set to resources: blogs

Your table in the index.html.erb will be

<table class="responsive nowrap table table-hover" id="dttb-blogs" data-sort="true" data-source="<%= url_for(format: :json)%>">
 <thead>
  <tr>
   <th data-data="title">Title</th>
   <th data-data="user_email">User</th>
   <th data-data="url" data-orderable="false" data-class-name="all" data-searchable="false" class="skip-export" width="100px"></th>
  </tr>
 </thead>
</table>

Add _blog.json.jbuilder in blogs

json.extract! blog, :id, :title, :user_id, :created_at, :updated_at
json.user_email blog.user.email
json.url blog_url(blog, format: :json)

Add index.json.jbuilder in blogs

json.set! :data do
json.array! @blogs do |blog|
json.partial! 'blogs/blog', blog: blog
json.url  "
          #{link_to 'Show', blog }
          #{link_to 'Edit', edit_blog_path(blog)}
          #{link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?' }}
          "
end
end

This will create the datatable using json object by taking the @blogs instance variable from your index action

and for your question how to show email in the user column which is in the user table, Assuming you have referenced the user table with blogs table, ie

in your blogs model you should have an association:

belongs_to :user

now you wil get the user email by blog.user.email

Sign up to request clarification or add additional context in comments.

1 Comment

Why would I do all this, when I can do: render json: { data: @blogdata } from the controller method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.