I am new to Rails creating a new Rails app. My boss wants the index page to have a text field to enter a case number. If the case number exists then I need to open the edit page for that case number. If the case number doesn''t exist then I need to create a new one. The case_number is a text field of the table scenario, id is an autoincrement integer. I created an index.rhtml page with a form that pointed to an eval method. My eval method resembles this: @case_number = @scenario.case_number @scenario = Scenario.find(@case_number) if @scenario redirect_to :action => ''edit'', :id => @scenario.id else flash[''notice''] = ''Unable to find Scenario, creating a new one.'' render_action ''new'' end end I have tried different combinations of redirect_to and render_action. I am currently pulling my hair out trying to solve 2 main issues. 1. I can''t find the correct scenario given and existing case_number and Rails goes to the new page. what am I doing wrong? 2. when Rails sends me to the new page I don''t get a clean new page I get notified that some of my fields are blank. How can I tell Rails to ignore those errors when I do this? Am I even going about this the right way?
Hi, I''m hardly an expert at this myself, so take the following with a grain of salt.> > My eval method resembles this: > @case_number = @scenario.case_numberI''m not clear on what is happening above, would have to see the index.rhtml - it seems to assume that a scenario already has been found/created somehow.> @scenario = Scenario.find(@case_number)I _believe_ that you want to do @scenario = Scenario.find(:first, :conditions => [ "case_number = ?", @case_number]) --ryan
>I am currently pulling my hair out trying to solve 2 main issues. >1. I can''t find the correct scenario given and existing case_number >and Rails goes to the new page. what am I doing wrong? > >@case_number = @scenario.case_number @scenario = Scenario.find(@case_number) makes no sense... you should be getting the case number from the params...>2. when Rails sends me to the new page I don''t get a clean new page I >get notified that some of my fields are blank. How can I tell Rails to >ignore those errors when I do this? > > >render_action ''create'' _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
arrgg hit send way to soon... and for once it was actually an accident... Michael King wrote:>I am new to Rails creating a new Rails app. My boss wants the index >page to have a text field to enter a case number. If the case number >exists then I need to open the edit page for that case number. If the >case number doesn''t exist then I need to create a new one. > >The case_number is a text field of the table scenario, id is an >autoincrement integer. > >I created an index.rhtml page with a form that pointed to an eval method. > >My eval method resembles this: >@case_number = @scenario.case_number >@scenario = Scenario.find(@case_number) >if @scenario > redirect_to :action => ''edit'', :id => @scenario.id > else > flash[''notice''] = ''Unable to find Scenario, creating a new one.'' > render_action ''new'' > end > end > >I have tried different combinations of redirect_to and render_action. > >I am currently pulling my hair out trying to solve 2 main issues. >1. I can''t find the correct scenario given and existing case_number >and Rails goes to the new page. what am I doing wrong? > >you need to get the case number from the params in the form... something like @scenario = Scenario.find(@params[:case_number])>2. when Rails sends me to the new page I don''t get a clean new page I >get notified that some of my fields are blank. How can I tell Rails to >ignore those errors when I do this? > >what is your new method? why not redirect to ''new'' makes bookmarking easier _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I appreciate the help so far, but I am still having problems. Here is some information which I didn''t realize you guy might need to help me. Also if has a way to stop duplicating the code to populate the @team_array and @p_array, I tried moving it to a subfunction but it didn''t work, I am thinking I lost scope. My model/scenario.rb class Triage < ActiveRecord::Base validates_format_of :case_number, :with=>/[a-zA-Z]{5}\d{5}/, :message=>"must be 10 characters, 5 alphabetical followed by 5 numberic" validates_uniqueness_of :cr_number validates_length_of :sw_version, :within=>1..20, :message=>"must be between 1 and 20 characters" validates_length_of :subcode, :within=>1..36, :message=>"must be between 1 and 36 characters" belongs_to :team belongs_to :pname has_many :note end Except from scenario_controller.rb ... def new @teams = Team.find_all @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} @p_codes = Pname.find_all @p_array = [["Select Code",""]]+ @p_codes.map {|u| [u.p_code+" "+u.p_name, u.id]} @scenario = Scenario.new rescue end def create @teams = Team.find_all @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} @p_codes = Pname.find_all @p_array = [["Select Code",""]] + @p_codes.map {|u| [u.p_code+" "+u.p_name, u.id]} @scenario = Scenario.new(@params[:scenario]) if @scenario.save flash[''notice''] = ''Scenario was successfully created.'' redirect_to :action => ''list'' else render_action ''new'' end end def edit @triage = Triage.find(@params[:id]) @notes = @triage.note @teams = Team.find_all @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} @p_codes = Pname.find_all @p_array = [["Select Code",""]] + @p_codes.map {|u| [u.p_code+" "+u.p_name, u.id]} end def eval @scenario = Scenario.find(@params[:case_number]) if @scenario redirect_to :action => ''edit'', :id => @scenario.id else flash[''notice''] = ''Unable to find Scenario, creating a new one.'' redirect_to :action => ''new'' end end ... Excerpt from index.rhtml is: ... <%= start_form_tag :action => ''eval'' %> <p><label for="scenario_case_number">Case number</label><br/> <%= text_field ''scenario'', ''case_number'' %></p> <%= submit_tag "Scenario" %> <%= end_form_tag %> ... To clarify, when I redirect to new I am getting validation errors saying the software version and subcode are still blank. I never get the chance to fill in these fields before the validation takes place. I want the result of the eval action to be a scenario=>new, fill in the fields and submit or scenario=>edit, change the fields and submit. - Michael
> def new > @teams = Team.find_all > @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} > @p_codes = Pname.find_all > @p_array = [["Select Code",""]]+ @p_codes.map {|u| [u.p_code+" >"+u.p_name, u.id]} > @scenario = Scenario.new > rescue > end > >and what is all this Team and Pname stuff? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 6/22/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote:> > > def new > > @teams = Team.find_all > > @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} > > @p_codes = Pname.find_all > > @p_array = [["Select Code",""]]+ @p_codes.map {|u| [u.p_code+" > >"+u.p_name, u.id]} > > @scenario = Scenario.new > > rescue > > end > > > > > and what is all this Team and Pname stuff?lists for selection boxes in the new and edit pages. It probably would have helped if I had inlcuded one of those .rhtml. Actually both use the same _form.rhtml. The code to build those list I have to propogate everywhere to prevent erorrs on my pages but I know that this is a bad coding practice, I tried moving the code to a method called populate, butthat didn''t work. Except of scenario_views/_form.rhtml <!--[form:scenario]--> <p><label for="scenario_case_number">CR number</label><br/> <%= text_field ''scenario'', ''case_number'' %></p> <p><label for="scenario_sw_version">SW version</label><br/> <%= text_field ''scenario'', ''sw_version'' %></p> <p><label for="scenario_p_code">Code</label><br/> <%= select("scenario", "p_name_id", @p_array) %> <p><label for="scenario_subcode">Subcode</label><br/> <%= text_field ''scenario'', ''subcode'' %></p> <p><label for="scenario_team_id">Team to be assigned to:</label><br/> <%= select(:scenario, :team_id, @team_array, { :include_blank => false }) %> <% unless @scenario.cr_number == '''' -%> <%= link_to ''Add Team'', :controller=>"teams", :action => ''new'', :id=>@scenario.id %> <% end -%> <p> <!--[eoform:scenario]--> - Michael
Michael King wrote:>On 6/22/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote: > > >>> def new >>> @teams = Team.find_all >>> @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} >>> @p_codes = Pname.find_all >>> @p_array = [["Select Code",""]]+ @p_codes.map {|u| [u.p_code+" >>>"+u.p_name, u.id]} >>> @scenario = Scenario.new >>> rescue >>> end >>> >>> >>> >>> >>and what is all this Team and Pname stuff? >> >> > >lists for selection boxes in the new and edit pages. It probably would >have helped if I had inlcuded one of those .rhtml. Actually both use >the same _form.rhtml. The code to build those list I have to propogate >everywhere to prevent erorrs on my pages but I know that this is a bad >coding practice, I tried moving the code to a method called populate, >butthat didn''t work. > >you should put all that in a helper that is in the application helper... unless its only used in this controller then put it in the helper for this controller...>Except of scenario_views/_form.rhtml ><!--[form:scenario]--> ><p><label for="scenario_case_number">CR number</label><br/> ><%= text_field ''scenario'', ''case_number'' %></p> > ><p><label for="scenario_sw_version">SW version</label><br/> ><%= text_field ''scenario'', ''sw_version'' %></p> > ><p><label for="scenario_p_code">Code</label><br/> ><%= select("scenario", "p_name_id", @p_array) %> > ><p><label for="scenario_subcode">Subcode</label><br/> ><%= text_field ''scenario'', ''subcode'' %></p> > ><p><label for="scenario_team_id">Team to be assigned to:</label><br/> ><%= select(:scenario, :team_id, @team_array, { :include_blank => false }) %> > >well looking at the rails api... select takes strings are the first two args not symbols... which you have in first but not this one><% unless @scenario.cr_number == '''' -%> ><%= link_to ''Add Team'', :controller=>"teams", :action => ''new'', >:id=>@scenario.id %> ><% end -%> ><p> >and what exactly is the error you are getting? that would really help... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 6/22/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote:> Michael King wrote: > > >On 6/22/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > >>> def new > >>> @teams = Team.find_all > >>> @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} > >>> @p_codes = Pname.find_all > >>> @p_array = [["Select Code",""]]+ @p_codes.map {|u| [u.p_code+" > >>>"+u.p_name, u.id]} > >>> @scenario = Scenario.new > >>> rescue > >>> end > >>> > >>> > >>> > >>> > >>and what is all this Team and Pname stuff? > >> > >> > > > >lists for selection boxes in the new and edit pages. It probably would > >have helped if I had inlcuded one of those .rhtml. Actually both use > >the same _form.rhtml. The code to build those list I have to propogate > >everywhere to prevent erorrs on my pages but I know that this is a bad > >coding practice, I tried moving the code to a method called populate, > >butthat didn''t work. > > > > > you should put all that in a helper that is in the application helper... > unless its only used in this controller then put it in the helper > for this controller...I''ll try moving that code to a helper...> >Except of scenario_views/_form.rhtml > ><!--[form:scenario]--> > ><p><label for="scenario_case_number">CR number</label><br/> > ><%= text_field ''scenario'', ''case_number'' %></p> > > > ><p><label for="scenario_sw_version">SW version</label><br/> > ><%= text_field ''scenario'', ''sw_version'' %></p> > > > ><p><label for="scenario_p_code">Code</label><br/> > ><%= select("scenario", "p_name_id", @p_array) %> > > > ><p><label for="scenario_subcode">Subcode</label><br/> > ><%= text_field ''scenario'', ''subcode'' %></p> > > > ><p><label for="scenario_team_id">Team to be assigned to:</label><br/> > ><%= select(:scenario, :team_id, @team_array, { :include_blank => false }) %> > > > > > well looking at the rails api... select takes strings are the first two > args not symbols... > which you have in first but not this one > > ><% unless @scenario.cr_number == '''' -%> > ><%= link_to ''Add Team'', :controller=>"teams", :action => ''new'', > >:id=>@scenario.id %> > ><% end -%> > ><p> > > > and what exactly is the error you are getting? > > that would really help... >I am starting at scenario=>index/index.rhtml. I enter a case number and submit. That should be routed to scenario=>eval, which should either start a new scenario if a scenario with that case number doesn''t exist or edit the existing scenario. What I am getting is the scenario=>create displayed with my SW version and subcode fields highlighted because they didn''t pass validation. - Michael
Michael King wrote:>On 6/22/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote: > > >>Michael King wrote: >> >> >> >>>On 6/22/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote: >>> >>> >>> >>> >>>>>def new >>>>> @teams = Team.find_all >>>>> @team_array = [["Select Team",""]] + @teams.map {|u| [u.teamname , u.id]} >>>>> @p_codes = Pname.find_all >>>>> @p_array = [["Select Code",""]]+ @p_codes.map {|u| [u.p_code+" >>>>>"+u.p_name, u.id]} >>>>> @scenario = Scenario.new >>>>> rescue >>>>>end >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>and what is all this Team and Pname stuff? >>>> >>>> >>>> >>>> >>>lists for selection boxes in the new and edit pages. It probably would >>>have helped if I had inlcuded one of those .rhtml. Actually both use >>>the same _form.rhtml. The code to build those list I have to propogate >>>everywhere to prevent erorrs on my pages but I know that this is a bad >>>coding practice, I tried moving the code to a method called populate, >>>butthat didn''t work. >>> >>> >>> >>> >>you should put all that in a helper that is in the application helper... >>unless its only used in this controller then put it in the helper >>for this controller... >> >> > >I''ll try moving that code to a helper... > > > >>>Except of scenario_views/_form.rhtml >>><!--[form:scenario]--> >>><p><label for="scenario_case_number">CR number</label><br/> >>><%= text_field ''scenario'', ''case_number'' %></p> >>> >>><p><label for="scenario_sw_version">SW version</label><br/> >>><%= text_field ''scenario'', ''sw_version'' %></p> >>> >>><p><label for="scenario_p_code">Code</label><br/> >>><%= select("scenario", "p_name_id", @p_array) %> >>> >>><p><label for="scenario_subcode">Subcode</label><br/> >>><%= text_field ''scenario'', ''subcode'' %></p> >>> >>><p><label for="scenario_team_id">Team to be assigned to:</label><br/> >>><%= select(:scenario, :team_id, @team_array, { :include_blank => false }) %> >>> >>> >>> >>> >>well looking at the rails api... select takes strings are the first two >>args not symbols... >>which you have in first but not this one >> >> >> >>><% unless @scenario.cr_number == '''' -%> >>><%= link_to ''Add Team'', :controller=>"teams", :action => ''new'', >>>:id=>@scenario.id %> >>><% end -%> >>><p> >>> >>> >>> >>and what exactly is the error you are getting? >> >>that would really help... >> >> >> > >I am starting at scenario=>index/index.rhtml. I enter a case number >and submit. That should be routed to scenario=>eval, which should >either start a new scenario if a scenario with that case number >doesn''t exist or edit the existing scenario. What I am getting is the >scenario=>create displayed with my SW version and subcode fields >highlighted because they didn''t pass validation. > >change the method to something other than eval... just due to the fact that it has meaning in ruby might be your problem... ive run into that from time to time and while you are at it you might want to consider things like why the edit method in the scenario controller wont actually allow editing... ( you need a scenario to edit after all ) another suggestion... dont worry about your selects for now... get it working without them... then add them in... your problems are really getting buried behing a bunch of unused code that isnt conceived of properly... i''d suggest spending some time reading the documentation and apis... you might have to dig a bit but you will find a lot of useful information... i spent my first two weeks with rails trying really small little things and exploring the api and really learning different way to do things right... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails