简体   繁体   中英

How to get params with link_to in rails?

My users have reviews, review may be negative and positive And now i want show positive and negative reviews for other users users_controllers.rb

class UsersController < ApplicationController

    def show
      @user = User.find(params[:id])
      @reviews = Review.where(for_user_id: @user, negative: false)
      @reviews = @reviews.where(negative: params[:negative]) if params[:negative].present?
      @reviews = @reviews
    end
end

and my view views/users/show.html.erb

<%= link_to 'Negative reviews', user_path(negative: true) %>
            <%= link_to 'Positive reviews', user_path(negative: false) %>
<% @reviews.each do |review| %>
            <li>
              <div class="user_data">
              <div class="user_review_left">
                <%= link_to (avatar_for review.user, size: "50x50"), review.user %>
              </div>
              <div class="user_review_right">
                <%= link_to review.user.name, review.user %>

                <div class="user_post_name">
                  review for: <%= link_to (truncate review.post.name, length: 50), review.post %>
                  </div>
                </div>
              </div>
              <div class="post_review">
                 <%= review.body %>
              </div>
              <div class="review_date">
                <%= l review.created_at, :format => :my %>
              </div>
            </li>
            <% end %>
            </ul>
            </div>

how i can add links for this code? if user link the controller return @reviews with params Thanks

<%= link_to 'Negative reviews', user_path(@user, negative: true) %>

and

<%= link_to 'Positive reviews', user_path(@user, negative: false) %>

and change your show method:

def show
  @user = User.find(params[:id])
  @reviews = Review.where(for_user_id: @user, negative: (params[:negative] || false))
end

even if I ask myself why you can't do that:

def show
  @user = User.find(params[:id])
  @reviews = @user.reviews.where(for_user_id: @user, negative: (params[:negative] || false))
end

You could pass the user ID and the review parameter

link_to "Reviews", user_path(id: @user.id, negative: true)

As said by coorase appending a parameter to the end of the args will work.

<%= link_to 'Negative reviews', user_path(@user, negative: true) %>

will create a url like: root/users/:id?negative=true

if you debug inside of the controller action which handles this and evaluate the value of 'params' you will see something like:

{"negative"=>"true", "controller"=>"root", "action"=>"index"}

this is your params hash and it means that

params[:negative] ==> true

(I recommend getting the 'pry' gem and inserting 'binding.pry' inside the controller action if you don't have a good debugger you use)

Please be more specific in explaining why this output doesnt work for your situation and I may be able to help you formulate better parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM