2015-11-08 68 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>