尝试更新我在Ruby on Rails项目中发布的帖子,但没有任何反应。这真的很烦人,因为我没有得到任何错误,似乎无法弄清楚我做错了什么。
我认为它之前有用,但自从我第一次写出不同的动作后,我就在我的“feed”模型中添加了多个内容,例如展示次数和标记。不知道这是否影响了我的更新操作...
我的控制器看起来像这样:
def edit
@feed = Feed.find(params[:id])
end
def update
@feed = Feed.find(params[:id])
respond_to do |format|
if @feed.update_attributes(params[:feed])
format.html { redirect_to @feed, notice: 'Feed was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @feed.errors, status: :unprocessable_entity }
end
end
end
我的Feed模型如下:
attr_accessible :content, :tag_list, :guid, :language, :location, :published_at, :summary, :url, :title, :user_id, :thumbnail_url, :url_to_feed, :type_of_feed
has_many :impressions, :as=>:impressionable
validates_length_of :tag_list, :maximum => 10
acts_as_taggable
我的观点如下:
<h1>Editing feed</h1>
<%= render 'form' %>
<%= link_to 'Show', @feed %> |
<%= link_to 'Back', feeds_path %>
____________________ form
<%= form_for(@feed) do |f| %>
<% if @feed.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@feed.errors.count, "error") %> prohibited this feed from being saved:</h2>
<ul>
<% @feed.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 :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :language %><br />
<%= f.text_field :language %>
</div>
<div class="field">
<%= f.label :tag_list, "Tags (seperated by spaces)" %><br />
<%= f.text_field :tag_list %>
</div>
<div class="actions">
<%= f.submit %>
</div>
答案 0 :(得分:2)
日志条目
Started PUT "/feeds/1" for 127.0.0.1 at 2013-02-17 12:27:36 +0100 Processing by FeedsController#show as HTML
说,PUT请求(发送表单数据)正由FeedsController#show处理,但它必须由FeedsController#update处理。所以你的路线似乎是错的。查看Rails Routing Guide。
我会使用Feed ressource,因为它会自动创建正确的路径:
resources :feeds