Does it posible to have Partial form,when multiple Models managed in a Rails form?
I have a form that manag开发者_开发问答ed multiple models,i treid to use Partial form for edit and new . when i want to create a record i couldn't change the name of submit button to 'mmmm' via this code:
<%= form_for(@word) do |w| %>
<%= render :partial => 'form',:locals => {:w =>w,:submit_lable =>'mmmm'} %>
<% end %>
when i tried to edit one record of table i.e record 2 from Table A ,Record 2 from Table B and Table C are too appeare in my form how can i avoid these? code for edit:
def edit
@word=Word.find(params[:id])
@verb=Verb.find(params[:id])
@adjektiv=Adjektiv.find(params[:id])
@adverb =Adverb.find(params[:id])
end
and this is my code in my partial form:
<html>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Nomen</a> </li>
<li><a href="#tabs-2">Verb</a> </li>
<li><a href="#tabs-3">Adjektiv</a> </li>
<li><a href="#tabs-4">Adverb</a> </li>
</ul>
<div id="tabs-1">
<table >
<tr>
<td align="right">
Artikel :
</td>
<td align="center">
<%= w.label :Artikel ,'der' %><%= w.radio_button :Artikel,'der',:checked =>false%>
<%= w.label :Artikel ,'die' %><%= w.radio_button :Artikel,'die',:checked =>true %>
<%= w.label :Artikel ,'das' %><%= w.radio_button :Artikel,'das',:checked =>false %>
</td>
</tr>
<tr>
<td align="right">
Neues Wort:
</td>
<td>
<%= w.text_field :Name ,:size => '31*45'%>
</td>
</tr>
<tr>
<td align="right">
Synonome :
</td>
<td>
<%= w.text_field :syn ,:size => '31*45'%>
</td>
</tr>
<tr>
<td align="right">
Bedeutung :
</td>
<td>
<%= w.text_area :bedeutung , :size =>'30*45' %>
</td>
</tr>
<tr>
<td align="right">
Beispiel :
</td>
<td>
<%= w.text_field :beispiel ,:size => '31*45' %>
</td>
</tr>
<tr>
<td>
Kommentar:
</td>
<td>
<%= w.text_area :kommentar ,:size =>'30*45' %>
</td>
</tr>
<tr>
<td align="center">
<%= w.submit 'Hinfügen' %>
</td>
</tr>
</table>
</div>
<%= fields_for (@verb) do |v| %>
<div id="tabs-2">
<table>
<tr>
<td align="right">
Das Verb :
</td>
<td>
<%= v.text_field :name %>
</td>
</tr>
<tr>
<td>
Hilfsverb :
</td>
<td align="center">
<%= v.label :hverb ,'Sein' %><%= v.radio_button :hverb,'Sein',:checked =>false%>
<%= v.label :hverb ,'haben' %><%= v.radio_button :hverb,'haben',:checked =>true %>
<%= v.label :hverb ,'beide' %><%= v.radio_button :hverb,'beide',:checked =>false %>
</td>
</tr>
<tr>
<td align="right">
Synonome :
</td>
<td>
<%= v.text_field :syn %>
</td>
</tr>
<tr>
<td align="right">
Stammenform :
</td>
<td>
<%= v.text_field :stmform%>
</td>
</tr>
<tr>
<td align="right">
Bedeutung :
</td>
<td>
<%= v.text_field :bedeutung %>
</td>
</tr>
<tr>
<td align="right">
Beispiel :
</td>
<td>
<%= v.text_area :beispiel ,:size => '23*45' %>
</td>
<tr>
<td align="right">
kommentar :
</td>
<td>
<%= v.text_area :kommentar ,:size => '23*45' %>
</td>
</tr>
<tr>
<td align="right">
Präposition :
</td>
<td>
<%= v.text_field :prep %>
</td>
</tr>
<tr>
<td>
<%= w.submit 'Hinfügen' %>
</td>
</tr>
</table>
</div>
<% end %>
<div id="tabs-3">
<%= fields_for (@adjektiv) do |a| %>
<table>
<tr>
<td align="right">
Adjektiv :
</td>
<td>
<%= a.text_field :name %>
</td>
</tr>
<tr>
<td align="right">
Bedeutung :
</td>
<td>
<%= a.text_field :bedeutung %>
</td>
</tr>
<tr>
<td align="right">
Synonome :
</td>
<td>
<%= a.text_field :syn %>
</td>
</tr>
<tr>
<td>
Komperativform :
</td>
<td>
<%= a.text_field :kom %>
</td>
</tr>
<tr>
<td>
<%= w.submit 'Hinfügen' %>
</td>
</tr>
</table>
<% end %>
</div>
<div id="tabs-4">
<%= fields_for (@adverb) do |av| %>
<table>
<tr>
<td align="right">
Adverb :
</td>
<td>
<%= av.text_field :name %>
</td>
</tr>
<tr>
<td align="right">
Bedeutung :
</td>
<td>
<%= av.text_field :bedeutung %>
</td>
</tr>
<tr>
<td align="right">
Synonome :
</td>
<td>
<%= av.text_field :syn %>
</td>
</tr>
<tr>
<td>
<%= w.submit 'Hinfügen' %>
</td>
</tr>
</table>
<% end %>
</div>
</div>
</html>
and this is my controller
class WordsController < ApplicationController
def new
@word=Word.new
@verb=Verb.new
@adjektiv=Adjektiv.new
@adverb=Adverb.new
end
def create
@word=Word.create(params[:word])
@verb=Verb.create(params[:verb])
@adjektiv=Adjektiv.create(params[:adjektiv])
@adverb=Adverb.create(params[:adverb])
if @word.save || @verb.save || @adjektiv || @adverb
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def index
@word=Word.find(:all)
@verb=Verb.find(:all)
@adjektiv=Adjektiv.find(:all)
@adverb =Adverb.find(:all)
end
def edit
@word=Word.find(params[:id])
@verb=Verb.find(params[:id])
@adjektiv=Adjektiv.find(params[:id])
@adverb =Adverb.find(params[:id])
end
def update
@word =Word.find(params[:id])
if @word.update_attributes(params[:word]) || @verb.update_attributes(params[:verb]) || @adjektiv.update_attributes(params[:adjektiv]) || @adverb.update_attributes(params[:adverb])
redirect_to :action => 'index'
else
redirect_to :action => 'edit'
end
end
end
thnk you for your helps
Thanks for posting the code for your partial. You seem to have 2 questions, so I'll answer them individually.
"when i want to create a record i couldn't change the name of submit button to 'mmmm'"
OK, so I see you're passing in a local variable to your partial:
:submit_lable =>'mmmm'
But your submit button is inserted with the following code:
<%= w.submit 'Hinfügen' %>
Passing in the local variable :submit_lable
won't set the label of the submit button straight away, all it does is create a variable in the partial called submit_lable
that you can use to accomplish that with this line of code:
<%= w.submit submit_lable %>
when i tried to edit one record of table i.e record 2 from Table A ,Record 2 from Table B and Table C are too appeare in my form how can i avoid these?
The way you've written your partial, it doesn't look like a partial. A partial should be a chunk of view code with a well-defined purpose. It should help you adhere to the DRY principle, and thus you shouldn't have repeated code in there. You also shouldn't have <html>..</html>
tags in your partial because it's supposed to represent a part of a complete HTML page, not the whole thing, and HTML tags inside a HTML body is not valid or sensible HTML. It actually looks like you're writing a layout, so you should have a think about how your page is supposed to fit together and move the structure around those form tags into your app/views/layouts/application.html.erb
file.
Have a read of the Rails Guides sections on partials and layouts. It talks about local variables and general guidelines for using partials and layouts.
Anyway, to answer your question: the reason you're seeing all the other forms for your various models is because inside your partial, you have forms for all those models. You're accessing the records for each model using params[:id]
and displaying them all at once, in tabs it seems.
This isn't pretty, and it's not easy to deal with. Really you should be dealing with the models one at a time.
The edit
action in your controller should be dealing with a single model. You should have a controller for each of your models, so in your WordsController
, for instance, you would have:
def edit
@word = Word.find(params[:id])
end
Same goes for the rest of them. I'm not too sure what your form is doing, but I'm guessing the first div contains the form you're wanting to use for @word
. In this case, your partial _form.html.erb
might be:
<table>
<tr>
<td align="right">Artikel :</td>
<td align="center">
<%= w.label :Artikel ,'der' %><%= w.radio_button :Artikel,'der',:checked =>false%>
<%= w.label :Artikel ,'die' %><%= w.radio_button :Artikel,'die',:checked =>true %>
<%= w.label :Artikel ,'das' %><%= w.radio_button :Artikel,'das',:checked =>false %>
</td>
</tr>
<tr>
<td align="right">Neues Wort:</td>
<td><%= w.text_field :Name ,:size => '31*45'%></td>
</tr>
<tr>
<td align="right">Synonome :</td>
<td><%= w.text_field :syn ,:size => '31*45'%></td>
</tr>
<tr>
<td align="right">Bedeutung :</td>
<td><%= w.text_area :bedeutung , :size =>'30*45' %></td>
</tr>
<tr>
<td align="right">Beispiel :</td>
<td><%= w.text_field :beispiel ,:size => '31*45' %></td>
</tr>
<tr>
<td>Kommentar:</td>
<td><%= w.text_area :kommentar ,:size =>'30*45' %></td>
</tr>
<tr>
<td align="center"><%= w.submit submit_lable %></td>
</tr>
</table>
I'm not sure how having all the forms in tabs on the same page can work - how does switching to a different model with the same ID in the database help you? I'd recommend you ditch the tabs. If I'm nor understanding the purpose of the tabs and they're required, try using Ajax to fetch the form content if and when it is required.
Sorry for the long post, but I hope it helps somewhat. There's not really a straight-forward answer for the 2nd part, as you'll have to do a bit of reading and thinking for yourself to work out the best solution! Do have a read about rendering, layouts and partials. Rails has some powerful tools to help make your life easy, so it's really worth getting to know what's at your disposal so you don't make things hard for yourself :).
精彩评论