Update JSon parsed vote using Ruby on Rails, possible without JQuery?











up vote
0
down vote

favorite












I am trying to make the vote total editable from a JSon parsed API. I have the following in my rosters controller:



def index
@rosters = HTTParty.get('https:api', :headers =>{'Content_Type' => 'application/json'})
@allrosters = Roster.all
@allrostershash = {}
@allrosters.each do |roster|
image_url = roster['image_url']
@allrostershash[ image_url ] = roster
end
@rosters.each do |roster|
img_url = roster['image_url']
unless @allrostershash[img_url]
Roster.create(roster)
end
end
end
def count_vote
roster_id = params[:id]
roster = Roster.find_by(roster_id)
newvote = roster.vote + 1
if roster.update({vote: newvote})
redirect_to rosters_path
end
end


Roster is the name of my class above. In my rails views I have the following:



<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(roster['id']) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>


I would like that every time someone clicks on yes.jpg, the roster['vote'] increases by 1.



Currently my routes are set up as follows:



get 'rosters', to: 'rosters#index'
patch 'rosters', to: 'rosters#count_vote'


I'm trying to accomplish this without jquery or ajax, that's why I have the if roster.update portion to redirect to rosters_path, so it basically refreshes the page upon click. Right now it isn't updating the vote total however, I'm not sure what I'm missing. I would like to do it all on a single page so if its not possible without JQuery, any guidance in right direction is appreciated.










share|improve this question




















  • 1




    Are you sure count_vote is being called? You can shorten count_vote and make it safer with roster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path Both find and save! will throw exceptions if they fail.
    – Schwern
    Nov 10 at 20:25










  • I believe the roster.save! raised the following error: Couldn't find Roster without an ID. When I try to type Roster.find(2) in the error console however, it returns me the correct value?
    – Sohel
    Nov 10 at 20:30












  • These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param?
    – Sohel
    Nov 10 at 20:39















up vote
0
down vote

favorite












I am trying to make the vote total editable from a JSon parsed API. I have the following in my rosters controller:



def index
@rosters = HTTParty.get('https:api', :headers =>{'Content_Type' => 'application/json'})
@allrosters = Roster.all
@allrostershash = {}
@allrosters.each do |roster|
image_url = roster['image_url']
@allrostershash[ image_url ] = roster
end
@rosters.each do |roster|
img_url = roster['image_url']
unless @allrostershash[img_url]
Roster.create(roster)
end
end
end
def count_vote
roster_id = params[:id]
roster = Roster.find_by(roster_id)
newvote = roster.vote + 1
if roster.update({vote: newvote})
redirect_to rosters_path
end
end


Roster is the name of my class above. In my rails views I have the following:



<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(roster['id']) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>


I would like that every time someone clicks on yes.jpg, the roster['vote'] increases by 1.



Currently my routes are set up as follows:



get 'rosters', to: 'rosters#index'
patch 'rosters', to: 'rosters#count_vote'


I'm trying to accomplish this without jquery or ajax, that's why I have the if roster.update portion to redirect to rosters_path, so it basically refreshes the page upon click. Right now it isn't updating the vote total however, I'm not sure what I'm missing. I would like to do it all on a single page so if its not possible without JQuery, any guidance in right direction is appreciated.










share|improve this question




















  • 1




    Are you sure count_vote is being called? You can shorten count_vote and make it safer with roster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path Both find and save! will throw exceptions if they fail.
    – Schwern
    Nov 10 at 20:25










  • I believe the roster.save! raised the following error: Couldn't find Roster without an ID. When I try to type Roster.find(2) in the error console however, it returns me the correct value?
    – Sohel
    Nov 10 at 20:30












  • These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param?
    – Sohel
    Nov 10 at 20:39













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to make the vote total editable from a JSon parsed API. I have the following in my rosters controller:



def index
@rosters = HTTParty.get('https:api', :headers =>{'Content_Type' => 'application/json'})
@allrosters = Roster.all
@allrostershash = {}
@allrosters.each do |roster|
image_url = roster['image_url']
@allrostershash[ image_url ] = roster
end
@rosters.each do |roster|
img_url = roster['image_url']
unless @allrostershash[img_url]
Roster.create(roster)
end
end
end
def count_vote
roster_id = params[:id]
roster = Roster.find_by(roster_id)
newvote = roster.vote + 1
if roster.update({vote: newvote})
redirect_to rosters_path
end
end


Roster is the name of my class above. In my rails views I have the following:



<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(roster['id']) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>


I would like that every time someone clicks on yes.jpg, the roster['vote'] increases by 1.



Currently my routes are set up as follows:



get 'rosters', to: 'rosters#index'
patch 'rosters', to: 'rosters#count_vote'


I'm trying to accomplish this without jquery or ajax, that's why I have the if roster.update portion to redirect to rosters_path, so it basically refreshes the page upon click. Right now it isn't updating the vote total however, I'm not sure what I'm missing. I would like to do it all on a single page so if its not possible without JQuery, any guidance in right direction is appreciated.










share|improve this question















I am trying to make the vote total editable from a JSon parsed API. I have the following in my rosters controller:



def index
@rosters = HTTParty.get('https:api', :headers =>{'Content_Type' => 'application/json'})
@allrosters = Roster.all
@allrostershash = {}
@allrosters.each do |roster|
image_url = roster['image_url']
@allrostershash[ image_url ] = roster
end
@rosters.each do |roster|
img_url = roster['image_url']
unless @allrostershash[img_url]
Roster.create(roster)
end
end
end
def count_vote
roster_id = params[:id]
roster = Roster.find_by(roster_id)
newvote = roster.vote + 1
if roster.update({vote: newvote})
redirect_to rosters_path
end
end


Roster is the name of my class above. In my rails views I have the following:



<% @rosters.each do |roster| %>
<div class='each'>
<%= image_tag(roster['image_url'], class: 'image') %>
<%= hidden_field_tag(roster['id']) %>
<p class='name'> <%= roster['name'] %> </p>
<p class='title'> <%= roster['title'] %> </p>
<p> <%= roster['bio'] %> </p>
<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path, method: :patch %>
<br>
<%= roster['vote'] %> People have said Yes! </p>
<br>
</div>
<% end %>


I would like that every time someone clicks on yes.jpg, the roster['vote'] increases by 1.



Currently my routes are set up as follows:



get 'rosters', to: 'rosters#index'
patch 'rosters', to: 'rosters#count_vote'


I'm trying to accomplish this without jquery or ajax, that's why I have the if roster.update portion to redirect to rosters_path, so it basically refreshes the page upon click. Right now it isn't updating the vote total however, I'm not sure what I'm missing. I would like to do it all on a single page so if its not possible without JQuery, any guidance in right direction is appreciated.







ruby-on-rails ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:26

























asked Nov 10 at 20:11









Sohel

195




195








  • 1




    Are you sure count_vote is being called? You can shorten count_vote and make it safer with roster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path Both find and save! will throw exceptions if they fail.
    – Schwern
    Nov 10 at 20:25










  • I believe the roster.save! raised the following error: Couldn't find Roster without an ID. When I try to type Roster.find(2) in the error console however, it returns me the correct value?
    – Sohel
    Nov 10 at 20:30












  • These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param?
    – Sohel
    Nov 10 at 20:39














  • 1




    Are you sure count_vote is being called? You can shorten count_vote and make it safer with roster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path Both find and save! will throw exceptions if they fail.
    – Schwern
    Nov 10 at 20:25










  • I believe the roster.save! raised the following error: Couldn't find Roster without an ID. When I try to type Roster.find(2) in the error console however, it returns me the correct value?
    – Sohel
    Nov 10 at 20:30












  • These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param?
    – Sohel
    Nov 10 at 20:39








1




1




Are you sure count_vote is being called? You can shorten count_vote and make it safer with roster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path Both find and save! will throw exceptions if they fail.
– Schwern
Nov 10 at 20:25




Are you sure count_vote is being called? You can shorten count_vote and make it safer with roster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path Both find and save! will throw exceptions if they fail.
– Schwern
Nov 10 at 20:25












I believe the roster.save! raised the following error: Couldn't find Roster without an ID. When I try to type Roster.find(2) in the error console however, it returns me the correct value?
– Sohel
Nov 10 at 20:30






I believe the roster.save! raised the following error: Couldn't find Roster without an ID. When I try to type Roster.find(2) in the error console however, it returns me the correct value?
– Sohel
Nov 10 at 20:30














These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param?
– Sohel
Nov 10 at 20:39




These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param?
– Sohel
Nov 10 at 20:39












1 Answer
1






active

oldest

votes

















up vote
0
down vote













count_vote will silently fail if it cannot find your Roster or if the update cannot be saved. Change it so it raises an exception of anything fails.



  def count_vote
roster = Roster.find(params[:id])
roster.vote += 1
roser.save!
redirect_to rosters_path
end


find will raise RecordNotFound if the Roster cannot be found. save! will raise an error if the changes cannot be saved.




These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param? – Sohel 5 hours ago




I'm not very familiar with how views work, but I think as in this example, I believe you need to pass the roster into rosters_path.



<%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path(roster), method: :patch %>


Similarly, if you want count_vote to redirect back to the roster you just changed...



redirect_to rosters_path(roster)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242995%2fupdate-json-parsed-vote-using-ruby-on-rails-possible-without-jquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    count_vote will silently fail if it cannot find your Roster or if the update cannot be saved. Change it so it raises an exception of anything fails.



      def count_vote
    roster = Roster.find(params[:id])
    roster.vote += 1
    roser.save!
    redirect_to rosters_path
    end


    find will raise RecordNotFound if the Roster cannot be found. save! will raise an error if the changes cannot be saved.




    These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param? – Sohel 5 hours ago




    I'm not very familiar with how views work, but I think as in this example, I believe you need to pass the roster into rosters_path.



    <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path(roster), method: :patch %>


    Similarly, if you want count_vote to redirect back to the roster you just changed...



    redirect_to rosters_path(roster)





    share|improve this answer

























      up vote
      0
      down vote













      count_vote will silently fail if it cannot find your Roster or if the update cannot be saved. Change it so it raises an exception of anything fails.



        def count_vote
      roster = Roster.find(params[:id])
      roster.vote += 1
      roser.save!
      redirect_to rosters_path
      end


      find will raise RecordNotFound if the Roster cannot be found. save! will raise an error if the changes cannot be saved.




      These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param? – Sohel 5 hours ago




      I'm not very familiar with how views work, but I think as in this example, I believe you need to pass the roster into rosters_path.



      <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path(roster), method: :patch %>


      Similarly, if you want count_vote to redirect back to the roster you just changed...



      redirect_to rosters_path(roster)





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        count_vote will silently fail if it cannot find your Roster or if the update cannot be saved. Change it so it raises an exception of anything fails.



          def count_vote
        roster = Roster.find(params[:id])
        roster.vote += 1
        roser.save!
        redirect_to rosters_path
        end


        find will raise RecordNotFound if the Roster cannot be found. save! will raise an error if the changes cannot be saved.




        These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param? – Sohel 5 hours ago




        I'm not very familiar with how views work, but I think as in this example, I believe you need to pass the roster into rosters_path.



        <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path(roster), method: :patch %>


        Similarly, if you want count_vote to redirect back to the roster you just changed...



        redirect_to rosters_path(roster)





        share|improve this answer












        count_vote will silently fail if it cannot find your Roster or if the update cannot be saved. Change it so it raises an exception of anything fails.



          def count_vote
        roster = Roster.find(params[:id])
        roster.vote += 1
        roser.save!
        redirect_to rosters_path
        end


        find will raise RecordNotFound if the Roster cannot be found. save! will raise an error if the changes cannot be saved.




        These are the only params currently for some reason ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"qbORnCLNnI9P1zUZ02VEP3qJMwYOGa5sGw6KblPFj99mvjwZQj9VnDQ2e+6ZStJi3PJZ3MidSMsdoWlwOgBN9w==", "controller"=>"rosters", "action"=>"count_vote"} permitted: false> how would I add an id param? – Sohel 5 hours ago




        I'm not very familiar with how views work, but I think as in this example, I believe you need to pass the roster into rosters_path.



        <%= link_to image_tag('yes.jpg', class: 'yes'), rosters_path(roster), method: :patch %>


        Similarly, if you want count_vote to redirect back to the roster you just changed...



        redirect_to rosters_path(roster)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 1:49









        Schwern

        87.6k16100227




        87.6k16100227






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242995%2fupdate-json-parsed-vote-using-ruby-on-rails-possible-without-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma