我的索引视图在rake路由

时间:2016-05-17 13:32:57

标签: html ruby-on-rails ruby controller routes

我理解为什么我的reviser索引页面没有出现,但我不知道如何去做。在我的rake路线中没有reviser #index,即使我在我的控制器上做了def index。我在has_oneuser之间存在reviser关系,因为user可以申请成为reviser。这是显示特定审核者http://localhost:3000/users/15/reviser的网址,但我也希望有一种方法可以查看您的个人资料,就像您是当前用户一样。这就是我设置索引的原因。所以我想要一个网址,根据我的索引ex,只显示我当前的用户作为修改者。 http://localhost:3000/users/reviserhttp://localhost:3000/reviser我怎样才能使我的索引页面显示为当前用户页面。总结一下:我的 ReviserController 索引不会显示在路线中。想要current_user的{​​{3}}网址。谢谢!

ReviserController:

class RevisersController < ApplicationController

before_action :set_reviser, only: [:show, :edit, :update]
before_action :authenticate_user!, except: [:show]

  def index
    @reviser = current_user.reviser
  end

  def show

  end

  def new
    @reviser = current_user.build_reviser(params[:reviser])
    @user = User.find(params[:user_id])
  end

  def create
    @reviser = current_user.reviser.build(reviser_params)

    if @reviser.save
      redirect_to @reviser,notice: "saved...."
    else 
      render :new
    end
  end

  def edit
    set_reviser
  end

  def update
    set_reviser
    if @reviser.update(reviser_params)
      redirect_to @reviser,notice: "updated.."
    else
      render :edit
    end
  end

private 
def set_reviser
  @reviser = current_user.reviser
end

  def reviser_params
    params.require(:reviser).permit(:description, :average_start, :average_end, :max_pages, :price_per, :active)
end
end

的index.html

&#13;
&#13;
<div class="row">
	<div class="col-md-3">
		<ul class="sidebar-list">
			<li class="sidebar-item"><%= link_to "Your Reviser Profile", reviser_path, class: "sidebar-list active" %></li>
			<li class="sidebar-item"><%= link_to "Your Essays", reviser_path, class: "sidebar-list active" %></li>
		</ul>
	</div>	
	<div class="col-md-9">
		<div class="panel panel-default">
			<div class="panel-heading">
				Reviser
			</div>
			<div class="panel-body">
			<% @reviser.each do |reviser| %>
				<div class="row">
					<div class="col-md-2">
						<%= link_to image_tag(current_user.avatar.url(:medium), class:'img-responsive'), user_path(user) %>
						
					</div>
				</div>
			<% end %>
			</div>
		</div>
	</div>		
</div>
&#13;
&#13;
&#13;

routes.rb中:

resources :users, only: [:index, :show] do 
    resource :reviser
 end

耙路线(审校指数不会出现!!!)

  1. user_reviser POST /users/:user_id/reviser(.:format)revisers #create
  2. new_user_reviser GET /users/:user_id/reviser/new(.:format)revisers #new
  3. edit_user_reviser GET /users/:user_id/reviser/edit(.:format)revisers#edit
  4. GET /users/:user_id/reviser(.:format)revisers#show
  5. PATCH /users/:user_id/reviser(.:format)revisers #update
  6. PUT /users/:user_id/reviser(.:format)revisers #update
  7. DELETE /users/:user_id/reviser(.:format)revisers#destroy
  8. 用户GET /users(.:format)用户#index
  9. user GET /users/:id(.:format)users#show

2 个答案:

答案 0 :(得分:2)

正如您所看到的那样in the rails route guide奇异资源就像您正在使用的资源一样,不会添加索引方法。

您应该将routes.rb文件修改为

resources :users, only: [:index, :show] do 
    resources :revisers
end

答案 1 :(得分:1)

这是因为您将其列为单一资源

来自http://guides.rubyonrails.org/routing.html#singular-resources

2.5 Singular Resources
Sometimes, you have a resource that clients always look up without referencing an ID.
For example, you would like /profile to always show the profile of the currently logged in user. 
 In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action:

它只会创建您列出的6个动作..

因为只有1个资源,没有意义显示索引。只需将您的逻辑移至show操作

即可
def show
    @reviser = current_user.reviser
end

并将index.html.erb重命名为show.html.erb