2015-11-08 116 views
1

混亂的原因是在一天的開始我所有的表格都在工作。在設計項目後,我的所有表單都停止工作。當我全部提交時,交易在控制檯中工作,除了社區創建。 如果您需要更多的代碼來了解,請詢問。我真的很困惑我的表格

社區控制器

def create 
current_user 
@community = Community.new(community_params) 
@community.user_id = session[:user_id] 
if @community.save 
    flash[:notice] = "Post Created" 
else 
    flash[:alert] = "Error post not created" 
end 
    redirect_to @community 
end 

def new 
current_user 
@community = Community.new 
end 

社區新視點

<%= render partial: 'users/user_profile' %> 
<br> 
<h1 class="create_new_community">CREATE A NEW COMMUNITY</h1> 

<form class="community_form"> 
<%= form_for @community do |f| %> 
    <div class="title"> 
    <%= f.text_field :title, placeholder: "Title of Community", rows: 40, class: "community_title" %> 
    </div> 
    <div class="text_field"> 
    <%= f.text_area :bio, placeholder: "Enter Community bio here...", :cols => 90, :rows => 10, :class => 'community_text_field' %> 
    </div> 
    <div class="submit_post"> 
    <%= f.submit class: "CREATE NEW COMMUNITY" %> 
    </div> 
<% end %> 
</form> 

評論控制器

def create 
@comment = @commentable.comments.new comment_params 
@comment.save 
redirect_to @commentable 
end 

def new 
@comment = Comment.new 
end 

評論 - 視圖 - 新

<p> 
<h2 class="comment_header">Comments</h2> 

<% commentable.comments.reverse.each do |comment| %> 
    <div id="well"> 
    <div class='image_name'> 
    <%= image_tag comment.user.avatar.url(:medium), class: 'comment_image' %> 
    <span class='comment_user_name'><%= comment.user.first_name %></span> 
    </div> 
    <div class="arrow_box"> 
    <ul class'messagebox'><%= comment.text %> - 
    <span class='comment_time'><%= time_ago_in_words(comment.created_at) %>  <strong>ago</strong></span> 
    </ul> 
    </div> 
    </div> 
    <hr> 
<% end %> 
</p> 

對於所有那些在解決這個錯誤或方向指向調試錯誤提供幫助,謝謝了很多。也感謝所有查看此頁面的人。

-Steven

編輯 提交評論時,在我的控制檯和發現錯誤

Started POST "/communities/14/comments" for 127.0.0.1 at 2015-11-09 13:08:23 -0500 
Processing by Communities::CommentsController#create as JS 
Parameters: {"utf8"=>"✓", "comment"=>{"text"=>"hi"}, "commit"=>"new_comment_button", "community_id"=>"14"} 
Community Load (0.1ms) SELECT "communities".* FROM "communities" WHERE "communities"."id" = ? LIMIT 1 [["id", 14]] 
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 11]] 
(0.1ms) begin transaction 
SQL (0.4ms) INSERT INTO "comments" ("commentable_id", "commentable_type", "created_at", "text", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["commentable_id", 14], ["commentable_type", "Community"], ["created_at", "2015- 11-09 18:08:23.804413"], ["text", "hi"], ["updated_at", "2015-11-09 18:08:23.804413"], ["user_id", 11]] 
(0.6ms) commit transaction 
Redirected to http://localhost:3000/communities/14 
Completed 302 Found in 6ms (ActiveRecord: 1.2ms) 
+0

請想出一個更具描述性的,有用的稱號。 –

+0

您可以在提交表單時顯示日誌嗎? –

+0

@KMRakibulIslam我添加了我在控制檯中看到的內容,一旦我點擊提交我的評論表單。 –

回答

2

這是因爲在社區新視角,你已經添加了HTML表單標記爲以及導軌助手只是刪除html格式標記:

<form > #remove this 
<%= form_for @community, html:{ class: "community_form"} do |f| %> 
    #you form fields 
<% end %> 
</form> #remove this 

希望這會有所幫助!

+0

謝謝!現在我可以通過刪除前面提到的

來創建一個新社區。我還有一個最後的問題,我仍然無法點擊提交評論表單。當我點擊它時,什麼也沒有發生,但是在控制檯中,它通過並保存在數據庫中。你知道可能是什麼原因嗎? –

+0

剛剛在控制檯中注意到,在它提交事務後,我找到了一個Completed 302 Found。 –

+0

找到原因是由於遙遠:真實。由於我沒有ajax的要求,所以它不允許我們進入設定的路線。無論哪種方式,再次感謝你。 –

0

你會是最好的有以下:

#app/views/communities/new.html.erb 
<%= render partial: 'users/user_profile' %> 
<h1 class="create_new_community">CREATE A NEW COMMUNITY</h1> 

<div class="community_form"> 
    <%= form_for @community do |f| %> 
    <div class="title"> 
     <%= f.text_field :title, placeholder: "Title of Community", rows: 40, class: "community_title" %> 
    </div> 
    <div class="text_field"> 
     <%= f.text_area :bio, placeholder: "Enter Community bio here...", :cols => 90, :rows => 10, :class => 'community_text_field' %> 
    </div> 
    <div class="submit_post"> 
     <%= f.submit "CREATE NEW COMMUNITY" %> 
    </div> 
    <% end %> 
</div>