0

How does one setup a condition if 'post_controller/show' is viewed.

I would like an 'Edit' button to display on the header navigation if the user is viewing the post, without setting up a different layout and have it just rely on the existing _header.html.erb for navigation.

# Header navigation partial _header.html.erb
<%= link_to 'Edit post', edit_post_path(@post) %>

I know I can take a param and set clause something like:

<% if params[:id] %>
  <%= link_to 'Edit post', edit_post_path(@post) %>
<% end %>

The problem with the above is it will cause an exception undefined method 'edit_post' if you visit a different route that doesn't relate to the Post model.

If I can get away from many layouts and partials that would great but if the suggestion is that I should be using layouts and partials in this scenario then I will.

Many thanks all.

4

1 に答える 1

2
# Header navigation partial _header.html.erb
<%= yield(:edit_link) %>

#In your post_controller/show view :
<% content_for :edit_link, link_to('Edit post', edit_post_path(@post)) %>

そしてそれはそれについてです。モデルごとに、好きな場所でコンテンツを変更:edit_linkできます...

于 2012-11-02T13:56:46.917 に答える