Topic: Do something based on URL parameter

Hi all I am new to ruby on rails. However I am slowly learning. I am currently learning how to build a ror web app following the Creating a weblog in 15 minutes screencast. I have the app up and running and its working fine.

Now I would like to do a few simple changes to the way it works, for example For the list and show pages I have a template called _post.rhtml which gets interpreted by the render :partials thingy in list.rhtml and show.rhtml.

The _post.rhtml template holds this code:

<div id="blog-post">
 
  <div id="blog-post-header">
    <!-- This displays the link -->
    <h2><%= link_to post.title, :action => 'show', :id => post %></h2>
  </div>
 
  <div id="blog-post-body">
    <%= RedCloth.new(post.body).to_html %>
  </div>
 
  <div id="blog-post-footer">
    <%= post.created_at.to_s(:long) %> (<%= link_to 'Edit', :action => 'edit', :id => post %> |
<span id="del"><%= link_to 'Delete', { :action => 'destroy', :id => post }, :confirm => 'Are you sure?', :post => true %></span>)
  </div>
 
</div>

Now What I'd like to do is display a link if you are on the list page, which it does. But when you goto view the blog post in the show page the blog title is still a link. I want it to be just normal text rather than a link when viewing the blog post. So I though I'd use a if/else statement to check whether the show URL parameter exists, if it doesnt produce a link, else dont produce a link
<% if !params[:show]
    headerTitle = "<h2>" + link_to post.title, :action => 'show', :id => post + "</h2>";
else
    headerTitle = "<h2>" + post.title + "</h2>";
%>

And used this to display the blog title:
<%= headerTitle %>

When I run this code I get a comile error:

compile error
./script/../config/../app/views/blog/_post.rhtml:2: syntax error
    headerTitle = "<h2>" + link_to post.title, :action => 'show', :id => post + "</h2>";
                                       ^
./script/../config/../app/views/blog/_post.rhtml:2: syntax error
    headerTitle = "<h2>" + link_to post.title, :action => 'show', :id => post + "</h2>";
                                                         ^
./script/../config/../app/views/blog/_post.rhtml:2: syntax error
    headerTitle = "<h2>" + link_to post.title, :action => 'show', :id => post + "</h2>";
                                                                 ^
./script/../config/../app/views/blog/_post.rhtml:21: syntax error

So can anybody help me. Also is params[:show] the correct code to get the send URL parameter or not? Am I doing this all wrong?

Any help would be grateful.

Last edited by wildteen88 (2006-08-31 08:34:23)

Re: Do something based on URL parameter

It's one or both of two things. First you need an 'end' at the end of the if statement.  If that doesn't fix it try adding parantheses to the link_to method.  Once you fix that, you're going to have a problem with the logic.  Try params[:action] != 'show'.

<% 
if params[:action] != 'show'
  headerTitle = "<h2>" + link_to(post.title, :action => 'show', :id => post) +    "</h2>";
else
   headerTitle = "<h2>" + post.title + "</h2>";
end
%>

If you're interested, you could streamline this quite a bit with link_to_unless.

<h2><%= link_to_unless params[:action] == 'show', post.title, :action => 'show', :id => post %></h2>

Re: Do something based on URL parameter

Thank you! It now works the way I wanted it to. I did look at the link_to_unless method over at the ruby documentation but I was little confused with the syntax. Both methods work! Thanx again.