Rails 谁能说下这段代码是怎么了?

zhangyanan · 2014年05月30日 · 最后由 lex 回复于 2014年05月31日 · 4992 次阅读
def update
p "====================="
    @blog = Blog.find(params[:blog_id])
    if @blog
      @blog.update_attributes(params[:blog])
      @blog.save
      redirect_to :action => :index
    else
      redirect_to :action => :new
    end
  end
<%= form_for(@blog, {:url => {:action => :update,:blog_id => @blog.id}}) do |f| %>
   <h3>blog , world!</h3>

        <p>This is a template for a simple marketing or informational website. It includes a large callout called a
          jumbotron and three supporting pieces of content. Use it as a starting point to create something more
          unique.</p>
        <div class="input-group input-group-lg" style="width: 1000px">
          <%= f.text_field :title,:class => "form-control", :placeholder => "title" %>
        </div>

        <%= f.text_area :content,:id => "myEditor", :rows => 40, :cols => 120 %>

        <label>分类:</label><%= f.select :blog_type_id, options_from_collection_for_select(@blog_types, "id", "name") %>
        <br/>
        <%= f.submit "发表日志", :class => 'btn btn-lg btn-primary' %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <%= f.button "保存草稿", :class => 'btn btn-lg btn-primary' %>
        <br/><br/>
 <% end %>

对数据进行修改,然后提交到 update,不打印 P,但是可以看到提交的参数,最后渲染 update 页面,迷惑

看一下你 Rails 的 log 输出,另外干嘛要显式使用blog_id这个参数啊?

看看 log 里边类似于这样的输出:

Started GET "/" for 127.0.0.1 at 2014-05-30 16:33:14 +0800
Processing by xxxxxController#index as HTML

Rails.logger.info replaced p/puts

#1 楼 @Martin91

Started PUT "/blog/update?blog_id=15" for 127.0.0.1 at Fri May 30 16:40:56 +0800 2014
Processing by BlogController#update as HTML
  Parameters: {"commit"=>"发表日志", "blog_id"=>"15", "blog"=>{"blog_type_id"=>"1", "content"=>"<p>the house on the lemon tree,there is one beautiful girl and a lucky family.</p>", "title"=>"hello world "}, "utf8"=>"✓", "authenticity_token"=>"c2x2NXRFpnFXf3+MqNfyjR3gET7VS7bSd/FeP+rmF8Q="}
  Rendered blog/update.html.erb within layouts/application (1.0ms)
Completed 200 OK in 11.0ms (Views: 11.0ms | ActiveRecord: 0.0ms)

因为总是渲染 update,我就加了一个空的 update 页面

#3 楼 @zhangyanan 可是从代码上看就是没有经过你贴的那段 controller 的代码啊,检查看看是不是一些文件名或者类名之类的有错误?确认 update 的代码没写错地方?

#5 楼 @Martin91 找到了,有一个相同的 action!!!!!

不服不行啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号