# Collection\_select help

**URL:** <https://www.ruby-forum.com/t/collection-select-help/166397>\
**Category:** Rails\
**Created:** [April 24, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/collection-select-help/166397 "2009-04-24T19:28:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@amna](https://www.ruby-forum.com/u/amna)\
**Post date:** [April 24, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/collection-select-help/166397/1 "2009-04-24T19:28:16Z")

</div>

Hi,  
I’m new at this… can you please help me…  
the drop down menus are fine, however when i click submit the  
selection is not saved and it tells me that movie and type can’t be  
blank

Here is my movie\_types controller  
class MovieTypesController \< ApplicationController  
before\_filter :login\_required, :only =\> [:new, :create]

# GET /movie\_types

# GET /movie\_types.xml

def index  
@movie\_types = MovieType.find(:all)

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @movie_types }
end

```

end

# GET /movie\_types/1

# GET /movie\_types/1.xml

def show  
@movie\_type = MovieType.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @movie_type }
end

```

end

# GET /movie\_types/new

# GET /movie\_types/new.xml

def new  
@movie\_type = MovieType.new

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @movie_type }
end

```

end

# GET /movie\_types/1/edit

def edit  
@movie\_type = MovieType.find(params[:id])  
end

# POST /movie\_types

# POST /movie\_types.xml

def create  
@movie\_type = MovieType.new(params[:movie\_type])

```
respond_to do |format|
  if @movie_type.save
    flash[:notice] = 'MovieType was successfully created.'
    format.html { redirect_to(@movie_type) }
    format.xml { render :xml => @movie_type, :status

```

=\> :created, :location =\> @movie\_type }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @movie\_type.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

# PUT /movie\_types/1

# PUT /movie\_types/1.xml

def update  
@movie\_type = MovieType.find(params[:id])

```
respond_to do |format|
  if @movie_type.update_attributes(params[:movie_type])
    flash[:notice] = 'MovieType was successfully updated.'
    format.html { redirect_to(@movie_type) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @movie_type.errors, :status

```

=\> :unprocessable\_entity }  
end  
end  
end

# DELETE /movie\_types/1

# DELETE /movie\_types/1.xml

def destroy  
@movie\_type = MovieType.find(params[:id])  
@movie\_type.destroy

```
respond_to do |format|
  format.html { redirect_to(movie_types_url) }
  format.xml { head :ok }
end

```

end  
end

view: new.html

# New movie\_type

\<%= error\_messages\_for :movie\_type %\>

\<% form\_for(@movie\_type) do |f| %\>

**Type**  
\<%= collection\_select(:type, :type\_id, Type.find(:all), :id, :name)%\>

**Movie**  
\<%= collection\_select(:movie, :movie\_id, Movie.find (:all), :id, :title) %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, movie\_types\_path %\>

thank you

---

<div class="post-metadata">

**Author:** ![amna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@amna](https://www.ruby-forum.com/u/amna)\
**Post date:** [April 24, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/collection-select-help/166397/2 "2009-04-24T19:48:46Z")

</div>

On Apr 24, 12:39 pm, amna [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> I’m new at this… can you please help me…  
> the drop down menus are fine, however when i click submit the  
> selection is not saved and it tells me that movie and type can’t be  
> blank

Like the other hellers, the first parameter should be the name of the  
instance variable, ie ‘movie\_type’ in your case. Or, since you are  
using form\_for, you could use f.collection\_select and drop the first  
parameter altogether.

Fred

---

<div class="post-metadata">

**Author:** ![amna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@amna](https://www.ruby-forum.com/u/amna)\
**Post date:** [April 25, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/collection-select-help/166397/3 "2009-04-25T21:07:07Z")

</div>

Thaaaaaaaaaaaaank you =D its working now

2009/4/24 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
