0

How to send data in database and show in needed part by using json format in ruby on rails3?

my form is like this

<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :login %><br />
    <%= f.text_field :login %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

1 Answer 1

1

In the controller:

def show
  @user = User.find(params[:user_id]
  respond_to do |format|
    format.json { :render => @user }
  end
end

You can customise the JSON output by overriding the as_json in the User model:

  def as_json(options=false)
    self.include_root_in_json = false
    options = (options || {}).reverse_merge(
      :except => [:updated_at, :created_at, :id],
      :include => :some_association
    )
    super(options)
  end
Sign up to request clarification or add additional context in comments.

Comments

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.