roobnoob
roobnoob

Reputation: 45

Using MVC in Rails correctly to pass data to database with form_for

I'm new to Ruby and Rails and am building a web app where I need to submit data to my database using form_for. I know I'm missing a key concept of the Rails MVC logic and need help closing the gap. I have a College object that has_many Posts. I'm trying to pass data from the form_for but also include data that can be pulled off of the URL using the params hash. Here's my code:

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....

routes.rb:

MyApp::Application.routes.draw do
get "sessions/new"

resources :posts
resources :smacks
resources :redemptions
resources :users do
  resources :microposts, :only => [:index]
  member do
    get :following, :followers
  end
end
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
resources :colleges, :only => [:new, :create, :index, :show] do
  resources :posts
  resources :smacks
  resources :redemptions
end

match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'

View for new post, 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 %>

'fields' file called from new.html.erb:

<%= 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>

The form submits with no problem, but it only posts fields included in the form_for. I want to pass more data such as college_id which is in the URL of the new_post page. Here's what the URL shows on the new page: "http://localhost:3000/colleges/1/posts/new". I want to pass the "1" from the URL to the college_id field in my post db. Let me know if anyone can help and let me know if I need to post anymore code.

Thanks!

Upvotes: 1

Views: 303

Answers (1)

brayne
brayne

Reputation: 1405

One way you can do it is to add a <%= f.hidden_field(:college_id, @college.id) %> to your form. This should populate your form with the correct id and then I believe your model for post has a property belongs_to :college .

Personally I would not recommend this way of handling nested objects though. Take a look at the following webcast and alter the implementation to your need: http://railscasts.com/episodes/196-nested-model-form-part-1

Upvotes: 0

Related Questions