Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails中正确使用MVC将数据以表单形式传递到数据库_Ruby On Rails - Fatal编程技术网

Ruby on rails 在Rails中正确使用MVC将数据以表单形式传递到数据库

Ruby on rails 在Rails中正确使用MVC将数据以表单形式传递到数据库,ruby-on-rails,Ruby On Rails,我是Ruby和Rails的新手,正在构建一个web应用程序,需要使用form_for将数据提交到数据库。我知道我缺少Rails MVC逻辑的一个关键概念,需要帮助填补这个空白。我有一个有很多帖子的大学对象。我试图从表单_传递数据,但也包括可以使用params散列从URL中提取的数据。这是我的密码: Posts_Controller.rb: class PostsController < ApplicationController def new @college = College.f

我是Ruby和Rails的新手,正在构建一个web应用程序,需要使用form_for将数据提交到数据库。我知道我缺少Rails MVC逻辑的一个关键概念,需要帮助填补这个空白。我有一个有很多帖子的大学对象。我试图从表单_传递数据,但也包括可以使用params散列从URL中提取的数据。这是我的密码:

Posts_Controller.rb:

class PostsController < ApplicationController

def new
 @college = College.find(params[:college_id])
 @post = Post.new(:college_id => @college.id)
 @title = "Submit Post"
end

def create
 @post = Post.new(params[:post])
 if @post.save
 redirect_to root_path, :flash => { :success => "Post Submitted Successfully!" }
else
  @title = "Submit Post"
  render 'new'
end
end....
查看新帖子,new.html.erb:

<h1>Submit a <%= @college.name %> Post</h1>
<%= form_for @post do |f| %> 
<%= render 'fields', :f => f %>
<div class="actions">
<%= f.submit "Submit" %> 
</div>
<% end %>
<%= render 'shared/error_messages', :object => f.object %> 

<div class="field">
<%= f.label :type %><br />
<%= f.text_field :type %> 
</div>
<div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %>
</div> 
<div class="field">
<%= f.label :content_type %><br />
<%= f.text_field :content_type %> 
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %> 
</div>
提交帖子
f%>
从new.html.erb调用的“字段”文件:

<h1>Submit a <%= @college.name %> Post</h1>
<%= form_for @post do |f| %> 
<%= render 'fields', :f => f %>
<div class="actions">
<%= f.submit "Submit" %> 
</div>
<% end %>
<%= render 'shared/error_messages', :object => f.object %> 

<div class="field">
<%= f.label :type %><br />
<%= f.text_field :type %> 
</div>
<div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %>
</div> 
<div class="field">
<%= f.label :content_type %><br />
<%= f.text_field :content_type %> 
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %> 
</div>
f.object%>




表单提交时没有问题,但它只发布表单中包含的字段。我想传递更多的数据,比如新帖子页面URL中的college_id。以下是URL在新页面上显示的内容:http://localhost:3000/colleges/1/posts/new". 我想将URL中的“1”传递到我的post db中的college_id字段。让我知道如果有人可以帮助,让我知道如果我需要张贴更多的代码


谢谢

一种方法是在表单中添加
。这应该用正确的id填充您的表单,然后我相信您的post模型有一个属性
属于:college

但我个人不推荐这种处理嵌套对象的方法。查看以下网络广播,并根据您的需要更改实现: