lgao
April 29, 2006, 8:41am
1
obviously, follow code is not working
<% if [email protected] (session[:user].id) %>
<%= link_to "Join This Group", :action => "join", :id => @group %>
<% end %>
“find” always throw an exception. I want to display the link of “find”
fails, what is the right way to do this?
Thanks.
lgao
April 30, 2006, 6:19pm
3
liang gao wrote:
obviously, follow code is not working
<% if [email protected] (session[:user].id) %>
<%= link_to "Join This Group", :action => "join", :id => @group %>
<% end %>
“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) %>
lgao
May 3, 2006, 3:05am
4
Thanks, Danny.
That solved my problem greatly.
Liang
Daniel B. wrote:
liang gao wrote:
obviously, follow code is not working
<% if [email protected] (session[:user].id) %>
<%= link_to "Join This Group", :action => "join", :id => @group %>
<% end %>
“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) %>
lgao
May 3, 2006, 7:08pm
5
liang gao wrote:
obviously, follow code is not working
<% if [email protected] (session[:user].id) %>
<%= link_to "Join This Group", :action => "join", :id => @group %>
<% end %>
“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.
lgao
May 3, 2006, 7:10pm
6
what’s wrong with Mark J.’ 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
lgao
May 19, 2006, 4:52pm
7
On 4/30/06, Mike G. [email protected] wrote:
what’s wrong with Mark J.’ 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
lgao
April 30, 2006, 8:22pm
8
liang gao wrote:
obviously, follow code is not working
<% if [email protected] (session[:user].id) %>
<%= link_to "Join This Group", :action => "join", :id => @group %>
<% end %>
“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