I am writing an imageboard in Ruby on Rails.
I have a form for creating new posts. In that form, there is logic to figure out whether the post was created from board#show or topic#show and act accordingly. It is:
<% if @board != nil %>
<%= f.hidden_field :board_id, :value => @board.id %>
<% end %>
<% if @topic != nil %>
<%= f.hidden_field :topic_id, :value => @topic.id %>
<% end %>
The form is rendered on board#show and topic#show with this piece of code:
<%= render :partial => 'posts/form'%>
Should I move that logic to the controller? How would I go about doing that?
As it is, in the view, it’s probably fine. However, you could (possibly, depending on your app) refine it a little by putting the board/topic information in the URL using nested routes. Routes that would look something like this:
Then your “is it from a board or a post” logic could happen in your PostsController. Again, whether or not this is ‘better’ is dependent on your requirements, but that’s an alternative way of approaching this.
As a side note, you can slim down your conditionals a little.
if @boardis the same asif @board != nil(unless you have a special case for@boardbeingfalse.nilandfalsewill both evaluate as false).