Op deze pagina vindt u een aantal problemen waarvoor Suares & Co oplossingen zocht.
Technology
On this page some solutions we found for some uncommon issues.
CSS-only inline inplace edit (no javascript) in Ruby on Rails
16-08-2008
I was looking around for instructions how to use inline / inplace editing in Rails. The Google showed not many promising things. There is a plugin that seems to need to be patched, and a lot of confusion. In any case, I couldn't make the plugin work, so I tried to get around all of that. Here's what happend:
In the view that shows the record, I added a AJAX remote link as an 'edit' button
<!-- normal text field -->
<span id="field_<%= field -%>">
<%= h(@client[field]) -%>
<%= link_to_remote "edit",
:url => { :action => "edit",
:id => @client.id,
:field => field },
:update => 'field_' + field
%>
</span>
So, 'field' is the name of the current field. The app is cycling trough a list of fields, like ['name','address','city']. If the field is 'address' then the result is a span that is named 'field_address'. The field name is also passed on as a parameter to the edit action.
The edit action looks like this:
def edit
@client = Client.find(params[:id])
render(:layout => false)
end
In views/index, there is 'edit.rhtml' to provide the template for the edit action. The output of this file will replace the span for that field. So if the span contains the value of the field and an edit button, then after clicking the edit button, it will contain a form field with the value, a 'Send' button and a 'cancel' link.
Why not use another button to implement the cancel action? Well, there's a bug somewhere that prevents this. Please note that the text field has it's own CSS class defined.
The second line puts the value from the form into the record, whatever the name of the field is. In real life, you might want some validation and error handling before you save.
The update template should show the value of the field and an edit button:
I leave it as an exercise to implement the cancel link.
You will have noted that this is not a javascript-free solution. We need the prototype library to do the AJAX stuff, of course. In views/layouts/index.rhtml: