Dok Nabaku
Dok Nabaku

Reputation: 83

How can I redirect to the object in an array

I have action index:

def index
  if params['type'] == 'random'
    @objects = Object.order("RANDOM()").limit(1)
  else
    @objects = Object.all.limit(1)
  end
end

and create action:

def create
  object = Object.find(params[:object_id])
  comment = object.comments.create(params[:comment].permit(:body))

  respond_to do |format|
    format.html 
    format.js #ajax
  end

  if comment.save
    redirect_to root_path(params[:object_id]) #doesn't work
  else
    flash[:error] = comment.errors.full_messages[0]
    redirect_to root_path(params[:object_id]) #doesn't work
  end
end

I can comment an object in my index page. When I put a comment, I want to redirect to the object that was commented.

With my code, the page is reloaded, but the next object is displayed, and I cannot see the comment. How can I redirect to the same object?

My root_path

<span class="random-icon"><%= link_to icon('random'), "http://localhost:3000/?type=random" %></span>

<div class="inner-container">
    <% @objects.each do |object| %>
      <h1 class="title"><%= object.title %></h1>
      <p class="obj"><%= object.body %></p>
      <h3 class="comments-title">Comments:</h3>
      <div id="comments">  
        <% object.comments.each do |comment| %>
          <div class="comments"> <%= comment.body %> 
            <span class="time-to-now"><%= distance_of_time_in_words_to_now(comment.created_at) %> ago</span>
          </div>
        <% end %>
      </div>

        <div id="error"><%= flash[:error] %></div>
        <%= form_for([object, object.comments.build], remote: true) do |f| %>
          <%= f.text_area :body, class: "text-area" %> 
          <p class="char-limit">255 characters limit</p>
          <%= f.submit "Comment", class: 'button' %>
        <% end %>
    <% end %>
</div>

Upvotes: 2

Views: 465

Answers (2)

ydaniju
ydaniju

Reputation: 400

If params['type'] is true, Object.order("RANDOM()").limit(1) will always br reevaluated and usually return a new object. To ensure you return to the same object, you might want to store it in session and then check first in your index if there is a liked comment in your sessions, if so, @objects = Object.find(session[:comment_object_id])

def index
  if session[:comment_object_id]
    @objects = Object.find(session[:comment_object_id])
    session.delete(:comment_object_id) # delete the session after use
  elsif params['type'] == 'random'
    @objects = Object.order("RANDOM()").limit(1)
  else
    @objects = Object.all.limit(1)
  end
end


def create
   object = Object.find(params[:id])
   comment = object.comments.create(params[:comment].permit(:body))

  respond_to do |format|
    format.html 
    format.js #ajax
  end

  if comment.save
    session[:comment_object_id] = :object_id # set the session here
    redirect_to root_path # should work now
  else
    flash[:error] = comment.errors.full_messages[0]
    redirect_to root_path #should work now
  end
end

Upvotes: 1

Dogweather
Dogweather

Reputation: 16779

This is pretty easy, and you're very close.

Inside your create method, you've got the object you want to redirect to. So just use it directly inside the if comment.save like:

redirect_to object_path(object)

You can get a list of all these path "helpers" via the command:

rake routes

And in that listing, you should see, by the way, that root_path does not accept any arguments ... for future reference.

Upvotes: 0

Related Questions