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 %>
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
Related
I have the following two _forms:
user form
<%= simple_form_for(#user, :url => #target) 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 %>
<%= f.input :email, :label => "User Email" %>
<%= f.input :password, :label => "User Password" %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.button :submit %>
<% end %>
tenant form
<%= simple_form_for(#tenant, :url => #target) do |f| %>
<% if #tenant.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#tenant.errors.count, "error") %> prohibited this tenant from being saved:</h2>
<ul>
<% #tenant.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :name, :label => 'Name', :required => true %>
<%= f.input :billing_email, :label => 'Email', :required => true %>
<%= f.input :country, :label => 'Country', :required => true %>
<%= f.button :submit %>
<% end %>
I have come across the following post from stackoverflow f.error_messages in Rails 3.0
Here there is method so that error messages can be returned from the simple form by using f.error_messages but I have been unable to get this working as I am unsure whereabout this method should be saved. Anyone got any hints? The method is as follows:
class StandardBuilder < ActionView::Helpers::FormBuilder
def error_messages
return unless object.respond_to?(:errors) && object.errors.any?
errors_list = ""
errors_list << #template.content_tag(:span, "There are errors!", :class => "title-error")
errors_list << object.errors.full_messages.map { |message| #template.content_tag(:li, message) }.join("\n")
#template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
end
end
Just add error: false to your inputs this will not clear the css but will clear the inline errors
f.input error: false
Edit:
From http://ruby.railstutorial.org/book/ruby-on-rails-tutorial
/app/views/shared/_error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
in VIEW
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
This is the login page for my app but the role selected for a user is not being reflected in the database so i want to try changing form_for into form_tag to see if the data gets submitted.
<%= 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 :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<% if #current_method == "new" %>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_tag users_path, methods=> post:do %>
<% 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">
<%= label_tag 'email' %><br />
<%= text_field_tag :email, params[:email], :placeholder => "Email" %>
</div>
<div class="field">
<%= label_tag 'username' %><br />
<%= text_field_tag :username, params[:username], :placeholder => "Username" %>
</div>
<% if #current_method == "new" %>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag 'password' %>
</div>
<div class="field">
<%= label_tag :password_confirmation %><br />
<%= f.password_field_tag 'password_confirmation' %>
</div>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= submit_tag 'submit' %>
</div>
<% end %>
So I have been trying to follow some paperclip tutorials and adjusting them to rails 3. I follow the steps and got an error once I started to add the code needed for the _form and show.htm.erb files. This is the error I get.
Error Message:
ActionView::Template::Error (undefined method `label' for nil:NilClass):
11: </div>
12: <% end %>
13: <div class="field">
14: <%= form.label :photo, "Photo" %>
15: <%= form.file_field :photo %>
16: </div>
17: <div class="field">
Form:
<%= form_for #user, :html => { :multipart => true } do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<div>
<% end %>
<div class="field">
<%= form.label :photo, "Photo" %>
<%= form.file_field :photo %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>`
form.label doesn't make sense, since you're calling your form variable f inside your block, with the line form_for ... do |f|.
You need to use f.label etc.
Hi
Im working with a form and have added :remote => true to the form and the destroy link. But when I do that this happends:
I submit the form and it makes 2 versions that are the same.
I destroy, I get prompted twice?
If i remove the :remote => true it only makes one version and only prompt once.
This is my form
<%= form_for([#comment], :remote => true) do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %>
<br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %>
<br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.hidden_field :user_id %>
<%= f.hidden_field :retailer_id %>
<%= f.hidden_field :product_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my destroy link
<%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete, :remote => true %>
Am I doing something very wrong here? Thanks in advance!
I think you have included your rails.js file twice. that might be the problem. check it.
i did a scaffold to make my DB and menus for group i need a drop down box instead of a text field
here is the code i have atm
<%= form_for(#ad) do |f| %>
<% if #ad.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#ad.errors.count, "error") %> prohibited this ad from being saved:</h2>
<ul>
<% #ad.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :ad_name %><br />
<%= f.text_field :ad_name %>
</div>
<div class="field">
<%= f.label :group %><br />
<%= f.text_field :group %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :credits %><br />
<%= f.text_field :credits %>
</div>
<div class="field">
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
if you can let me know how to change group to a pull down with selections it would be helpful.
Have you tried changing f.text_field to f.select?
You need something like
<div class="field">
<%= f.label :group %><br />
<%= f.select :group, Group.all.collect{|g| [g.id, g.name]} %>
</div>
That is to say, pass as second argument the list [id, label] of the field you want to generate.