Problème avec clé étrangère

Bonjour

J’ai un model “User”. L’utilisateur a une clé étrangère(state_id) qui
reference le model “state”.

Le problème, est le suivant: Lorsque que j’ajoute un utilisateur en
spécifiant son “state_id”, alors celui-ci est null dans la base de
données.

Dans le formulaire d’ajout d’utilisateur, j’ai le code suivant:

<%= f.select(:state_id, State.find(:all).map {|c|
[c.name,c.id]},{:prompt => “Select a canton”}, {:onchange =>
“#{remote_function(:url => {:action =>
“update_municipals”},=>”‘name=’+value")}"}) %>

Qu’est-ce que je dois faire, pour que l’id de state soit enregistré
comme voulu?

Merci d’avance

Ali Fakheri wrote:

<%= f.select(:state_id, State.find(:all).map {|c|
[c.name,c.id]},{:prompt => “Select a canton”}, {:onchange =>
“#{remote_function(:url => {:action =>
“update_municipals”},=>”‘name=’+value")}"}) %>

Qu’est-ce que je dois faire, pour que l’id de state soit enregistré
comme voulu?

Ça dépend de ton code…


Cyril M.

Ça dépend de ton code…

#users_controller
def update_municipals

state = State.find(params[:name])
@municipals = state.municipals
a = 0

render :update do |page|
  page.replace_html 'municipalfd', :partial => 'cantcoll', :object 

=> @municipals
end
end

#new.html.erb (tjr pour users)
<%= error_messages_for :user %>
<% form_for :user, :url => users_path do |f| -%>

Login
<%= f.text_field :login %>

Email
<%= f.text_field :email %>

Password
<%= f.password_field :password %>

Confirm Password
<%= f.password_field :password_confirmation %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :surname %>
<%= f.text_field :surname %>

<%= f.label :birthday %>
<%= f.date_select :birthday, :start_year => 1900, :end_year => 2100%>

<%= f.label :state %>
<%= f.select(:state_id, State.find(:all).map {|c| [c.name, c.id]},{:prompt => "Select a state"}, {:onchange => "#{remote_function(:url => {:action => "update_municipals"}, :with => "'name='+value")}"}) %>

<%= render :partial => 'cantcoll', :object => @municipals %>

Select the role of the user: <%= f.select(:role, %w{ voter admin}) %>

<%= f.label :has_voted %>
<%= f.check_box :has_voted %>

<%= submit_tag ‘Sign up’ %>


<% end -%>

#_cantcoll.html.erb
<% if @municipals == nil %>
<%= select(nil,:municipal, “select a canton first”) %>
<% else %>
<%= collection_select(:user,:municipal, @municipals, :municipal_name,
:municipal_name) %>
<% end %>