I am using SF 1.2.9 (with Propel ORM), to build a website. I have generated admin modules for some of my models. I want to modify the template used to display the Form because there are some fields that I dont want to display (but need) - example the 'slug' field in a form.
Since the templates are autogenerated, I cant make the changes there. How can I specify a template to use for rendering a form in the new/edit view?
In general, to override generated templates with your own, you just have to name your template same as generated one.
But looks like you do not need it. Hint: you can choose which fields to show in admin generator. Please consult symfony reference manual to realize how
To hide or remove fields from a form, I think you should check that link.
To sum up, the admin generator uses a ORM sfForm to render the edit action. So if you remove fields:
// lib/form/doctrine/ADoctrineClassForm.class.php
class ADoctrineClass extends BaseADoctrineClass
{
public function configure()
{
...
unset($this['field_name']
...
You can also change the widget used the render the field by redefining which widget to use:
//in ADoctrineClass configure method also
$this->widgetSchema['a_field_a_want_to_hide'] = new sfWidgetFormInputHidden()
I hope this helps.
Related
I'm using active admin gem. In one table i want admins page to have some extra fields apart from the default functionalities.
Is it possible to have a default active admin page + a provision to render some html.
I want something like this in picture
The extra feature that i was talking was about push notification and message and that text area part
If you want a page with your own content, you could register a page: http://www.activeadmin.info/docs/10-custom-pages.html
But if you want to customise the default html strucure of the index page, you have to re-open the ActiveAdmin::Views::Pages module and override the build_page_content method. You can do this inside your app, for example create a new file in app/lib folder and override the method.
https://github.com/gregbell/active_admin/blob/master/lib/active_admin/views/pages/base.rb#L62
or if you want to customise the index table:
https://github.com/gregbell/active_admin/blob/master/lib/active_admin/views/pages/index.rb
Not a nice solution, but it's work. :/
Unfortunately, it seems index doesn't support custom rendering:
https://github.com/gregbell/active_admin/issues/813#issuecomment-3059957
However, you might have luck using panel, except in my experience that renders above the table ActiveAdmin generates.
I'm trying to set a set of fields to be dynamically displayed on demand. In the model, I've the fields:
attr_accessible ... :instruct1, :instruct2, ... :instruct30
I would like the form to display just instruct1 with a button to add 1 more field until instruct30 is hit and a button to remove one until instruct 1 is hit. All should happen without refreshing page which i think would include some use of AJAX but I couldn't find anything that is similar.
I've searched for something similar but only able to come up with nested form which is not what im looking for as my model is fixed.
The majority of your work is going to be on the client side.
To add and remove form fields dynamically, you have to use javascript.
Check out the HTML that Rails generates for the first field, replicate that and add the additional fields using for example jQuery.
A crude example:
$("#button").click(function() {
$("#theForm")
.append('<input id="instruct2" name="object[instruct2]" type="text">');
});
You'd have to keep track of how many fields you've added or removed.
I'm writing a rails app that allows users to delete records of various sorts. After pressing a delete button, I'd like to show a confirm dialog using bootstrap. I'd like to use this same dialog in several of my views, so I'll need to include the same HTML snippet in most of my pages.
I'm new to rails and I'm still learning the conventions. Can anyone suggest the best (or standard) place to put the dialog code? Should it be a partial in views/layouts/_confirm_delete_dialog.html.erb, should it go inside application.html.erb, or should I put it somewhere else?
Thanks in advance for your advice,
D.
Within your Views folder, you can create a general folder (called whatever you want). If you have a variable that you need to pass through to the general layout, you can definitely do so, but you will want to make sure that the information that you're passing through doesn't cause conflicts with the fields pulled from the model. For example, if you have two models and one has a public field but the other doesn't then you will not want to have a generic message that is using the public field. However, something like the created_at or updated_at would be okay.
You would use a code similar to,
<%= render 'general/simple_message', :f => f %>
In your views folder, you would have a directory called general and a file called _simple_message.html.erb.
I'd like a good source on how to set up controller actions and forms for creating a resource inside the view of another resource that it belongs_to...
Set up your controllers as you would normally. You'll need to use the nested attributes feature of Rails. This enables you to create children objects at the same time as creating their parent using one form.
This is my go-to link for nested attributes. The only change you will need to make if you are running Ruby 1.9.2 is in the setup_person helper. returning has been deprecated so you can change it to:
def setup_person(person)
person.tap do |p|
p.children.build if p.children.empty?
end
end
In typical Rails style, this will just work using standard controllers for each of your resources.
Other links
http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
http://jeffperrin.com/2009/06/04/rails-nested-forms-and-collection_select/
I don't have a web source that documents what I usually do, but I created a gist that documents what I do most often here: https://gist.github.com/900241
The premise of the gist is that you have a project model with many project roles, and you want to edit many project roles in the project form. This is pretty much the classic accepts_nested_attributes_for scenario, and just about any page that talks about it will give you a decent writeup. The problem is, the solutions I've seen have always involved some seriously messy obtrusive JavaScript that escaped your entire form view and threw it in the onClick method of a link. I recently came up with a cleaner unobtrusive approach using jQuery templates.
You don't have to do a thing to your ProjectsController when you move to a nested model. Everything Just Works at the controller level, and you don't even need a ProjectRolesController. (This is why I didn't bother including them in the gist.) At the model level, it's just standard accepts_nested_attributes_for. Where it gets interesting is in the view.
The project form has two form_for blocks: one rendering a jQuery template, and another rendering the project roles form. The jQuery template in turn just renders the project roles form (mmm DRY!), but from within a <script> tag, and with a blank project role. Because the form is within a script tag, it won't get submitted along with the project form, and because the script type is "text/x-jquery-tmpl", this is completely valid markup.
When the user clicks on "Add a Project Role", it fires some jQuery that takes the form within the template, replaces the index with the current date (this is all so this project role can be uniquely identified), and appends it to the end of the project roles section of the form.
When the user clicks on "Delete" next to a project role, it checks to see if this project role is a new record, and if not, it appends a "_delete" hidden field to the end of the form. In either case, it removes the project role div from the DOM.
I generated a custom theme by copying
plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default"
into
plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/mytheme1"
I changed the templates to match my needs (no tables, custom errors display and so on).
./symfony doctrine:generate-module --theme=mytheme1 frontend user User
works as I expected however if I add
$this->embedI18n(array('en','fr'));
to the form class the generator renders the I18n embedded form with and .
Where do this come from? How can I customize it? where are the template files for i18n embedded forms located?
Thanks a lot,
Massimo
Firstly it's not the best idea to overload a theme in the plugin itself. Plugin's shouldn't be touched to allow future updates. You can easily overload the theme in your application.
Secondly something seems to be missing in your post: "to the form class the generator renders the I18n embedded form with and ."
Maybe missing translation indicators are shown?
The template is defined in the formatter of the widgetSchema of your i18n form. By default, it is set to sfWidgetFormSchemaFormatterTable which contains the definition of what you are looking for :
class sfWidgetFormSchemaFormatterTable extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = "<tr>\n <th>%label%</th>\n <td>%error%%field%%help%%hidden_fields%</td>\n</tr>\n",
$errorRowFormat = "<tr><td colspan=\"2\">\n%errors%</td></tr>\n",
$helpFormat = '<br />%help%',
$decoratorFormat = "<table>\n %content%</table>";
}
You can change this by editing your i18n form class initialization. For example, if you want to display the fields in a list :
public function setup()
{
parent::setup();
$formatter = new sfWidgetFormSchemaFormatterList($this->getWidgetSchema());
$this->getWidgetSchema()->addFormFormatter('list', $formatter);
$this->getWidgetSchema()->setFormFormatterName('list');
}
You can also define your own formatter inheriting from sfWidgetFormSchemaFormatter to match your layout preferences.