Add a method for approving/disapproving content

Hey everyone,

This is my first post to the forums. I'm trying to add an 'approve' and
'disapprove' link for new content to my website. I thought I should
just add two links to the "show" view to trigger the "approve" and
"disapprove" method. Clicking these links doesn't have any effect in
the DB. Is this the totally wrong approach or is my syntax just bad?
I've included the code below.

View:

...
<p class="right">
<%= link_to 'Approve', post, :action => 'approve', :id => post %> |
<%= link_to 'Disapprove', post, :action => 'disapprove', :id => post %>

<%= link_to 'Edit', edit_post_path(post) %> |
<%= link_to 'Delete', post, :confirm => 'Are you sure?', :method =>
:delete %>
</p>
...

Controller:

...
  def approve
    @post = Post.find(params[:id])
    @post.approve!

    flash[:notice] = "This post was approved."

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml { head :ok }
    end
  end

  def disapprove
    @post = Post.find(params[:id])
    @post.disapprove!

    flash[:notice] = "This post was disapproved."

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml { head :ok }
    end
  end
...

And in my routes.rb:

...
  map.resource :posts, :member => {
    :approve => :put,
    :disapprove => :put
  }
...

Hey everyone,

Hey David,

<p class="right">
<%= link_to 'Approve', post, :action => 'approve', :id => post %> |
<%= link_to 'Disapprove', post, :action => 'disapprove', :id => post %>
>
<%= link_to 'Edit', edit_post_path(post) %> |
<%= link_to 'Delete', post, :confirm => 'Are you sure?', :method =>
:delete %>
</p>

A couple of things:

1) Your resource is configured to only accept put method type and your
links appear to be get.
2) In the link_to its self I would use:
<%= link_to 'Disapprove', disapprove_post_path(post), :method => 'put'
%>

Notice that I added the method and am now using the route helpers?

There is more to talk about the approach, but you'll get it working with
those two changes.

Paul

Hey everyone,

This is my first post to the forums. I'm trying to add an 'approve' and
'disapprove' link for new content to my website. I thought I should
just add two links to the "show" view to trigger the "approve" and
"disapprove" method. Clicking these links doesn't have any effect in
the DB. Is this the totally wrong approach or is my syntax just bad?
I've included the code below.

View:

...
<p class="right">
<%= link_to 'Approve', post, :action => 'approve', :id => post %> |
<%= link_to 'Disapprove', post, :action => 'disapprove', :id => post %>
>
<%= link_to 'Edit', edit_post_path(post) %> |
<%= link_to 'Delete', post, :confirm => 'Are you sure?', :method =>
:delete %>
</p>
...

Controller:

...
def approve
   @post = Post.find(params[:id])
   @post.approve!

   flash[:notice] = "This post was approved."

   respond_to do |format|
     format.html { redirect_to(posts_url) }
     format.xml { head :ok }
   end
end

def disapprove
   @post = Post.find(params[:id])
   @post.disapprove!

   flash[:notice] = "This post was disapproved."

   respond_to do |format|
     format.html { redirect_to(posts_url) }
     format.xml { head :ok }
   end
end
...

And in my routes.rb:

...
map.resource :posts, :member => {
   :approve => :put,
   :disapprove => :put
}

Dave, can you post your model file?

Thanks,

-Conrad

Dave, can you post your model file?

Thanks Conrad. I found a workaround and got some pointers on how to
build the links. I'll report back on the link approach tomorrow.