我正在关注Michael Hartl的书“Rails Tutorial”,并且需要更改登录的URL以在/members
而不是站点的根目录进行渲染。
我尝试过更改home.html.erb:
<% if logged_in? %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<section class="stats">
<%= render 'shared/stats' %>
</section>
<section class="micropost_form">
<%= render 'shared/micropost_form' %>
</section>
</aside>
<div class="col-md-8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<%= render 'covers/non_logged_in_home' %>
<% end %>
到
<% if logged_in? %>
<%= render 'members/index' %>
<% else %>
<%= render 'covers/non_logged_in_home' %>
<% end %>
并在views / members /中创建一个新的partial,其中包含从上面提取的内容,如下所示:
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<section class="stats">
<%= render 'shared/stats' %>
</section>
<section class="micropost_form">
<%= render 'shared/micropost_form' %>
</section>
</aside>
<div class="col-md-8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
执行此操作后,微博饲料仍然照常工作,但仍保留在网站的根目录中。我认为这是一个路由问题,但我不知道如何处理它作为routes.rb中微博的唯一路由信息是:
resources :microposts, only: [:create, :destroy]
更新
Rails.application.routes.draw do
get 'members/index' #just added; didn't work.
get 'covers/new_cover' => 'covers#new_cover', :as => :new_cover
get 'covers/home'
get 'covers/new'
get 'covers/create'
get 'covers/destroy'
get 'password_resets/new'
get 'password_resets/edit'
get 'account_activations/edit'
get 'sessions/new'
root 'covers#home'
get 'profile' => 'static_pages#profile'
get 'tours' => 'static_pages#tours'
get 'blog' => 'static_pages#blog'
get 'reviews' => 'static_pages#reviews'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
# resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :covers, only: [:home, :index, :new, :create, :destroy]
更新2:
更新路线时
get 'members' => 'members#index'
正如@Arslan正确建议的那样,路由成功但在其位置返回了以下错误:
ArgumentError in MembersController#index
First argument in form cannot contain nil or be empty
进一步调查显示此错误是由app/views/members/index.html.erb
中包含的以下一个或两个渲染语句引起的:
<%= render 'shared/micropost_form' %>
<%= render 'shared/feed' %>
这些文件的内容是:
# _micropost_form.html.erb
<%= form_for(@micropost, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost (420 chars max)..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<% end %>
<script type="text/javascript">
$('#micropost_picture').bind('change', function() {
size_in_megabytes = this.files[0].size/1024/1024;
if (size_in_megabytes > 5) {
alert('Maximum file size is 5MB. Please choose a smaller file.');
}
});
</script>
和
# _feed.html.erb
<% if @feed_items.any? %>
<ol class="microposts">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
答案 0 :(得分:0)
在SesssionController
课程中,您可以将redirect
方法更改为指向/members
:
class SessionsController < ApplicationController
# other code
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to '/members'
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
end
现在,只要用户成功登录您的网站,他/她就会被重定向到/members
页面。
修改强>
get 'members/index'
如果你有一个名为MembersController
的类,并且在该类中有一个名为index
的方法,这将有效。与此同时,您还应该在app/views/members/index.htmlerb
处有一个视图。
并明确表达如下:
get 'members/index' => 'members#index'
但是如果你想调用任何其他控制器的任何方法,你可以这样做:
get 'members/index' => 'any_other_controller#any_method'
# Remember controller name should be in plural: 'members', 'users'