Forum: Ruby on Rails collection_select help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
61ec20ecc315e4e5d84b522d97af72b3?d=identicon&s=25 amna (Guest)
on 2009-04-24 19:28
(Received via mailing list)
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
<h1>New movie_type</h1>

<%= error_messages_for :movie_type %>

<% form_for(@movie_type) do |f| %>
  <p>
    <b>Type</b><br />
<%= collection_select(:type, :type_id, Type.find(:all), :id, :name)%>
  </p>

  <p>
    <b>Movie</b><br />
<%= collection_select(:movie, :movie_id, Movie.find
(:all), :id, :title) %>
  </p>

  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

<%= link_to 'Back', movie_types_path %>


thank you
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-24 19:48
(Received via mailing list)
On Apr 24, 12:39 pm, amna <a.alhi...@gmail.com> 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
61ec20ecc315e4e5d84b522d97af72b3?d=identicon&s=25 Amna K (Guest)
on 2009-04-25 21:07
(Received via mailing list)
Thaaaaaaaaaaaaank you =D its working now

2009/4/24 Frederick Cheung <frederick.cheung@gmail.com>:
This topic is locked and can not be replied to.