obviously, follow code is not working <p> <% if !@group.users.find(session[:user].id) %> <%= link_to "Join This Group", :action => "join", :id => @group %> <% end %> </p> "find" always throw an exception. I want to display the link of "find" fails, what is the right way to do this? Thanks. -- Posted via http://www.ruby-forum.com/.
liang gao wrote:> obviously, follow code is not working > > <p> > <% if !@group.users.find(session[:user].id) %> > <%= link_to "Join This Group", :action => "join", :id => @group %> > <% end %> > </p> > > "find" always throw an exception. I want to display the link of "find" > fails, what is the right way to do this?<% unless @group.users.find_by_id(session[:user].id) %> -- We develop, watch us RoR, in numbers too big to ignore.
Any takers? liang gao wrote:> obviously, follow code is not working > > <p> > <% if !@group.users.find(session[:user].id) %> > <%= link_to "Join This Group", :action => "join", :id => @group %> > <% end %> > </p> > > "find" always throw an exception. I want to display the link of "find" > fails, what is the right way to do this? > > Thanks.-- Posted via http://www.ruby-forum.com/.
what''s wrong with Mark James'' code? find_by... doesn''t throw an exception when it fails to find a record, it simply returns nil. That''s what you want and should be using in this case. Mike On 4/29/06, liang gao <liangg@gmail.com> wrote:> > Any takers? > > liang gao wrote: > > obviously, follow code is not working > > > > <p> > > <% if !@group.users.find(session[:user].id) %> > > <%= link_to "Join This Group", :action => "join", :id => @group %> > > <% end %> > > </p> > > > > "find" always throw an exception. I want to display the link of "find" > > fails, what is the right way to do this? > > > > Thanks. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060430/fae77335/attachment.html
liang gao wrote:> obviously, follow code is not working > > <p> > <% if !@group.users.find(session[:user].id) %> > <%= link_to "Join This Group", :action => "join", :id => @group %> > <% end %> > </p> > > "find" always throw an exception. I want to display the link of "find" > fails, what is the right way to do this? > > Thanks.I prefer to use the dynamic finders, because they return nil on failure, rather than throw an exception. Vis- <% if !@group.users.find_by_id(session[:user].id) %> - Danny -- Posted via http://www.ruby-forum.com/.
liang gao wrote:> obviously, follow code is not working > > <p> > <% if !@group.users.find(session[:user].id) %> > <%= link_to "Join This Group", :action => "join", :id => @group %> > <% end %> > </p> > > "find" always throw an exception. I want to display the link of "find" > fails, what is the right way to do this? > > Thanks.I personally try to never put a find in a view. It really belongs in the controller. There I would have something like this. ## method to view a particular post in full def view @post = Post.find(params[:id]) rescue logger.error("Attempt to access invalid post #{params[:id]}") end -- Posted via http://www.ruby-forum.com/.
Thanks, Danny. That solved my problem greatly. Liang Daniel Burkes wrote:> liang gao wrote: >> obviously, follow code is not working >> >> <p> >> <% if !@group.users.find(session[:user].id) %> >> <%= link_to "Join This Group", :action => "join", :id => @group %> >> <% end %> >> </p> >> >> "find" always throw an exception. I want to display the link of "find" >> fails, what is the right way to do this? >> >> Thanks. > > I prefer to use the dynamic finders, because they return nil on failure, > rather than throw an exception. Vis- > > <% if !@group.users.find_by_id(session[:user].id) %> > > - Danny-- Posted via http://www.ruby-forum.com/.
On 4/30/06, Mike Garey <random52k@gmail.com> wrote:> what''s wrong with Mark James'' code? find_by... doesn''t throw an exception > when it fails to find a record, it simply returns nil.Too bad it doesn''t throw an exception. Then I prefere writing in my controller: unless @blog = Blog.find_by_id_and_is_archived(params[:id], 0) raise ActiveRecord::RecordNotFound, "Couldn''t find a non archived Blog with ID=#{params[:id]}" end