current_user.id正在更改,但没有修改函数

时间:2015-11-10 00:35:25

标签: ruby-on-rails ruby ruby-on-rails-4 devise

我有一个小应用程序,其中包含一个呈现'stats'的配置文件,显示了关注者的数量以及您拥有的以下内容。您可以单击该按钮查看您的关注者/关注者列表,然后单击其中一个以查看该人员的个人资料。问题是,当我转到此人的个人资料时,current_user.id会更改为该人的个人资料。我没有直接修改current_user的代码。

我确实有两种用户控制器 - 我使用了一个以下功能,以及设计提供的控制器。我觉得问题可能来自于此,但我不确定。

控制器/ users_controller.rb:

class UsersController < ApplicationController
  def following
    @title = "Following"
    @user  = User.find(params[:id])
    @users = @user.following
    render 'profiles/show_follow'
  end

  def followers
    @title = "Followers"
    @user  = User.find(params[:id])
    @users = @user.followers
    render 'profiles/show_follow'
  end
end

视图/简档/ show.html.erb:

<h1> PROFILE  </h1>

<% if(@profile.id == current_user.id)  %>
<!-- If user logged in we can view this -->
<p id="notice"><%= notice %></p>

<li class="round-image-50"><%= image_tag(@profile.avatar.url(:thumb)) %> </li>
<div>  
<b>First name: </b><%= @profile.first_name %> <br/>
</div>

<div>
<b>Last name: </b><%= @profile.last_name %><br />
</div>

<div>
<b>About me: </b><%= @profile.about_me %><br />
</div>

</p>

<section class="stats">
  <%= render 'stats' %>
</section>

<%= link_to 'Edit Profile', edit_profile_path(current_user) %>
<%= link_to 'Home', root_path %>

<% else %>

</div>
<li class="round-image-50"><%= image_tag(@profile.avatar.url(:thumb)) %> </li>
<div>  
<b>First name: </b><%= @profile.first_name %> <br/>
</div>

<div>
<b>Last name: </b><%= @profile.last_name %><br />
</div>

<div>
<b>About me: </b><%= @profile.about_me %><br />
</div>

</p>

<section class="stats">
  <%= render 'stats', locals: {profile: @profile.id}  %>
</section>

<%= link_to 'Home', root_path %>
<% end %>

视图/简档/ _stats.html.erb:

<% @user.id = @profile.id %>
<div class="stats">
  <a href="<%= following_user_path(@user) %>"> 
    <strong id="following" class="stat">
      <%= @user.following.count %>
    </strong>
    following
  </a>
  <a href="<%= followers_user_path(@user) %>">
    <strong id="followers" class="stat">
      <%= @user.followers.count %>
    </strong>
    followers
  </a>
</div>

编辑:profiles_controller和show_follow

profiles_controller.rb:

class ProfilesController < ApplicationController

  # GET /profiles/1
  # GET /profiles/1.json
  def show
    @profile=Profile.find(params[:id])
  end

  # GET /profiles/new
  def new
    @profile = Profile.new
  end

  # GET /profiles/1/edit
  def edit
    @profile=Profile.find_by user_id: current_user.id
  end

  # POST /profiles
  # POST /profiles.json
  def create
    respond_to do |format|
      if @profile.save
        format.html { redirect_to root_path, notice: 'Profile was successfully created.' }
        format.json { render :show, status: :created, location: @profile }
      else
        format.html { render :new }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /profiles/1
  # PATCH/PUT /profiles/1.json
  def update
    @profile=Profile.find_by user_id: current_user.id
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
        format.json { render :show, status: :ok, location: @profile }
      else
        format.html { render :edit }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
  def set_profile
    @profile=Profile.find_by user_id: current_user.id
    end

  #Note: update this everytime we add a new attribute to Profile model
    def profile_params
      params.require(:profile).permit(:first_name, :last_name, :about_me, :avatar)
    end
end

视图/简档/ show_follow.html.erb

<% provide(:title, @title) %>
<h3><%= @title %></h3>
<% if @users.any? %>
  <ul class="users follow">
    <% puts @user.username %>
    <% @users.each do |person| %>
    <%= link_to person.username, profile_path(person.id) %><br>
    <% end %>
  </ul>
<% end %>

0 个答案:

没有答案