2017-07-20 2 views
0

Ich habe eine Ansicht, die ich durch einen Parameter mit dem Namen provider durchlaufen und eine Gefahr Schaltfläche oder Success Button basierend darauf ausgeben soll.Rails - Wenn es sich sonst für jede Iteration wiederholt

Das unmittelbare Problem ist offensichtlich - Es wird beide Fälle ausgelöst, per Authentifizierung. Ich arbeite daran, es zu beheben, indem ich es in elsif ändere, aber wirklich scheint das EXTREM redundant. Ich versuche herauszufinden, wie ich es durchlaufen kann, und gebe den Button 'per' aus, aber das Problem ist, wenn jemand kein Facebook hat, ich habe keine Ahnung, wie man das "Sync mit Facebook" ausgibt? Taste. Meine Idee wäre momentan der Hardcode in den else aber ich würde das für jeden Provider machen.

Willkommen jede Eingabe, wie ich damit besser umgehen kann.

Überschuss Info: provider ist ein einfacher String - ich das Modell ändern kann, so weiß er, dass oauth = facebook, twitter, and steam wenn, dass es hilft zu lösen, aber ich bin nicht sicher, wie

Antwort

1

So zu lösen, werde ich Gerry Idee von leihen Verwenden eines Helfers, was wahrscheinlich eine bessere Vorgehensweise beim DRYen des Codes ist.

*your_controller.rb* 

def action_on_your_controller 
    @user = User.find(params[:id]) # I guess? 
end 

*your_helper.rb* 

def build_authentication_hash(user_authentications) 
    # build the original hash, where all authentications are non-existing, in this case false, you could extend it to accommodate any number of providers 
    authentications = { 
    facebook: false, 
    twitter: false, 
    steam: false 
    } 
    user_authentications.each do |auth| 
    # iterate through all the user authentications, auth is the authentication record/object, so basically what happens is, each auth will substitute its own key on the authentications hash from false to itself, but if the authentication doesn't exist, it's not iterated through and remains false, allowing us to use that on the template logic 
    authentications[auth.provider.to_sym] = auth 
    end 
    return authentications #here return the full hash, which will allow us to iterate 
end 

# here you call the helper method, passing it the collection of authentications of the user. 
# This helper method will return the authentications hash, which you can iterate on, 
# where `p` is the `key` (facebook, steam, etc) and `v` will be the `authentication` record itself, it there was one, or `false`. 
# Since we built the hash that way it means you have access to the provider name as `p`, 
# but also to all fields of the provider record, by using `v.attribute`, such as `v.id`, etc. 
# Since all providers are present on the hash you can safely use the `if` branching to decide what to show relative to that provider. 
# Because in case the user doesn't have an authentication for twitter, the hash will have kept the value of `false` 
# So `if v` will evaluate to `false`. 
# you usually write |k,v| and not |p,v|, being k(ey), v(alue), referring to the hash structure. I wrote p because of p(rovider) 

<%- build_authentication_hash(@user.authentications).each do |p,v| %> 
    <div id="#{p}-section"> 
    <%- if v %> 
    <%= "You have #{p.capitalize}. Do you want to delete it?" %> 
    <%= button_to 'Delete Auth', deauth_url(v.id), :class => "btn btn-danger" %> 
    <%- else %> 
    <span><%= "You don't have #{p.capitalize}. Want to add it?" %></span> 
    <%= link_to "Sync with #{p.capitalize}!", user_twitter_omniauth_authorize_url, :class =>"btn btn-success" %> 
    <%- end %> 
    </div> 
<%- end %> 
+0

Die Zweiter würde immer Twitter erzeugen, und wie nennt man das die "was auch immer" -Funktion? – DNorthrup

+0

@DNorthrup, 'was auch immer' ist deine Controller-Aktion, da du nicht auf die Frage geschrieben hast, dachte ich' was auch immer' passt gut. Der zweite "else" ist problematisch, da es nur einen gibt. WENN du meinst, dass die URL auf twitter zeigt, liegt das daran, dass alle URLs in deinem Beispiel auf twitter verweisen. Du müsstest natürlich 'user_twitter_omniahuth_authorize_url' auf die Routen für die anderen Objekte setzen, wahrscheinlich sind sie so etwas wie'/auth /: provider 'so könnten Sie'/auth/# {p} 'von' tun, wenn Sie auf Weghelfern bestehen, 'send (" user _ # {p} _omniauth_authorize_url ")' –

+0

eigentlich @DNorthrup Entschuldigung, es wurde tatsächlich etwas anderes gedruckt nach, weil der Anfangsblock '<% - @ authentications.each do | p, v | %> 'wurde als' <% = 'anstelle von' <% - 'geschrieben, da das Zeichen' = 'den Block selbst druckt. Mit '<% ​​-' sollte es in Ordnung sein. –

1

werde ich eine Hilfsmethode verwenden, um die Ansicht zu bereinigen, wie folgt aus:

users_helper.rb:

def check_provider(provider, auths) 
    auth = auths.find { |auth| auth.provider == provider } 

    if auth.present? 
    capture do 
     concat "You have #{provider.capitalize}. Do you want to delete it?" 
     concat button_to("Delete Auth", "#{auth.id}", class: "btn btn-danger") 
    end 
    else 
    capture do 
     concat content_tag(:span, "You don't have #{provider.capitalize}. Want to add it?") 
     concat link_to("Sync with #{provider.capitalize}!", "twitter", class: "btn btn-success") 
    end 
    end 
end 

view.html.erb:

<% %w(twitter facebook steam).each do |provider| %> 
    <%= check_provider(provider, @auths)%> 
<% end %> 
+0

Ich bekomme einen Fehler auf 'PROVIDERS =% w' - Konstante Fehler. Ich habe etwas recherchiert und es scheint, dass es ein Problem damit ist, keine Schnur zu sein? – DNorthrup

+0

@DNorthrup Ich habe die Antwort aktualisiert, um zu bekommen, was ich verstehe, nach dem Sie suchen (ich habe zuerst die falsche Idee). Hör zu. – Gerry