我真的很困惑我的表格

时间:2015-11-08 23:06:57

标签: ruby-on-rails forms model-view-controller controller

混淆的原因是在我的所有表格开始工作的那一天。在我为项目设计样式后,我的所有表单都停止了工作。当我提交所有内容时,除了社区创建外,该事务在控制台中工作。 如果您需要更多代码来理解,请询问。

社区控制器

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)

2 个答案:

答案 0 :(得分:2)

这是因为在社区新视图中,您添加了html表单标记以及rails helper,只需删除html表单标记:

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

希望这会有所帮助!

答案 1 :(得分: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>