在自定义 location/view 中渲染来自 Rails 教程的 'micropost feed'?
Render a 'micropost feed' from the Rails Tutorial in a custom location/view?
我遵循了 Michael Hartl 的 'Rails Tutorial' 书,需要在自定义位置 (/members) 而不是站点的根目录中呈现 'micropost feed'。
我已经更新了 SessionsController,这样当用户登录时,他们会被重定向到新的 /members URL,但是成员视图需要两个语句来导致错误并阻止页面正在加载微博。
# app/views/members/index.html.erb
<%= render 'shared/micropost_form' %>
<%= render 'shared/feed' %>
第一个错误是:
ArgumentError in MembersController#index
First argument in form cannot contain nil or be empty
返回 raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
和第二个错误(在删除第一个渲染语句时看到)returns undefined method 'any?' for nil:NilClass
部分原因是因为要在 /members
处呈现 'micropost feed',app/views/members/index.html.erb
似乎需要访问 app/controllers/microposts_controller.rb
,我不确定如何处理这个问题.
更新:
micropost_controller.rb
# controllers/micropost_controller.rb
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to '/members'
else
@feed_items = []
render '/members'
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || '/members'
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to '/members' if @micropost.nil?
end
end
_micropost_form.html.erb
# shared/_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
# shared/_feed.html.erb
<% if @feed_items.any? %>
<ol class="microposts">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
MembersController(根据@Mandeep 下面的建议更新)
class MembersController < ApplicationController
def index
@micropost = current_user.microposts.build
@feed_items = current_user.microposts
end
end
您没有在偏音中传递参数。您需要执行以下操作:
<%= render 'shared/micropost_form', :locals => { :micropost => @micropost } %>
<%= render 'shared/feed', :locals => { :feed_items => @feed_items } %>
请记住,您需要在 index
方法中包含这两个变量:@micropost
和 @feed_items
。
将此代码添加到您的控制器:
class MembersController < ApplicationController
def index
@micropost = current_user.microposts.build
@feed_items = current_user.microposts
end
end
我遵循了 Michael Hartl 的 'Rails Tutorial' 书,需要在自定义位置 (/members) 而不是站点的根目录中呈现 'micropost feed'。
我已经更新了 SessionsController,这样当用户登录时,他们会被重定向到新的 /members URL,但是成员视图需要两个语句来导致错误并阻止页面正在加载微博。
# app/views/members/index.html.erb
<%= render 'shared/micropost_form' %>
<%= render 'shared/feed' %>
第一个错误是:
ArgumentError in MembersController#index
First argument in form cannot contain nil or be empty
返回 raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
和第二个错误(在删除第一个渲染语句时看到)returns undefined method 'any?' for nil:NilClass
部分原因是因为要在 /members
处呈现 'micropost feed',app/views/members/index.html.erb
似乎需要访问 app/controllers/microposts_controller.rb
,我不确定如何处理这个问题.
更新:
micropost_controller.rb
# controllers/micropost_controller.rb
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to '/members'
else
@feed_items = []
render '/members'
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || '/members'
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to '/members' if @micropost.nil?
end
end
_micropost_form.html.erb
# shared/_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
# shared/_feed.html.erb
<% if @feed_items.any? %>
<ol class="microposts">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
MembersController(根据@Mandeep 下面的建议更新)
class MembersController < ApplicationController
def index
@micropost = current_user.microposts.build
@feed_items = current_user.microposts
end
end
您没有在偏音中传递参数。您需要执行以下操作:
<%= render 'shared/micropost_form', :locals => { :micropost => @micropost } %>
<%= render 'shared/feed', :locals => { :feed_items => @feed_items } %>
请记住,您需要在 index
方法中包含这两个变量:@micropost
和 @feed_items
。
将此代码添加到您的控制器:
class MembersController < ApplicationController
def index
@micropost = current_user.microposts.build
@feed_items = current_user.microposts
end
end