ruby, sinatra, padrino,slim - ruby

I need to develop a simple File Upload
I'm using padrino and slim-templates. Also generated my views using the padrino command generator such as padrino g admin_pages modelName. Now I want to add a fileUpload field to the generated code... I'm getting this error:
undefined method `name' for nil:NilClass
My question is any way to automatically generate the admin page with this functionality or simply add the field manualy?
This is the code:
= f.text_field :newName, :class => 'form-control input-large input-with-feedback',
= f.label :content, :class => 'control-label'
= f.text_area :content, :class => 'form-control input-large input-with-feedback'
= f.file_field :fileimg
= f.submit pat(:save), :multipart => true ,:class => 'btn btn-primary'
Thanks in advance!

It seems that fileimg field is nil for the record f. Make sure that it isn't `nil, and in that case, it shell be validated by a model validation rule.

Related

undefined local variable or method `params' when implementing Search Field

I am doing a tutorial from Stuck.io on implementing a search field on my admin model, so someone can search for a specific admin. the form is appearing, however when I try to use the search I am getting an error field and I don't know where I've gone wrong.
The Exact error I am getting is:
NameError at /admins
undefined local variable or method `params' for #<Class:0x007f91e39df950>
My custom Devise Admin Controller: (to create an index and show page)
class AdminController < ApplicationController
before_action :authenticate_admin!
def index
#admins = Admin.all
# Search Query
#admins = Admin.search(params[:search])
end
def show
#admin = Admin.find_by_admin_ident(params[:id])
end
end
Routes for my custom Admin controller:
admins GET /admins(.:format) admin#index
admin GET /admins/:id(.:format) admin#show
My Admin Model: Search method
class Admin < ActiveRecord::Base
# Search Functionality
def self.search(search)
if search
#admins = Admin.where(["name LIKE ?","%#{params[:search]}%"])
else
all
end
end
end
And finally my Search form:
<div class="col-md-4">
<div class="info-block">
<%= form_tag admins_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], :class => 'form-control', :id => 'admin-search-field' %>
<center><%= submit_tag "Search Users", :class => 'btn btn-default btn-sm', :id => 'search-btn' %></center>
<% end %>
</div>
</div>
Thanks in advance for your input! Please let me know if you need anything more!
You cannot access the params hash inside the model. This is only accessible from controllers.
Instead of:
#admins = Admin.where(["name LIKE ?","%#{params[:search]}%"])
Use:
#admins = Admin.where(["name LIKE ?","%#{search}%"])
And then in the controller, pass the value as you are doing already:
Admin.search(params[:search])

How to call an `f.action` to a custom :delete callback in an Active Admin form

In order to remove (paperclip) images from my objects, I have a custom callback (and route) defined:
ActiveAdmin.register Camping do
#...
member_action :destroy_image, :method => :delete do
camping = Camping.find(params[:id])
camping.image.destroy
redirect_to({:action => :show}, :notice => "Image deleted")
end
end
This works as expected; trough a named route destroy_image_admin_camping => /admin/campings/:id/destroy_image.
The problem is that I cannot find how to add this to the form:
ActiveAdmin.register Camping do
form do |f|
f.inputs "Camping" do
f.input :name
f.input :image
f.action :delete_image, :url => destroy_image_admin_camping_path(#camping.id), :button_html => { :method => :delete }
f.input :description
end
f.actions
end
#...
end
More detailed: I don't know how to pass the "id of the current item we are editing" into destroy_image_admin_camping_path; #camping is nil, f.camping not defined and so I don't know how to pass the item in there.
Is this the right approach? I prefer this "ajax-ish" interface over a more common checkbox-that-deletes-images-on-update, but I am not sure if this will work at all.
There's a few questions here, I'll try to address them all.
How to access the "id of the current item we are editing"
You are pretty close in looking for f.camping. What you want is f.object, so:
destroy_image_admin_camping_path(f.object.id).
NOTE: f.object.id will be nil when it's a new form (as opposed to an edit form), you'll want to check for that with unless f.object.new_record?
"Is this the right approach?"
I'm not sure, really. To me it seems like making requests without actually saving the currently rendered form could create complications, but it might turn out to be a better interface. Anyway, if you want to do the checkbox-that-deletes-images-on-update, this should help you out: Rails Paperclip how to delete attachment?.
However, if you want the ajax-ish approach I think you'll want an <a> tag styled as a button. The problem with actually using a button is that you don't want to submit the form.
Here's an example:
f.inputs do
link_to 'Delete Image', delete_image_admin_camping_path(f.object.id), class: 'button', remote: true, method: :delete
end
remote: true will make it an ajax request and ActiveAdmin gives you a pretty reasonable button class for <a> tags. Updating the interface based on success / failure is left as an exercise to the reader.
Also, you'll probably want to use erb templates for this instead of the Active Admin DSL (see http://activeadmin.info/docs/5-forms.html at the bottom).
Like this:
# app/views/admin/campings/_form.html.erb
<%= semantic_form_for [:admin, #post] do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :image %>
<%# Actually, you might want to check for presence of the image, I don't know Paperclip well enough to demonstrate that though %>
<% unless f.object.new_record? %>
<%= image_tag f.object.image_url %>
<%= link_to 'Delete Image', delete_image_admin_camping_path(f.object.id), class: 'button', remote: true, method: :delete %>
<% end %>
...
<% end %>
<%= f.actions %>
<% end %>

Rails link_to unable to get a confirmation dialog to appear

I am trying to get the link_to to be able to create a link with both options and html options. I need to be able to add the so I went with the method with the do.
My question is: How do I get this to work with the :confirmation. As it is written, it is appending confirm to the url. I would like to pass it in as a html option so it will pull up the dialog box.
Any ideas what I need to do to be able to get it to behave like that?
<li>
<%= link_to :controller => "services", :action => "delete_results", :build => #id, :suite => #cookie_value, :confirm => "Are you sure?" do %>
<i class="icon-trash"></i>
Delete Results
<% end %>
</li>
Thanks
url_options and html_options are separate attributes for link_to. You need to tell ruby that they are different parameters in your list
link_to({:controller => "services", :action => "delete_results", :build => #id, :suite => #cookie_value}, {:confirm => "Are you sure?"})
Wrapping of second hash in {} is optional, but simplifies reading

Ruby rails - text field change event and ajax

Ok.. I am running into this types of issues more often. Most of my html is ajax rendered and I use fields and values from rendered page to make new ajax requests. But none of the events seem to fire. Need to understand this why is that.
This is a simple form with text field which I want to make ajax call on a change event, to skip the form/button. And it is not working. What do I change here?
<div><%= form_tag users_search_path, :remote => true, :id => 'search_form' do %>
<%= text_field_tag :keyword, nil, :maxlength => 11, :size => 20 %># Tried this putting outside of this form, didn't work.
<%= submit_tag " Search ", :id => "search_button", :onclick => "javascript:user_search()"%>
<% end %></div>
<%= text_field_tag :keyword, nil, :maxlength => 11, :size => 20 %> Tried this putting outside of this form, didn't work.
$(function(){
$("#keyword").change(function() {
alert('ok');
$.post('/users/search', function(data) {
$("#search").html(data);
});
});
}
Because the last jquery code is a function, it's not executed until you call it. Remove the first and last line, then it will be run when the page is loaded so it will attach the function defined inside to your text box.

Custom template for padrino admin page

How can I make padrino-admin page generator produce beautiful custom pages?
By default padrino-admin generates pretty ugly admin pages, totally unmaintainable:
.group
=f.label :title
=f.error_message_on :title
=f.text_field :title, :class => :text_field
%span.description Ex: a simple text
.group
=f.label :name
=f.error_message_on :name
=f.text_field :name, :class => :text_field
%span.description Ex: a simple text
--- more annoyingly redundant frak
.group.navform.wat-cf
=f.submit pat(:save), :class => :button
=f.submit pat(:cancel), :onclick => "window.location='#{url(:pages, :index)}';return false", :class => :button
I wrote a nice AdminFormBuilder < AbstractFormBuilder, connected it with set :default_builder, 'AdminFormBuilder', it generates same admin pages from very short code:
= f.inputs :name, :surname, :email
= f.inputs :password, :password_confirmation, :as => :password
= f.input :role, :as => :select, :options => access_control.roles, :descr => 'a simple text'
= f.submits
Now I want padrino g admin_page to generate more of such pages. What should I do?
There are two ways:
1) Make your custom admin gem copying as base the actual padrino-admin
2) Fork the project (where now we support a new admin based on bootstrap) apply your changes and submit a pull request.
Btw the most interesting file for this job is this: https://github.com/padrino/padrino-framework/blob/master/padrino-admin/lib/padrino-admin/generators/admin_page.rb
Here is one-line patch for padrino-admin gem: https://github.com/ujifgc/padrino-framework/commit/b07399bdfbc15d05682237c64580e77558ac9fce
Now I can place copy of original templates folder from padrino-admin-0.10.5/lib/padrino-admin/generators to vendor/padrino-admin/generators and enjoy my own admin page templates.

Resources