Hey all, I have a table helper. Basically I want only certain tables to have a view link for each record. Im not sure the best way to achieve this but the way I can up with is to check if the model contains a certain method such as viewable and if it does, then you create the link in a table cell: def table(collection, header_names, fields, class_name) return false unless collection.any? table_str = "" table_str += "<table id=\"" + class_name + "\" class=\"" + class_name + "\">\n" table_str += "\t<thead>\n" table_str += "\t\t<tr>\n" header_names.each do |name| table_str += "\t\t\t<th>" table_str += name table_str += "</th>\n" end table_str += "\t\t</tr>\n" table_str += "\t</thead>\n" table_str += "\t<tbody>\n" collection.each do |col| table_str += "\t\t<tr>\n" fields.each do |name| table_str += "\t\t\t<td>\n" table_str += col[name].to_s table_str += "\t\t\t</td>\n" if params[:controller].singularize.constantize.responds_to(:viewable) table_str += "\t\t\t\t<td>\n" table_str += link_to "Edit", edit_ + params[:action].singularize + _path table_str += "\t\t\t\t</td>\n" end end table_str += "\t\t</tr>\n" end table_str += "\t</tbody>\n" table_str += "</table>\n" table_str.html_safe end This file is located in helpers/layout_helper Right now I get error: "wrong constant name user" when trying to display the users page. Or perhaps there''s a better way to determine whether you want to have a link available for certain views (note this is not role-based user authorization but rather some views will be editable and others not period regardless of user)? Thanks for response. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 23, 8:52 pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > params[:controller].singularize.constantize.responds_to(:viewable)Presumably this is the line throwing the error. If params[:controller] is users then you''ll be calling constantize on the string user. Constants have to start with a capital letter, so constantize throws an error. As an aside, seing that much markup in a helper is off-putting(in the same way that too much logic in a template is awkward), if I were you i''d shift some of that into a partial Fred> table_str += "\t\t\t\t<td>\n" > table_str += link_to "Edit", edit_ + > params[:action].singularize + _path > table_str += "\t\t\t\t</td>\n" > end > end > table_str += "\t\t</tr>\n" > end > table_str += "\t</tbody>\n" > table_str += "</table>\n" > table_str.html_safe > end > > This file is located in helpers/layout_helper > > Right now I get error: "wrong constant name user" when trying to display > the users page. > > Or perhaps there''s a better way to determine whether you want to have a > link available for certain views (note this is not role-based user > authorization but rather some views will be editable and others not > period regardless of user)? > > Thanks for response. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #988949:> On Mar 23, 8:52pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> params[:controller].singularize.constantize.responds_to(:viewable) > > Presumably this is the line throwing the error. If params[:controller] > is users then you''ll be calling > constantize on the string user. Constants have to start with a capital > letter, so constantize throws an error. > As an aside, seing that much markup in a helper is off-putting(in the > same way that too much logic in a template is awkward), if I were you > i''d shift some of that into a partial > > FredHey thanks you were right. Now it looks even more off putting: def table(collection, header_names, fields, class_name) return false unless collection.any? table_str = "" table_str += "<table id=\"" + class_name + "\" class=\"" + class_name + "\">\n" table_str += "\t<thead>\n" table_str += "\t\t<tr>\n" header_names.each do |name| table_str += "\t\t\t<th>" table_str += name table_str += "</th>\n" end if params[:controller].singularize.capitalize.constantize.method_defined? :viewable table_str += "\t\t\t<th>" table_str += "View" table_str += "</th>\n" end table_str += "\t\t</tr>\n" table_str += "\t</thead>\n" table_str += "\t<tbody>\n" collection.each do |col| table_str += "\t\t<tr>\n" fields.each do |name| table_str += "\t\t\t<td>\n" table_str += col[name].to_s table_str += "\t\t\t</td>\n" end if params[:controller].singularize.capitalize.constantize.method_defined? :viewable table_str += "\t\t\t<td>\n" table_str += link_to ''View'', :action => ''show'', :id => col.id table_str += "\t\t\t</td>\n" end table_str += "\t\t</tr>\n" end table_str += "\t</tbody>\n" table_str += "</table>\n" table_str.html_safe end It works but I dont like that I am checking whether a model has the method "viewable" just to display a link to a detail view in the table. Is there a better way to determine if the current controller''s table should contain a view or not? Thanks for response. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 March 2011 16:47, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> ... > It works but I dont like that I am checking whether a model has the > method "viewable" just to display a link to a detail view in the table. > Is there a better way to determine if the current controller''s table > should contain a view or not?Can''t you set an @variable in the controller and test it in the view to determine whether to show the link? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 Mar 2011, at 16:47, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #988949: >> On Mar 23, 8:52pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> >>> > if > params[:controller].singularize.capitalize.constantize.method_defined? > :viewableWhy not just controller.respond_to? :viewable ? Fred> table_str += "\t\t\t<th>" > table_str += "View" > table_str += "</th>\n" > end > > table_str += "\t\t</tr>\n" > table_str += "\t</thead>\n" > table_str += "\t<tbody>\n" > collection.each do |col| > table_str += "\t\t<tr>\n" > fields.each do |name| > table_str += "\t\t\t<td>\n" > table_str += col[name].to_s > table_str += "\t\t\t</td>\n" > end > if > params[:controller].singularize.capitalize.constantize.method_defined? > :viewable > table_str += "\t\t\t<td>\n" > table_str += link_to ''View'', :action => ''show'', :id => > col.id > table_str += "\t\t\t</td>\n" > end > > table_str += "\t\t</tr>\n" > end > table_str += "\t</tbody>\n" > table_str += "</table>\n" > table_str.html_safe > end > > It works but I dont like that I am checking whether a model has the > method "viewable" just to display a link to a detail view in the table. > Is there a better way to determine if the current controller''s table > should contain a view or not? > > Thanks for response. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #989266:> On 25 Mar 2011, at 16:47, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> Frederick Cheung wrote in post #988949: >>> On Mar 23, 8:52pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>> >>>> >> if >> params[:controller].singularize.capitalize.constantize.method_defined? >> :viewable > > Why not just controller.respond_to? :viewable ? > > FredThanks for the idea. That should work. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.