0

Here is my issue: I am setting up internationalization on my site (so we can have multiple translations of the text on the pages). I have followed a rails cast to set up a page that can manage the translations instead of me manually having to edit every yml file.

I have set everything up and can create entries fine, I am trying to add the ability to delete an entry and I have hit a wall. I can't seem to set up the link correctly to delete the entry from redis. The first thing that made this complicated (at least to me) is that I am not deleting an object that was created through active record (like a user etc). So instead of using the active record object to construct the url for the link_to or form_for I have to construct it manually.

From what I have read so far I have to put the link in a form (and set to post since we are modifying the redis db). So I have been trying to create the correct syntax in the form for tag to direct to the action I have set up in the controller.

Controller:

class InternationalizationTranslationsController < ApplicationController
  def index
    @translations = I18n.backend.store
  end

  def create
      I18n.backend.store_translations(params[:locale], {params[:key] => params[:value]}, :escape =>false)
      redirect_to internationalization_translations_url, :notice => "Added translation"
  end

  def destroy
    puts "Key is:  #{params[:key]}"
    I18n.backend.delete(params[:key])
    redirect_to internationalization_translations_url, :notice => "Removed translation"
  end
end

View:

<%= form_tag internationalization_translations_path do %>
  <p>
    <%= label_tag :locale %>
    <%= text_field_tag :locale %>
  </p>
  <p>
    <%= label_tag :key %>
    <%= text_field_tag :key %>
  </p>
  <p>
    <%= label_tag :value %>
    <%= text_field_tag :value %>
  </p>
  <p><%= submit_tag "Submit" %></p>
<% end %>
</div>
  <div class="grid_7 top_padding">
<table class="trans_table">
<% @translations.keys.each_with_index do |key, i| %>
<tr class="<%= i%2 == 0 ? "even" : "odd" %>">
  <td><%= key %></td>
  <td><%= @translations[key] %></td>

Then I played with form_for and form_tag looking at the documentation (form helpers and form tag docs) eventually ending with these, that still do not work:

    <%= form_tag(controller: "internationalization_translations", action: "destroy", method: "post", key: key) %>
    <%= submit "Delete" %>
    <% end %>

and now

    <%= form_tag(internationalization_translations_path, action: "destroy", method: "post", key: key) do %>
    <%= submit_tag "Delete" %>
    <% end %>

I also played with the link_to for a while before coming across this post which linked to why the delete link/button should be in a form because it is editing the DB so it needed to be post instead of get. I am a little frustrated because this seems like a pretty straight forward task but I am running into some difficulties finding a clear answer regarding my particular issue, specifically the routing for this link for a redis entry and not an activerecord object.

**also since the form for the button is being created in a loop for each entry I should probably have the form named with an index so it is specific for each button?

Any insight or links would be greatly appreciated.

Thanks,

Alan

Community
  • 1
  • 1
Alan DeLonga
  • 454
  • 1
  • 10
  • 27
  • when u have the redis-key for each entry, why you not creating a new route+controller action for it? now you could send the post to the new action with the correct redis.key - and in this method u delete the key? delete_translation_path(key: your_key) – marvwhere Dec 10 '13 at 22:43
  • That is what the **Controller** code is, that is the action within my controller InternationalizationTranslationsController that is supposed to be deleting the key I am trying to pass through form_for button. In my routes I have resources :internationalization_translations which is working for the other form, that goes to the create action. For some reason I cannot figure out how to have it use the destroy action and send the key as a param[:key](as shown above from the different ways I was strying to set up the form_for link) – Alan DeLonga Dec 10 '13 at 23:11
  • remove the "key-option" from the form_tag and add a "hidden_field_tag :key, key" before/after the button and then try again. – marvwhere Dec 11 '13 at 00:25

1 Answers1

0

Ok so I ended up figuring it out resetting some things up and taking marvwhere's advice. I wanted to set it up as a link without a form, how it generated for other controllers that were manipulating active record objects. But since this was a different case making a custom action other than the default destroy action worked.

      <%= form_tag(destroy_key_internationalization_translations_path, method: :post) do %>
      <%= hidden_field_tag 'key', key %>
          <%= submit_tag "Delete" %>
      <% end %>

where I created the destroy_key action within the internationalization_translation controller.

Also the deleting of the key from redis needed to be changed. I had to use the actual Redis instance created. So instead of

I18n.backend.delete(params[:key])

in my initializer I had to set a global variable when creating the Redis instance:

TRANSLATION_STORE = Redis.new(:db => 10)

and then call the delete on that object

TRANSLATION_STORE.del(params[:key])
Alan DeLonga
  • 454
  • 1
  • 10
  • 27