我现在已经坚持这个问题太久了 - 所以希望有人能够告诉我我做错了什么。我试图在我的网站上添加一个简单的博客,通过远程使用rails ajax:true选项 - 除编辑帖子外,一切正常。
所有内容都是通过scaffold命令生成的,我的印象是它实际上应该正常工作。
如上所述,不起作用的是编辑帖子。
返回错误:
ActionController::UrlGenerationError in Posts#edit
Showing ~/webapp-rails4/app/views/posts/_form.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"posts", :locale=>#<Post id: 6, title: "blog post nummer 2", body: "så prøver vi igen", created_at: "2014-11-02 11:42:19", updated_at: "2014-11-02 11:42:19", image: "Sk_rmbillede_2014-06-23_kl._21.31.58.png">, :id=>nil, :format=>nil} missing required keys: [:locale, :id]
1: <% logger.debug 'form being rendered'%>
2: <%= form_for @post, remote: true do |f| %>
3:
4:
5: <div class="field">
app/views/posts/_form.html.erb:2:in _app_views_posts__form_html_erb__3358809483585932967_70366784059520'
app/views/posts/edit.js.erb:2:in _app_views_posts_edit_js_erb___2360131588799055390_70366783957680'
我的编辑链接:
<%= link_to 'Edit', edit_post_path(post.id, locale: I18n.locale), remote: true %>
控制器:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
respond_to :js, :html
def edit
logger.debug 'controller action edit'
respond_with(@post)
end
def create
@post = Post.new(post_params)
@post.save
#respond_with(@post)
end
def update
@post.update(post_params)
#respond_with(@post)
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :body, :image)
end
edit.js.erb(我在这里尝试了很多东西,这就是我现在所拥有的):
alert('this is edit.js.erb');
$('#post<%= @post.id %> #editPostModal').append("<%= escape_javascript(render 'form', object: @post) %>");
$('#post<%= @post.id %> #editPostModal').foundation('reveal', 'open');
_form.html.erb(似乎是抛出错误的原因):
<% logger.debug 'form being rendered'%>
<%= form_for @post, remote: true, :multipart => true, authenticity_token: true do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div>
<%= f.label :remote_image_url, "or image URL" %><br>
<%= f.text_field :remote_image_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
rake routes:
posts GET /:locale/posts(.:format) posts#index {:locale=>/en|dk/}
POST /:locale/posts(.:format) posts#create {:locale=>/en|dk/}
new_post GET /:locale/posts/new(.:format) posts#new {:locale=>/en|dk/}
edit_post GET /:locale/posts/:id/edit(.:format) posts#edit {:locale=>/en|dk/}
post GET /:locale/posts/:id(.:format) posts#show {:locale=>/en|dk/}
PATCH /:locale/posts/:id(.:format) posts#update {:locale=>/en|dk/}
PUT /:locale/posts/:id(.:format) posts#update {:locale=>/en|dk/}
DELETE /:locale/posts/:id(.:format) posts#destroy {:locale=>/en|dk/}