Ajax form partials in Rails for multiple Models -


I have AJAX code (taken :) which is present in many views (New .html.erb and edit html.erb mostly). To avoid code duplication, I pretend to use partial:

App / View / Cities / _state_city_form.html. ARB

  & lt; P & gt; State: <% = select (: state,: state_name, city.all (: select => 'DISTINCT (state)' ,: order = & gt ;: state) .collect {| c | [c. State.titleize, C.state]}, {: include_blank = & gt; true})%> & Lt;% = inspection_field 'state_state_name' ,: url = & gt; {: Action = & gt; : By_state ,: Admin = & gt; : Cities} ,: Update = & gt; 'Cities_list' ,:: with = & gt; "'State =' + $ ('# state_state_name'). Val ()"% " & Lt; / P & gt; City: & lt; Div id = "cities_list" & gt; & Lt;% = select (modelname.to_sym ,: city_id, [], {: include_blank = & gt; true})%> & Lt; / Div & gt; & Lt; P & gt; & Lt; / P & gt;  

I have Ajax callback content:

App / View / Cities / By_State.html. ARB

  & lt;% = select (/ * PROBLEM_HERE * /,: city_id, @categoriesCollect {| c | [c.name, c.id]}, {: Include_blank = & gt; true})%>  

and My Cities Controller:

App / Controller / Cities_controller RB

  Class Cities Controller & lt; ApplicationController def by_state @ cities = City.find_all_by_state (Param [[state]) render: layout = & gt;  

app / views / user / new.html.erb

/ strong>

  .. & lt;% = Render: partial = & gt; 'Cities / condition_city_form' ,: local = & gt; {: ModelName = & gt; : User}%> ...  

App / Views / Employees / new.html. ARB

  ...  ...  

Here I can pass: partly to Users or : Employee , but how do I get them in Ajax? Can I pass the callback?

You can try to pass the model name partially as a string and its use By changing the symbol can To_sym.

Hope it helps.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -