无法读取控制器中的参数,Ruby on Rails

时间:2016-01-17 20:32:41

标签: ruby-on-rails

在我的Ruby on Rails项目中,根据您点击的链接,我希望生成的对象具有不同的编号。

我有一本书和章节模型,章节属于书。 Chapter有一个名为“place”的整数属性。

目前,当我点击链接时:http://localhost:3000/1/chapters/new/ 它呈现new.html.erb页面,一切正常。

现在,我添加了一条新路线:

chapterchapter GET    /:book_id/chapters/new(.:format)/:place     chapters#new

此链接

<%= link_to "Contribute", chapterchapter_path(@book, 5 ) %>

将导致:

http://localhost:3000/1/chapters/new/5

在我的控制器中,我有这行代码:

  def new
    @chapter = Chapter.new
  end

  def create
    @chapter = Chapter.new(chapter_params)
    @chapter.book_id = @book.id
    @chapter.user_id = current_user.id
    @chapter.place = params[:place]

我的问题是,这行代码:@chapter.place = params[:place]不起作用,因为创建的章节为chapter.place返回nil。我需要做什么,@ chapter.place读取参数:place?有什么建议吗?

编辑:我的数据库架构:

ActiveRecord::Schema.define(version: 20160117110001) do

  create_table "books", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.integer  "maxnumchapt"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "min_length"
    t.integer  "max_length"
    t.integer  "user_id"
  end

  create_table "chapters", force: :cascade do |t|
    t.string   "chaptertitle"
    t.text     "chaptercontent"
    t.string   "author"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
    t.integer  "book_id"
    t.integer  "user_id"
    t.integer  "place"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

编辑2:看来,只要按下提交按钮,参数位置就会丢失:

Started GET "/30/chapters/new/2" for 127.0.0.1 at 2016-01-17 23:39:07 +0100
Processing by ChaptersController#new as HTML
  Parameters: {"book_id"=>"30", "place"=>"2"}
  Book Load (0.1ms)  SELECT  "books".* FROM "books" WHERE "books"."id" = ? LIMIT 1  [["id", 30]]
  Rendered chapters/new.html.erb within layouts/application (36.8ms)
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 200 OK in 55ms (Views: 53.2ms | ActiveRecord: 0.2ms)


Started POST "/books/30/chapters" for 127.0.0.1 at 2016-01-17 23:39:16 +0100
Processing by ChaptersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"566kl+qg5otw1FGVh0uIoytGxFlF5qp0c4Ws41JhtzSzhqWuND997PltE7LXGiIvEC7A/uqCtg8WMdnNndN/Rg==", "chapter"=>{"chaptertitle"=>"fadfadfaf", "chaptercontent"=>"afddfaf", "author"=>"adfadfa"}, "commit"=>"Create Chapter", "book_id"=>"30"}
  Book Load (0.1ms)  SELECT  "books".* FROM "books" WHERE "books"."id" = ? LIMIT 1  [["id", 30]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]

编辑3:章节/ new.html

<%= simple_form_for([@book, @book.chapters.build]) do |f| %>
    <%= f.input :chaptertitle %>
    Mininmum amount of characters: <%= @book.min_length %> Maximum amount of characters: <%= @book.max_length %>
    <div class="counting" ><%= f.input :chaptercontent %>Characters: <span class="count"></span></div><br>
    <%= f.input :author %>
    <%= f.button :submit %>
<% end %>

<script>

  $(document).ready(function(){

    var choosingbar = function( event ){
      $(event.target).parents(".counting").children(".count").text($(event.target).val().length);
    };

    $(".counting textarea").keyup(choosingbar);
  });

</script>

1 个答案:

答案 0 :(得分:1)

您应该在 表单 中有 隐藏字段 ,以便抓住{{1}并将其发回params[:place]行动。

create

现在在创建操作中,可以像<%= simple_form_for([@book, @book.chapters.build]) do |f| %> <%= f.input :chaptertitle %> Mininmum amount of characters: <%= @book.min_length %> Maximum amount of characters: <%= @book.max_length %> <div class="counting" ><%= f.input :chaptercontent %>Characters: <span class="count"></span></div><br> <%= f.input :author %> <%= f.input :place, :as => :hidden, :input_html => { :value => params[:place] } %> <%= f.button :submit %> <% end %>

一样访问它