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.
ruby-on-rails ruby
add a comment |
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.
ruby-on-rails ruby
1
Are you surecount_vote
is being called? You can shortencount_vote
and make it safer withroster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path
Bothfind
andsave!
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
add a comment |
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.
ruby-on-rails ruby
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
ruby-on-rails ruby
edited Nov 10 at 20:26
asked Nov 10 at 20:11
Sohel
195
195
1
Are you surecount_vote
is being called? You can shortencount_vote
and make it safer withroster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path
Bothfind
andsave!
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
add a comment |
1
Are you surecount_vote
is being called? You can shortencount_vote
and make it safer withroster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path
Bothfind
andsave!
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 11 at 1:49
Schwern
87.6k16100227
87.6k16100227
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Are you sure
count_vote
is being called? You can shortencount_vote
and make it safer withroster = Roster.find(params[:id]); roster.vote += 1; roster.save!; redirect_to rosters_path
Bothfind
andsave!
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