我正在尝试让我的表单提交并显示结果而不使用Ajax刷新页面。问题是,似乎没有返回方法,一旦提交,它们都会出现未定义。
_micropost.html.erb
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
data: { confirm: 'You sure?' },
title: micropost.content %>
<% end %>
</li>
create.js.erb
$(".microposts").append("<%= escape_javascript(render('shared/feed')) %>")
microposts_controller.rb
class MicropostsController < ApplicationController
before_filter :authenticate
before_filter :authroized_user, :only => :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save!
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
def destroy
@micropost.destroy
redirect_to root_path, :flash => { :success => "Micropost deleted!" }
end
private
def authroized_user
@micropost = Micropost.find(params[:id])
redirect_to root_path unless !current_user?(@micropost.user)
end
end
_feed.html.erb
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
_feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if !current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
data: { confirm: "You sure?" },
title: feed_item.content %>
<% end %>
</li>
错误登录
Started POST "/microposts" for 127.0.0.1 at 2012-11-24 10:20:26 -0800
Processing by MicropostsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+x5dhM0t5pZzB0flEo46n+Rr8OKcxNdgaAgvkh46dek=", "micropost"=>{"content"=>"Please work"}, "commit"=>"Post"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 103 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 103 LIMIT 1
(0.1ms) BEGIN
SQL (0.6ms) INSERT INTO "microposts" ("content", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["content", "Please work"], ["created_at", Sat, 24 Nov 2012 18:20:26 UTC +00:00], ["updated_at", Sat, 24 Nov 2012 18:20:26 UTC +00:00], ["user_id", 103]]
(1.7ms) COMMIT
Rendered shared/_feed.html.erb (2.2ms)
Rendered microposts/create.js.erb (4.1ms)
Completed 500 Internal Server Error in 14ms
ActionView::Template::Error (undefined method `any?' for nil:NilClass):
1: <% if @feed_items.any? %>
2: <ol class="microposts">
3: <%= render partial: 'shared/feed_item', collection: @feed_items %>
4: </ol>
app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb___3604038303202035148_70347748984380'
app/views/microposts/create.js.erb:1:in `_app_views_microposts_create_js_erb__2642480281700969603_70347793753860'
app/controllers/microposts_controller.rb:8:in `create'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (11.6ms)
linkto github https://github.com/thebusiness11/appsample
错误渲染微博/微博部分
Started POST "/microposts" for 127.0.0.1 at 2012-11-24 12:23:25 -0800
Processing by MicropostsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+x5dhM0t5pZzB0flEo46n+Rr8OKcxNdgaAgvkh46dek=", "micropost"=>{"content"=>"rendering microposts/micropost partial\r\n"}, "commit"=>"Post"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 103 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 103 LIMIT 1
(0.2ms) BEGIN
SQL (5.5ms) INSERT INTO "microposts" ("content", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["content", "rendering microposts/micropost partial\r\n"], ["created_at", Sat, 24 Nov 2012 20:23:25 UTC +00:00], ["updated_at", Sat, 24 Nov 2012 20:23:25 UTC +00:00], ["user_id", 103]]
(2.2ms) COMMIT
Rendered microposts/_micropost.html.erb (26.8ms)
Rendered microposts/create.js.erb (32.4ms)
Completed 500 Internal Server Error in 140ms
ActionView::Template::Error (undefined method `content' for nil:NilClass):
1: <li>
2: <span class="content"><%= micropost.content %></span>
3: <span class="timestamp">
4: Posted <%= time_ago_in_words(micropost.created_at) %> ago.
5: </span>
app/views/microposts/_micropost.html.erb:2:in `_app_views_microposts__micropost_html_erb__2883653912254136485_70298849712020'
app/views/microposts/create.js.erb:1:in `_app_views_microposts_create_js_erb__910732530435705121_70298849590600'
app/controllers/microposts_controller.rb:8:in `create'
答案 0 :(得分:0)
您正在尝试渲染'shared / feed',它会立即查看@feed_items实例变量,该变量未在您的创建操作中设置。你的意思是改为渲染'shared / micropost'吗?
答案 1 :(得分:0)
所以事实证明我需要做的就是传递对象@micropost
$(".microposts").prepend("<%= escape_javascript(render(:partial => 'shared/feed_item', :object => @micropost)) %>")