Discussion:
[jifty-devel] Struggling with Jifty
Sadia Lone
2010-05-15 04:08:49 UTC
Permalink
An absolute newbie here:

I have been struggling with Jifty for the last couple of weeks. My progress:

1. I was able to successfuly install jifty after many tries.
2. I have been able to create and run the tutorial blog application at
http://search.cpan.org/~alexmv/Jifty-0.91117/lib/Jifty/Manual/Tutorial.pod

I would really appreciate if someone can:

1. Point out how can I write few more lines to edit/delete the post.
To explain a bit, the following snippet is for adding a new post:

package MyWeblog::View;
use strict;
use warnings;
use Jifty::View::Declare -base;

template post => page { title => 'Post Entry' } content {
my $action = new_action(class => 'CreatePost');

form {
render_action $action;
form_submit(label => 'Post');
}
};

1;

I want a similar code snippet for editing and deleting an existing post.

I know I can do these operations from admin interface but I want to do
these from my own code so that I can add/remove columns as required
and add validation etc.

Since I am a newbie to programming as well I could not make sense out
of all docs I read (Jifty::Action etc.)

2. A ready made jifty application somewhere on Internet with just
basic operations for a database table (CREATE, INSERT, UPDATE, DELETE)

All documentation at jifty.org or cpan looks like has been written for
experts. If there is anything I missed, point me to that.

Thx

S.Lone
Jes
2010-05-15 08:07:27 UTC
Permalink
El Sat, 15 May 2010 05:08:49 +0100
Post by Sadia Lone
1. I was able to successfuly install jifty after many tries.
2. I have been able to create and run the tutorial blog application at
http://search.cpan.org/~alexmv/Jifty-0.91117/lib/Jifty/Manual/Tutorial.pod
1. Point out how can I write few more lines to edit/delete the post.
package MyWeblog::View;
use strict;
use warnings;
use Jifty::View::Declare -base;
template post => page { title => 'Post Entry' } content {
my $action = new_action(class => 'CreatePost');
form {
render_action $action;
form_submit(label => 'Post');
}
};
1;
I want a similar code snippet for editing and deleting an existing post.
I know I can do these operations from admin interface but I want to do
these from my own code so that I can add/remove columns as required
and add validation etc.
Since I am a newbie to programming as well I could not make sense out
of all docs I read (Jifty::Action etc.)
2. A ready made jifty application somewhere on Internet with just
basic operations for a database table (CREATE, INSERT, UPDATE, DELETE)
All documentation at jifty.org or cpan looks like has been written for
experts. If there is anything I missed, point me to that.
Thx
S.Lone
_______________________________________________
jifty-devel mailing list
http://lists.jifty.org/cgi-bin/mailman/listinfo/jifty-devel
Hi Sadia:

Well, I'm not an expert in Jifty, but from my experience, you cand edit
or delete in a similar way you create the post, using autoclasses (is it
the right name?). For example, to edit a record (to modify it):

template '/edit_post' => page { title => 'Edit Post Entry' } content
{
my $postid = get 'postid';

my $post = MyWeblog::Model::Post->new;
$post->load($postid);

my $action = new_action(class => 'UpdatePost', record=>$post);
#^^^^^^Here is the magic
form {
render_param( $action , 'param') ; #where param is
#the column in your
#model you want to
#modify
#Probably you need more
#fields to update a
#record
form_submit(label =>
'Post');
}
};

This page would be called as 'http://xxxxx/edit_post/1' for example, to
edit postid=1. So in the dispatcher you need something like:

before qr'/edit_post/(\d+)' => run {
set postid => $1 ; #this is to send the postid to the view
};

on '/edit_post/*' => run {
show '/edit_post' ;
};

Well, take care with this code because it was typed on fly so probably
is buggy. You can do more or less the same to delete records. In this
case, probably you want to add some code for show a confirmation box
before delete the record. For example:


hyperlink(
label => 'Delete',
onclick => {
confirm => 'Are you sure? ',
url => '/delete_post/' . $post->id ,
}
);

Or something similar. And the autoclass in this case (when you define
the action) would be "DeletePost". Or you can create a specific
deletion action from command line, and in the file generated (for
example DelPost.pm) delete the record with something like:

my $post = MyWeblog::Model::Post->new;
$post->load( $postid);

$post->delete;

Obiously you need to call the action from the dispatcher, send it the
post_id, and so... More info in the docs and google :).

Probably other people in this list help you with more precise examples.

Best regards,

Jes
Ruslan Zakirov
2010-05-15 16:36:27 UTC
Permalink
Post by Jes
El Sat, 15 May 2010 05:08:49 +0100
[snip]
Post by Jes
Well, I'm not an expert in Jifty, but from my experience, you cand edit
or delete in a similar way you create the post, using autoclasses (is it
  template '/edit_post' => page { title => 'Edit Post Entry' } content
  {
       my $postid = get 'postid';
       my $post = MyWeblog::Model::Post->new;
       $post->load($postid);
[snip]
Post by Jes
       };
This page would be called as 'http://xxxxx/edit_post/1' for example, to
before qr'/edit_post/(\d+)' => run {
       set postid => $1 ; #this is to send the postid to the view
};
on '/edit_post/*' => run {
   show '/edit_post' ;
};
In my experience it's better to move slightly more things into dispatcher:

on '/edit_post/*' => run {
# * - also captures so you can do the following and don't need before rule:
my $id = $1;

# load right in dispatcher
my $post = Jifty->app_class('Model::Post');
$post->load( $id );

# "404: not found" if not found
abort(404) unless $post->id;

# store object instead of id
set post => $post;

   show '/edit_post' ;
};

[snip]

I hope once you implement this and learn new tricks, you send us
patches for documentation to make things clearer for other newbies :)
--
Best regards, Ruslan.
Sadia Lone
2010-05-16 05:36:54 UTC
Permalink
Post by Jes
Post by Jes
Well, I'm not an expert in Jifty, but from my experience, you cand edit
or delete in a similar way you create the post, using autoclasses (is it
  template '/edit_post' => page { title => 'Edit Post Entry' } content
  {
       my $postid = get 'postid';
       my $post = MyWeblog::Model::Post->new;
       $post->load($postid);
Hi Jes, thanks a lot for your kind help. With your help I am starting
to make sense of number of difficult things.

Yes, I was missing this code needed to get parameters from URL,
loading existing record and then updating it. I am still struggling
with delete page but hopefully get it done. If not will ask you again.
Post by Jes
on '/edit_post/*' => run {
   my $id = $1;
   # load right in dispatcher
   my $post = Jifty->app_class('Model::Post');
   $post->load( $id );
   # "404: not found" if not found
   abort(404) unless $post->id;
   # store object instead of id
   set post => $post;
    show '/edit_post' ;
};
Hi Ruslan, I tried to use the $post object in my lib/MyWeblog/View.pm
as follows:

template editpost => page { title => 'Edit Entry' } content {

my $post = get 'post';
my $action = new_action(class => 'UpdatePost', record => $post);

form {
render_action $action;
form_submit(label => 'Update');
}
};

But I think I am doing something wrong because of the following error.
(JiftyDemo is my app name)

INFO - GET request for /errors/500
FATAL - View error: Attempt to reload JiftyDemo/View.pm aborted.
Compilation failed in require at
/usr/local/share/perl/5.10.1/Jifty/Util.pm line 277.
Post by Jes
I hope once you implement this and learn new tricks, you send us
patches for documentation to make things clearer for other newbies :)
Yes sure Ruslan, I badly needed such a document and I think a lot of
beginners too. I am going to do a write up as soon as I am able to do
following things:

1. Adding, editing, deleting rows in a model. (Almost done).
2. Using templates to selectively display table columns.
3. User registration, management with the plug-in.
4. Menu bar mgmt.
5. Basic validation using models.

Most applications at my work place don't need functionality more than
this. I am surprised (and happy) to learn that I need to write very
few lines of code to do these things using Jifty. This is indeed going
to increase my productivity many fold.

So I think Jifty is not only for the developers of advanced applications.

Thx for all the help.

Regards
--S
Ruslan Zakirov
2010-05-16 12:18:48 UTC
Permalink
On Sun, May 16, 2010 at 9:36 AM, Sadia Lone <***@mavsol.com> wrote:

[snip]
Post by Sadia Lone
Hi Ruslan, I tried to use the $post object in my lib/MyWeblog/View.pm
template editpost => page { title => 'Edit Entry' } content {
     my $post = get 'post';
     my $action = new_action(class => 'UpdatePost', record => $post);
     form {
         render_action $action;
         form_submit(label => 'Update');
     }
};
But I think I am doing something wrong because of the following error.
(JiftyDemo is my app name)
INFO - GET request for /errors/500
FATAL - View error: Attempt to reload JiftyDemo/View.pm aborted.
Compilation failed in require at
/usr/local/share/perl/5.10.1/Jifty/Util.pm line 277.
You're in devel mode I believe and don't restart server on code
changes. If new code has syntax errors then auto reload can fail in
such way. Try to stop and start server, I hope it will show you more
detailed error messages.
--
Best regards, Ruslan.
Sadia Lone
2010-05-16 14:09:25 UTC
Permalink
Post by Ruslan Zakirov
You're in devel mode I believe and don't restart server on code
changes. If new code has syntax errors then auto reload can fail in
such way. Try to stop and start server, I hope it will show you more
detailed error messages.
Hi Ruslan, thanks for your reply (on Sunday :-) )

I tried by restarting the server and the error was something about
"Jifty->app_class('Model::Post');" with strict subs.

I changed "new_action(class => 'UpdatePost', record => $post);" to
"new JiftyDemo::Model::Post;" and it works perfectly.

Thx a lot.

I shall come back with few more questions once I have done my home
work on other issues.

Regards
--S
Sadia Lone
2010-05-16 14:13:09 UTC
Permalink
Post by Sadia Lone
Post by Ruslan Zakirov
You're in devel mode I believe and don't restart server on code
changes. If new code has syntax errors then auto reload can fail in
such way. Try to stop and start server, I hope it will show you more
detailed error messages.
Hi Ruslan, thanks for your reply (on Sunday :-) )
I tried by restarting the server and the error was something about
"Jifty->app_class('Model::Post');" with strict subs.
I changed "new_action(class => 'UpdatePost', record => $post);" to
"new JiftyDemo::Model::Post;"  and it works perfectly.
"Jifty->app_class('Model::Post');" with "new JiftyDemo::Model::Post;"
to be exact.

Probably the "there is more than one way to ..." thing.

Rgds
--S
Ruslan Zakirov
2010-05-16 18:30:01 UTC
Permalink
[snip]
Post by Sadia Lone
Post by Sadia Lone
I changed "new_action(class => 'UpdatePost', record => $post);" to
"new JiftyDemo::Model::Post;"  and it works perfectly.
"Jifty->app_class('Model::Post');" with "new JiftyDemo::Model::Post;"
to be exact.
Sorry, it was my mistake. I didn't test code. The following code
returns class name and makes sure it's loaded. ->new is missing part
as you need object instead of class.

Jifty->app_class('Model::Post');

Instead the folling code can be used.

Jifty->app_class('Model::Post')->new;

Sure, your current code that works for you is correct and equal to
above line. I just find app_class method better long term, for example
if you will want rename application or re-use code in another
application.

So at the end the following lines are very similar:

my $obj = App::Class::Model::X->new;
my $obj = new App::Class::Model::X;
my $obj = Jifty->app_class('Model::X')->new;
Post by Sadia Lone
Probably the "there is more than one way to ..." thing.
Rgds
--S
_______________________________________________
jifty-devel mailing list
http://lists.jifty.org/cgi-bin/mailman/listinfo/jifty-devel
--
Best regards, Ruslan.
Loading...