在我的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]
不起作用,因爲創建的章節返回nil for chapter.place。我需要做什麼,@ 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>
這可能是有用的日誌PARAMS。嘗試Rails.logger.debug「====>#{params}」 –
向我們展示您的數據庫架構,如果您不介意:) – RuNpiXelruN
@RuNpiXelruN添加數據庫架構(我希望,就是這個,你想要的,我是業餘愛好者) – Metaphysiker