D
D
dexdev2014-09-18 16:35:03
Ruby on Rails
dexdev, 2014-09-18 16:35:03

How to correctly write a condition using params[params]?

At a dead end!
I can’t figure out how best to implement the task.
There are posts, posts have categories, categories have subcategories, they are in the Category table, but they have a parent_id when creating a post with a certain category, it is necessary to show the user a block with popular post names
. How best to implement this?
and how to use <% if params[:category_id] %>
can somehow alter the base?
Help me how to do it better to indicate a bunch of code on the view, I
tried to do something like this:
@leftblock = Category.where(parent_id: 1)

<% if @leftblock.ids.to_s.include?(params[:category_id]) %>

but what about other categories... and it doesn't work even if true shows everything and even if false also shows everything
maybe someone came across...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Snegirev, 2014-09-19
@Rikkit

In general, this logic needs to be pushed into the model.
If you have a category tree, I can recommend the awesome_nested_set gem . He organizes the work with the tree.

F
FanKiLL, 2014-09-18
@FanKiLL

Show posts from the category in which a person creates a post?

Post.where(category_id: params[:category_id]) #или откуда вы там получаете id категории

D
dexdev, 2014-09-20
@AdilA

<% if @category.id > 13 %>
    <% Category.where(id:params[:category_id]).each do |c| %>
    <% Category.where(parent_id:c.parent_id).each do |cc| %>
    <ul>
    <li>
      <div class="pop_task_block">
      <%=link_to cc.name, new_category_post_path(cc) %>
      </div>
    </li>
    <% end %>
    <% end %>
    <% end %>
    </ul>

It turned out to be done this way, but it's all not kosher on the view, but it works, maybe someone will tell you how to push it into the controller or model)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question