I have a few checkboxes coming from nested model (:admin accepts_nested_attributes_for :account_setting). By default, the label for the checkbox is generated from the model's attributes. But I want to use custom labels which I store in the locales folder as key value pairs.
This is the code,
= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
= f.input :email
= f.inputs :receive_email_digest, :as =>:check_boxes, :for => :account_setting, :label => 'My custom label'
And it does not work. I tried :input_html, :member_label.
Does formtastic suppor this? Or do we have to hack it?
inputs is a fieldset. You're telling formtastic to make a fieldset and ordered list with one list item and input, :receive_email_digest, which I presume is a boolean. The :as here is not really having any effect, and :check_boxes is for has_many associations.
You want something like:
= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
= f.inputs do
= f.input :email
= f.inputs :for => :account_setting do
= f.input :receive_email_digest, :label => 'My custom label'
which will render two fieldsets each with a single input. Or perhaps even:
= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
= f.inputs "Email Settings" do
= f.input :email
= f.semantic_fields_for :account_setting do |ff|
= ff.input :receive_email_digest, :label => 'My custom label'
The semantic fields for here is merely a scope and shouldn't output any markup.
I believe both of these will use en.formtastic.labels.<resource_name>.account_setting.receive_email_digest for the check box's label, where resource name is whatever your resource's underscored name is. Check out the formtastic source for how these keys are generated.
Related
Following is my code block of state Model, SatatImage Model and Active Admin Code. In active admin when I try to create a new record or Edit a --record that time I show an error on production server. but works on my localhost in development mode,
---------Error---------------------------
too many arguments for format string
app/admin/state.rb:49:in `block (2 levels) in '
I am using Ruby 1.9, Rails 3,2, activeadmin (0.6.0)
======State Model===============
class State < ActiveRecord::Base
attr_accessible :name, :code
validates :code, :uniqueness => true
has_one :state_image, :dependent => :destroy
accepts_nested_attributes_for :state_image, :allow_destroy => true
.......
end
==============StatImage Model=============
class StateImage < ActiveRecord::Base
attr_accessible :state_id, :stateimage, :image_name
belongs_to :state
mount_uploader :stateimage, StateUploader
end
=======Active Admin part=================
ActiveAdmin.register State do
.....
form(html:{multipart:true}) do |f|
f.inputs "State Form" do
f.input :name, required:true
f.input :code, required:true
end
#line-49#
f.inputs "StateImage", for:[:state_image, f.object.state_image || StateImage.new] do |p|
p.input :stateimage, :as => :file, :label => "Image"
end
f.buttons :submit
end
end
I am using
f.semantic_fields_for
And Formtastic requires you to wrap ALL inputs in an "inputs" block. So this should be:
f.inputs 'State Image' do
f.semantic_fields_for :state_image, (f.object.state_image || StateImage.new) do |p|
p.inputs do
p.input :stateimage, :as => :file, :label => "Image"
p.input :_destroy, :as => :boolean, :required => false, :label => 'Remove image'
end
end
end
Please try this:
form :html => { :enctype => "multipart/form-data" } do |f|
Also upgrade you activeadmin version to 0.6.6
I am upgrading from rails2.3.5 to rails4 and i am facing difficulty to convert link_to_remote into rails 4
link_to_remote("Retire", :url => { :action => :delete, :id => user.id },
:condition => "confirm_retire(#{user.id})", :html => {:style => 'color: #CC0000;float:right;margin-right:8px;'})
I know that we need for this :remote => true with link_to like
link_to('Retire', {:action => :delete, :id => user.id},
:condition => "confirm_retire(#{user.id})", :remote => true,
:class => 'retire')
But i am facing problem with :condition => "confirm_retire(#{user.id})",
how to handle this ?, i did lot of research but not found any solution
The condition option isn't supported by link_to. You could perform that check in a js file using a data attribute on the link element to provide a value for the condition.
link_to('Retire', {:action => :delete, :id => user.id},:remote => true, :class => 'retire', :data-user-retire => "your_condition")
$('.retire').on('click', function(){
if ($(this).attr('data-user-retire') == "your condition") {
return true;
} else {
$(this).append("some message")
return false;
}
});
This is just an example, you'll need to amend it for your requirements.
I have ajax comments in my rails app and it all works in the posts show but I'd like to add the form to render the comments in the index page.
I'm using sorl when rendering the index page and I tried to include comments in the post model like this:
searchable(:include => :comments) do
code....
end
but that doesn't render the comments below the posts in the index page, even though I have this in the view
<%= render :partial => 'comments/comment',:collection => #comments, :as => :comment %>
So to help you understand, here is all the code:
_post.html.erb:
<%= simple_form_for [post, Comment.new], :url => comments_path, :remote => true do |f| %>
<%= image_tag current_user.avatar.url(:small) %>
<%= f.input :body, placeholder: 'write a comment...', :input_html => { :rows => "1"}, :label => false %>
//the below two lines do not work, the commentable_id is 0 and commentable_type is blank
//post.comments.commentable_id doesn't work either
<%= f.input :commentable_id, :as => :hidden, :value => Comment.new.commentable_id %>
<%= f.input :commentable_type, :as => :hidden, :value => Comment.new.commentable_type %>
<%= button_tag(type: 'submit', :id => 'commentsend') do %>
<i class="fa fa-check"></i>
<% end %>
<% end %>
<%= render :partial => 'comments/comment',:collection => #comments, :as => :comment %>
Posts_controller:
def index
#user_count = User.where(:school => current_user.school).count
#blub_count = Post.where(:category => "status").where(:school => current_user.school).count
#items_count = Post.where(:school => current_user.school).where.not( :category => "status").count
#search = Post.search(:include => :comments) do #Post.search do
fulltext params[:search]
with(:school, current_user.school)
paginate :page => params[:page], :per_page => 20
order_by(:updated_at, :desc)
end
#posts = #search.results
respond_to do |format|
format.js
format.html
end
end
def show
#post = Post.find(params[:id])
#comments = #post.comment_threads.order('created_at desc')
#new_comment = Comment.build_from(#post, current_user, "")
end
comments_controller:
def create
#comment_hash = params[:comment]
#obj = #comment_hash[:commentable_type].constantize.find(#comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
#comment = Comment.build_from(#obj, current_user.id, #comment_hash[:body])
if #comment.save
render :partial => "comments/comment", :locals => { :comment => #comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
comments model:
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
validates :body, :presence => true
#validates :user, :presence => true
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_votable
belongs_to :commentable, :polymorphic => true
# NOTE: Comments belong to a user
belongs_to :user
belongs_to :post
# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
new \
:commentable => obj,
:body => comment,
:user_id => user_id
end
#helper method to check if a comment has children
def has_children?
self.children.any?
end
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
where(:user_id => user.id).order('created_at DESC')
}
# Helper class method to look up all comments for
# commentable class name and commentable id.
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
}
# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
The comments form shoudl be like this:
<%= simple_form_for Comment.build_from(post, current_user, ""), :url => comments_path, :remote => true do |f| %>
I have to assign the comment the current post ID(commentable_id) and current user id for commentable_type to build the comment
in the view, to render the comments I used
<%= render post.comment_threads.order('created_at desc') %>
the only issue is the the render is ignoring the div
i am trying to delete some rows on my database but when i call destroy action, update action works.
this is delete.html.haml
=form_for #post, :url => {:action => 'destroy', :id => #post.id} do |f|
this is route
resources :post
root :to => "post#index"
match '/delete/:id/', :to => "post#delete"
What's the problem? Anybody does understand?
The :action should be delete, to match the name of the action in your controller:
= form_for #post, :url => {:action => 'delete', :id => #post.id} do |f|
If you are using rails 3 then you need to make sure you have <%= csrf_meta_tag %> in your header. I put mine after my <%= javascript_include_tag... %> tags. Also make sure that you include the rails.js. (This will be on your layout files).
More details here: http://railsforum.com/viewtopic.php?id=38460 Look toward the bottom.
The root entry in your routes should be placed after the matchblock. Plus: seems like you mixed up the controller action and the http verb: use delete in the form action and destroy in the controller action call:
=form_for #post, :url => {:action => 'delete', :id => #post.id} do |f|
resources :post
match '/delete/:id/', :to => "post#destroy"
root :to => "post#index"
I'm trying to make a simple room management service. The rooms have these properties:
class Room
include DataMapper::Resource
validates_is_unique :number
property :id, Serial
property :number, Integer
property :guest, String
property :status, Enum[ :free, :occupied ], :default => :free
end
Then I create a new room like this
post '/new' do
content_type :json
#room = Room.new :guest => params[:guest],
:number => params[:number],
:status => params[:status]
if #room.save
{ :number => #room.number, :guest => #room.guest, :status => #room.status }.to_json
end
end
through this haml form
%form#new_message{:action => '/new', :method => 'post'}
%p
%input{:type => "text", :id => "number", :name => "number"}
%input{:type => "text", :id => "guest", :name => "guest"}
%input{:type => "checkbox", :id => "status", :name => "status", :value => "occupied"}
%input{:type => "submit", :value => "post"}
When the box is checked the :status is "occupied" but when I leave it unchecked the object won't save. I thought it would work since it is defaulted to "free" but no...
For whatever stupid reason, checkboxes do not get submitted if they are not clicked. This means they are not in the hash that hits your app. When you say :status => params[:status] you are really saying :status => nil. Since you have set a value, it checks that against your enum, and nil is not in your enum, so it fails validations. (based on how you are using this, doesn't it seem like it should be a boolean called either "occupied" or "available" ?)
Anyway, you could either explicitly set it to free, or not set it at all, and let the default take care of it. That is what I opted for when checking it, by moving it into a mass assignment. The code I used is below.
require 'rubygems'
require 'sinatra'
require 'haml'
require 'dm-core'
require 'dm-validations'
require 'dm-types'
require 'dm-migrations'
require 'sqlite3'
configure do
class Room
include DataMapper::Resource
validates_uniqueness_of :number
property :id, Serial
property :number, Integer
property :guest, String
property :status, Enum[ :free, :occupied ], :default => :free
end
set :sessions , true
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup( :default , "sqlite3://#{Dir.pwd}/development.sqlite3" )
DataMapper.finalize
DataMapper.auto_upgrade!
end
get '/' do
#rooms = Room.all
haml :index
end
post '/new' do
p params
#room = Room.new params[:room]
if #room.save
session[:flash] = "room reserved"
redirect '/'
else
session[:flash] = #room.errors.to_a
redirect '/new'
end
end
get '/new' do
haml :new
end
__END__
##layout
!!!
%html
#flash
= session[:flash].inspect
= yield
##new
%form#new_message{:action => '/new', :method => 'post' , :name => 'room' }
%p
%input{:type => "text", :id => "number", :name => "room[number]"}
%input{:type => "text", :id => "guest", :name => "room[guest]"}
%input{:type => "checkbox", :id => "status", :name => "room[status]", :value => "occupied"}
%input{:type => "submit", :value => "post"}
##index
%table
- #rooms.each do |room|
%tr
%td= room.number
%td= room.guest
%td= room.status
View the HTML source of your web-form! There should be a hidden field which sets the unchecked checkboxes to '0' as the default , in case nobody checks them...
see also:
http://railscasts.com/episodes/17-habtm-checkboxes?autoplay=true (towards the end)