Hi,
I want to list users(modal) in sinatra, with links to other pages for each user
{{#users}}
{{name}}
<a href="show/{{id}}">show detail</a>
{{/users}}
however, I'd like to use the link_to function instead of the raw <a>
tag in view code, say
class Main
module Views
class UsersIndex < Mustache
def users
@users.each do |u|
# this doesn't work since u is a class(model)
u['show_link'] = link "Edit", "/users/#{id}"
# this doesn't work either
u.edit_link = link "Edit", "/users/"
end
end
end
end
end
I know it should work if I build a new hash against the @users, but,
is there a easy way?
Thanks.
ERB helpers that generate HTML are just that: ERB helpers.
Mustache loves HTML.
Here's how I'd do it:
{{#users}}
{{name}}
<a href="{{url}}">show detail</a>
{{/users}}
On Oct 3, 2010, at 7:48 AM, Seven Du <dujinfang@gmail.com> wrote:
>
> {{#users}}
> {{name}}
> <a href="show/{{id}}">show detail</a>
> {{/users}}
Yes, I'm curious how do you generate the url variable in your view. Since users is a list or user which is a model User, my User model doesn't has an attribute called url. So anytime assign user.url = make_a_url_helper(blah) fails. Perhaps the easiest solution would be add a attr_accessor :url to the User model ... Thank you. On Tue, Oct 5, 2010 at 4:32 AM, Chris Wanstrath <chris@ozmm.org> wrote: > ERB helpers that generate HTML are just that: ERB helpers. > Mustache loves HTML. > Here's how I'd do it: > {{#users}} > {{name}} > <a href="{{url}}">show detail</a> > {{/users}} > > On Oct 3, 2010, at 7:48 AM, Seven Du <dujinfang@gmail.com> wrote: > > > {{#users}} > {{name}} > <a href="show/{{id}}">show detail</a> > {{/users}} -- Blog: http://www.dujinfang.com Proj: http://www.freeswitch.org.cn
Or, in your view code: def url build_your_url end On Oct 4, 2010, at 10:10 PM, Seven Du <dujinfang@gmail.com> wrote: > Yes, I'm curious how do you generate the url variable in your view. > > Since users is a list or user which is a model User, my User model > doesn't has an attribute called url. So anytime assign user.url = > make_a_url_helper(blah) fails. Perhaps the easiest solution would be > add a attr_accessor :url to the User model ... > > Thank you. > > > On Tue, Oct 5, 2010 at 4:32 AM, Chris Wanstrath <chris@ozmm.org> wrote: >> ERB helpers that generate HTML are just that: ERB helpers. >> Mustache loves HTML. >> Here's how I'd do it: >> {{#users}} >> {{name}} >> <a href="{{url}}">show detail</a> >> {{/users}} >> >> On Oct 3, 2010, at 7:48 AM, Seven Du <dujinfang@gmail.com> wrote: >> >> >> {{#users}} >> {{name}} >> <a href="show/{{id}}">show detail</a> >> {{/users}} > > > > -- > Blog: http://www.dujinfang.com > Proj: http://www.freeswitch.org.cn
You must misunderstood me, because users is a list of users(array), so, the url function in view code must return different url for each loop. On Wed, Oct 6, 2010 at 3:47 AM, Chris Wanstrath <chris@ozmm.org> wrote: > Or, in your view code: > > def url > build_your_url > end > > On Oct 4, 2010, at 10:10 PM, Seven Du <dujinfang@gmail.com> wrote: > >> Yes, I'm curious how do you generate the url variable in your view. >> >> Since users is a list or user which is a model User, my User model >> doesn't has an attribute called url. So anytime assign user.url = >> make_a_url_helper(blah) fails. Perhaps the easiest solution would be >> add a attr_accessor :url to the User model ... >> >> Thank you. >> >> >> On Tue, Oct 5, 2010 at 4:32 AM, Chris Wanstrath <chris@ozmm.org> wrote: >>> ERB helpers that generate HTML are just that: ERB helpers. >>> Mustache loves HTML. >>> Here's how I'd do it: >>> {{#users}} >>> {{name}} >>> <a href="{{url}}">show detail</a> >>> {{/users}} >>> >>> On Oct 3, 2010, at 7:48 AM, Seven Du <dujinfang@gmail.com> wrote: >>> >>> >>> {{#users}} >>> {{name}} >>> <a href="show/{{id}}">show detail</a> >>> {{/users}} >> >> >> >> -- >> Blog: http://www.dujinfang.com >> Proj: http://www.freeswitch.org.cn > -- Blog: http://www.dujinfang.com Proj: http://www.freeswitch.org.cn