0

I am writing a blog application in Rails where registered Users and allowed to create Article. These articles can be viewed by anyone (Without user account). The article/show.html.erb is fairly complex displaying additional information such as related articles etc... A simplified version of the code is below.

Models

class User < ActiveRecord::Base
  has_many :articles
end

class Article < ActiveRecord::Base
  belongs_to :user
end

Controller

class ArticlesController < ApplicationController
  def show
    @article = Articles.find(params[:id])
  end
end

Once a user is signed in, the user should be able to view the articles similar to anyone without user account. So when signed in user clicks on an article article/show.html.erb is rendered. I want to provide a separate link for the signed in user to view his article in a separate view without the additional complex details mentioned previously.

How can I do this since controller action ArticleController#show is already mapped to article/show.html.erb.

It seems that this can be achieved by using an additional controller such as UserArticleController and userarticlecontroller/show.html.erb. But I wonder whether this may not be the Rails way of doing things and there may exist a better method for handling this kind of situations.

2 Answers 2

1

Either a different action in the same controller, or a new action in a new controller, or some conditional logic in the existing action:

def show
  @article = Artile.find(params[:id])

  render 'show_for_user' if @article.user == current_user
end

Basically, if the article is owned by the current user (or whatever other kind of logic you need), it will render a specific view; otherwise flow falls off the end of the function and (if nothing else has been output via render or redirect_to) it will render the default show view.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I settled down to using a new controller since I need to provide 2 separate views of the same article according to the link the user clicks on. If the user clicks on "Read" link article/show.html.erb will be rendered. If the user clicks on "Owners view" a separate controller will be used. Still I am concerned about adding a new controller for this. Can it violate MVC concepts?
Don't worry, there's nothing in MVC preventing you from having different views and controllers for one model, it's just a matter of keeping things dry vs keeping things decoupled, both approaches have their pros and cons depending on your project, see my answer below
1

For a smaller app, you can take the answer you accepted, but for slightly more complicated projects I'd actually prefer going with separate controllers 90% of the time,

keeping guest and member code separate helps a lot down the line when requirements between the two invariably start drifting and when additional considerations such as security and ddos vulnerability start cropping up (on big sites you'll be using cacheing on public rutes A LOT more then on member routes for example),

using a separate superclasses (which are children of ApplicationController) for all guest routes and all member controllers is also a good idea, you can set security, event logging etc separately and in one place this way without kludges.

1 Comment

Agreed. This should be the way to go for complex applications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.