michaelf
michaelf

Reputation: 11

Assign Rails variable through an on-click event?

I'm building a website involving recipes and I have some content being loaded through an iframe from another site, I would like to remove it's headers/footers and just really manipulate a specific part of the foreign HTML so i can just display that. I was first thinking using jquery or angularjs to solve the problem, but I started thinking of if I could pass the url from an onclick (I already have that function sorted out below):

AngularJS:

    $scope.currentRecipeUrl = "";
    $scope.trustSrc = function(SrcToBeTrusted) {
            return $sce.trustAsResourceUrl(SrcToBeTrusted);
        };

    $scope.changeWeeklyRecipe = function(recipeUrl) {
        $scope.currentRecipeUrl = $scope.trustSrc(recipeUrl);
    };

HTML.ERB:

    <iframe id="displayedRecipe" src="{{ currentRecipeUrl }}"

to my Rails controller so I could just inject the specific part of the html that I wanted into the view.

Can anybody think of a way I might be able to accomplish that?

EDIT: I forgot to mention that the changeWeeklyRecipe function is being called on the on-click event.

Upvotes: 0

Views: 195

Answers (1)

jdgray
jdgray

Reputation: 1983

Consider using $http with your onClick function to pass data to your controller.

Here is an example of what you could do:

# Use $http to send data to your rails controller and use the appropriate callbacks

$http.post('/foo/create', {bar_params: {text: 'Hello World'}})

And in your rails controller:

class FooController < ApplicationController
  def create
    @variable = example_params
  end

  private

  def example_params
    params.require(:bar_params).permit(:text)
  end
end

Hope that gives you an idea of how to accomplish what you are looking for.

Upvotes: 1

Related Questions