Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using autocomplete on different model/controller #35

Open
Sajonara opened this issue Dec 16, 2015 · 1 comment
Open

Using autocomplete on different model/controller #35

Sajonara opened this issue Dec 16, 2015 · 1 comment

Comments

@Sajonara
Copy link

I am using rails form helper with one model's data on several controllers/views. Like let's say I have a comments model and use comments at many occasions.

In this particular case I am using several forms in an admin namespace. With normal select fields I have a working solution but I don't know how to access the correct data. As I don't have too much Rails experience right now I am stuck. That's why maybe someone here can help me out.

In my example I am using a form for a @gallery on a foreign admins controller. In the form right now I let editors chose which @game belongs to a @gallery. Like so:

<%= form_for(@gallery) do |f| %>
...
<%= f.select :game_id, options_for_select(@games.map {|g| [g.name, g.id]}) %>
...
<% end %>

If I use <%= f.autocomplete_field :name, autocomplete_name_games_path %> instead it tells me, that :name is not available with my @gallery object, which is true, but I thought I could use the autocomplete of the games controller at this place, too.

Kind regards

@ghost
Copy link

ghost commented Nov 25, 2016

You should be able to. For example, I have a user form that has an address field for "state". But I have created only a model that interacts with a table of states in the database but I can use those states in any controller using this autocomplete function. So I setup autocomplete in the user controller to look at the states model.

In your example, you would need to setup the autocomplete in the foreign controller but where you may be confused is that you don't need that autocomplete_field to be :name in order to use the name data for the autocomplete. You would then have something that looks like so:

In your foreign controller:
autocomplete :game, :name

Then in the form:
<%= f.autocomplete_field :game_id, autocomplete_game_name_foreigncontrollernames_path %>

The field would autocomplete on the game names but then you will run into the same issue I'm having where the reality is you need the form to save the game_id and not the name and I'm still working on figuring that out since I want the autocomplete to show the full state name but save the two letter abbreviation instead. :)

I hope what I said makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant