View based ActiveAdmin item displaying errors - ruby-on-rails

I have my workers view based on users table, and I need separate item in my ActiveAdmin, so I changed default create/update logic to sth like this:
def update
if params[:worker][:password].blank?
params[:worker].delete("password")
end
#user = User.find(params[:id])
if #user.update_attributes(permitted_params[:worker])
redirect_to "/admin/workers/#{#user.id}"
else
render 'new'
end
end
But when I get the errors on my validation, I can't see any errors on page, just rendering 'new'. I have tried to something like this
resource = #user
But it didn't help.
UPDATE:
My form looks like this:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Details" do
f.input :email, as: :email
f.input :first_name
f.input :last_name
f.input :street_address
f.input :apt
f.input :zip_id, :input_html => {:class => 'zip_selector'}
f.input :phone_number
f.input :is_active
f.input :description
f.input :password, :value => ''
end
f.actions
end
Any ideas ?

Related

Unable to click radio button in Ruby ActiveAdmin

I am displaying a form using ruby on rails as follows.
form do |f|
f.semantic_errors *f.object.errors.keys
# Form creation
f.inputs "User" do
f.input :first_name
f.input :last_name
f.input :email
f.input :phone_number, required: false, as: :number
f.input :password
f.input :text_sms, as: :radio, :label => "Receive sms", :checked => "Yes"
end
f.action
end
This displays the form correctly, but the problem exists when I click the radio button. I am unable to select the radio button.
By default Yes is selected, but when I click on No, it doesn't allow me.
Any help will be appreciated.
Rails version : 4.2.0
There is way, Try to replace your with
f.input :text_sms, as: :radio, :label => "Receive sms",:collection => [ ['Yes','yes',{:checked => true}], ['No','no'] ]
As given below:
form do |f|
f.semantic_errors *f.object.errors.keys
# Form creation
f.inputs "User" do
f.input :first_name
f.input :last_name
f.input :email
f.input :phone_number, required: false, as: :number
f.input :password
f.input :text_sms, as: :radio, :label => "Receive sms",:collection => [ ['Yes','yes',{:checked => true}], ['No','no'] ]
end
f.action
end

Rails ActiveAdmin. How to set default value?

I have code like this:
ActiveAdmin.register Post do
form do |f|
f.inputs "Post Details" do
f.input :title
f.input :body
f.input :published_at, :as => DateTime.now
end
f.actions
end
I want the field :published_at (which is t.datetime) to be set to the current date and time by default. My example doesn't work. How can I achieve this?
Yep. Found the answer myself.
ActiveAdmin.register Post do
form do |f|
f.object.published_at = DateTime.now
f.inputs "Post Details" do
f.input :title
f.input :body
f.input :published_at
...
end
end
You can try with something like this:
<%= f.input :published_at, input_html: {value: "#{Time.now}"} %>

Conditional Show/Hide in Active Admin

I have this form in Active Admin:
form(:html => { :multipart => true }) do |f|
f.inputs 'Home Carousel Image' do
f.input :name
f.input :file, as: :file
f.input :headline_text, as: :html_editor
f.input :button_text
f.input :featured_image?
f.input :headline_text
f.input :button_text
end
actions
end
featured_image? is a boolean. I was hoping to see if a user selects this (switching it to true), only then would the input fields for :headline_text and :button_text be displayed. Otherwise, these two fields will be hidden on the form.
Is this possible?
Thanks
Yes, just use if:
f.input :featured_image?
if f.object.featured_image?
f.input :headline_text
f.input :button_text
end
Use f.object to take instance of your model.

simple_form select-field turns into input-field after validation

I'm using simple_form combined with twitter boostrap for my rails application. And I've one problem that stops me.
<%= f.input :title %>
<%= f.input :url %>
<%= f.input :tag_list, :label => 'Tags' %>
<%= f.input :type_id, :collection => #types, :label_method => :type_name, :value_method => :id, :include_blank => false %>
<%= f.input :description %>
That creates me nice form. But when the validation fails every input-field shows their error.
But not the select field, it only changes from a select-field into a normal input-field filled with the id and I don't know why.
Any ideas?
For some reasons, it seems Rails + Bootstrap + Simple_form keep you select field after validation when the collection is placed into the field itself instead of your controller's action. This should work :
<%= f.input :title %>
<%= f.input :url %>
<%= f.input :tag_list, :label => 'Tags' %>
<%= f.input :type_id, :collection => Type.all.order('type_name'), :label_method => :type_name, :value_method => :id, :include_blank => false %>
<%= f.input :description %>
Hope this helps.
Well I would need more information to help you out.
Could you post your models? or explain what #types is and what it is for.
This is a basic simple for association:
models/type.rb
belongs_to :post
models/post.rb
has_many :types
views/post/_form.html.haml
= f.association :types
Make sure you have a column named "title" or "name" for types.
You need to set #types in methods create or update before render action new or edit respectively
For example
def new
#your_object = YourObject.new
#types = Type.all
....
end
def create
#your_object = YourObject.new(your_object_params)
respond_to do |format|
if #your_object.save
format.html { redirect_to ... }
format.json { render action: 'show' ... }
else
### ! init data for the form ! ###
#types = Type.all
format.html { render action: 'new' }
format.json { render json: ... }
end
end
end

Passing post_id for comment through comments controller

I have two models with a belongs_to/has_many relationship. Posts have many comments, comments belong to posts.
I need to pass the post_id through comments_controller.rb#new.
def new
#post = Post.find(params[:post_id])
#comment = Comment.new(:parent_id => params[:parent_id], :post_id => params[:post_id])
end
comment form:
<%= simple_form_for([#post, #post.comments.new]) do |f| %>
<%= f.input :post_id, :required => false, :as => :hidden %>
<%= f.input :parent_id, :required => false, :as => :hidden %>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
<%= f.button :submit, "Reply" %>
<% end %>
You can pass a params from view to a controller method but I don't think you can pass a params to a controller then to a view.
In your new action, you may have to declare a #variable and define it to be your params where you can use it in your view.
But, you have already have #post.id, why can't you just use that?

Resources