I am struggling to troubleshoot why this ajax call on a standard Destroy method won't work. From looking at my HTML source, I think it might be a problem with either how I'm creating divs using div_for OR it has something to do with my js. I'm at a loss for troubleshooting javascript.
My view:
<% #quizzes.each do |quiz| %>
<%= div_for quiz do %>
<tr>
<td>
<%= link_to quiz.name, quiz_review_path(quiz.id) %>
</td>
<% if quiz.finished? %>
<td>
<%= link_to "Results", quiz_results_path(quiz) %>
</td>
<% elsif quiz.questions.first != nil %>
<td>
<%= link_to "Take quiz", question_answer_path(question_id: quiz.questions.first.id) %>
</td>
<% else %>
<td>
<%= link_to "Broken, delete!", nil %>
</td>
<% end %>
<td>
<%= link_to "Delete", quiz_path(quiz), method: 'delete', remote: true %>
</td>
</tr>
<% end %>
The controller:
def destroy
#quiz = Quiz.find(params[:id])
#quiz.destroy
respond_to do |format|
format.html { redirect_to quizzes_path }
format.js
end
end
My views/quizzes/destroy.js
$(document).ready(function() {
$('#<%= dom_id(#quiz) %>').fadeOut();
})
From looking at the Rails s logs, I can see that the delete request comes in and is processed by the js, so my best guess is it's either an issue with div_for and dom_id OR my js is bad (highly likely).
Fixed it: trying to wrap in a with div_for creates invalid html. Removed the tables and now the ajax works fine.
Related
In my app, I have a view that shows a number of products. If a product has an active "deal" on it, a link to that deal is given for that deal that links to the show action for that deal, as below.
<tr>
<% #item.inventory_items.each do |p| %>
<td><%= link_to image_tag("#{p.vendor.image_url}"), vendor_path(p.vendor) %></td>
<td class="price">$<%= p.price %></td>
<td>
<% if Deal.where(:product_code => p.product_code, :vendor_id => p.vendor_id).exists? %>
<%= link_to "See It!", deal_path(Deal.where(:product_code => p.product_code, :vendor_id => p.vendor_id)) %>
<% else %>
<% end %>
</td>
<td>
......
</td>
</tr>
The problem I'm having is that my show action for Deal is looking in params for a deal id. This deal id isn't really in params, so how can I tell this Deal show action which deal I'm interested in? Thanks in advance! As an aside, my above code feels really verbose and very un-rails-like in my if block. If you have any suggestions for it, any advice is much appreciated.
deal/show.html.erb
<%= link_to image_tag("#{#deal.vendor.image}") %>
<%= #deal.vendor.address %>
<%= image_tag(#deal.image) %>
Deal show action:
def show
#deal = Deal.find(params[:id])
end
Try
<%= link_to "See It!", deal_path(Deal.where(:product_code => p.product_code, :vendor_id => p.vendor_id).first) %>
Your code is definitely not very rails-like.
What you should be able to do is something like this:
<tr>
<% #item.inventory_items.each do |p| %>
<td><%= link_to image_tag("#{p.vendor.image_url}"), vendor_path(p.vendor) %></td>
<td class="price">$<%= p.price %></td>
<td>
<% if p.deal.exists? %>
<%= link_to "See It!", deal_path(:id => p.deal.id)) %>
<% else %>
<% end %>
</td>
<td>
......
</td>
</tr>
Note that you can pass parameters along in a link_to. See this question - link_to send parameters along with the url and grab them on target page
If you're models are setup correctly, you shouldn't have to be doing queries in your views.
I'm trying to use radio buttons to make administrators approve users. So in the page I have a list for all the users waiting for approval (a true/false value in user model). After the administrator has set "yes" or has left "no" on the radio button field of all of them, he should press the unique submit button.
I'm trying to figure out how to pass to the controller all of the users for the approval update.`
<%= form_tag(approve_url, :html => { class: "form-inline"} )do |f| %>
<table>
<% #tobeapproved.each do |user| %>
<tr >
<td>
<%= user.name %>
</td>
<td>
<%= user.surname %>
</td>
<td>
<%= user.email %>
</td>
<td>
<%= label :approved %>
<%= radio_button_tag "approved[#{user.id}]", "true" %> Approved
<%= radio_button_tag "approved[#{user.id}]", "false" %> Not approved
</td>
</tr>
<% end %>
</table>
<%= f.submit %>
<% end %>
On submit I should get "approved[id]"=>{"true/false"} right? I'm very new on all of this.
Thank you
In the end I figured out how to do such a thing.
<%= form_tag(approve_url, :html => { class: "form-inline"}) do |f| %>
<table>
<% #tobeapproved.each do |user| %>
<tr >
<td>
<%= user.name %>
</td>
<td>
<%= user.surname %>
</td>
<td>
<%= user.email %>
</td>
<td>
<%= radio_button_tag "approved[#{user.id}]", "true" %> Approved
<%= radio_button_tag "approved[#{user.id}]", "false", true %> Not approved
</td>
</tr>
<% end %>
</table>
<%= submit_tag 'Confirm'%>
It gets to me something like this in params:
"approved"=>{"27"=>"false", "26"=>"false", "25"=>"true"}
And in my controller:
def update
#users = params[:approved]
logger.debug "The object is #{#users}"
respond_to do |format|
#users.each_pair do |key, value|
user = User.find(key)
user.update_attribute(:approved, value)
end
format.html { redirect_to :action => :list, notice: 'Users updated' }
end
But I imagine this code kind of sucks. Anybody knows how to better it?
I've checked out a lot of questions for putting multiple objects on one form, but they seem to be out of date.
I have a bunch of user objects:
def index
#users = User.all
#user = current_user
end
that I need to edit in a form. They all have roles which will be edited on the form. The form is rendered in a partial, and nothing shows up, just 'admin form' plaintext.
users/_admin.html.erb
admin form
<% form_for "user[]", :url => users_path do |f| %>
<ul>
<li class="layout">
<div class="header"><h2>Users</h2></div>
<table>
<thead>
...
</thead>
<tbody>
<% #users.each do |user| %>
<% puts "USER #{user}" %>
<tr>
<td><%= f.check_box(:editor) %></td>
<td><%= f.check_box(:admin) %></td>
<td><%= user.first_name %> <%= user.last_name %></td>
<td><%= user.email %></td>
</tr>
<% end %>
</tbody>
</table>
</li>
</ul>
<%= submit_tag "Save"%>
<% end %>
Just the plaintext is rendered, but no form. Any ideas on how to fix it? I've tried the suggestions in these posts: one two three, but they're out of date.
Thank you.
You need to use <%= .. %> in Rails 3.1:
<%= form_for "user[]", :url => users_path do |f| %>
Hi I worked through Michael Hartl's RAILSTUTORIAL book and I have a question about how he builds the user's show page.
The page is supposed to list all the posts a user has made.
UsersController
def show
#user = User.find(params[:id])
#posts = #user.posts.paginate(:per_page => "10",:page => params[:page])
#title = #user.name
end
users/show.html.erb
<table class="profile" summary="Profile information">
<tr>
<td class="main">
<h1><%= #user.name %></h1>
<%= render 'follow_form' if user_signed_in? %>
<% unless #user.posts.empty? %>
<table class="posts" summary="User posts">
<%= render #posts %> # this goes to posts/_post and sends the object as post
# that makes the _post view use a local variable correct?
</table> # is there a way to do with with an #post variable?
<%= will_paginate #posts %>
<% end %>
</td>
<td class="sidebar round">
<%= link_to avatar_for(#user), #user.avatar.url %><br />
<strong>Name</strong> <%= #user.name %><br />
<strong>URL</strong> <%= link_to user_path(#user), user_path(#user) %>
<strong>Posts</strong> <%= #user.posts.count %>
<%= render 'shared/stats' %>
</td>
</tr>
</table>
posts/_post.html.erb
<tr>
<td class="post">
<span class="title"><strong><%= link_to post.title, post %></strong></span><br />
<span class="timestamp">
Posted <%= time_ago_in_words(post.created_at) %> ago. </span>
Likers<span id="likers"><br />
</span>
</td>
<% if current_user?(post.user)%>
<td>
<%= link_to "delete", post, :method => :delete,
:confirm => "You sure?",
:title => post.content %>
</td>
<%end%>
</tr>
I need to render a partial in the users view that uses the post object, but it asks for it as #post and since no #post has been defined in the show action of the user's controller I get a nil error.
It seem odd to me to go from the user's controller to the posts view and use a local variable, which if I understand local variables correctly can't be used outsider that view. Is there a way to assign the value of post in that view to #post in the users view?
Thanks for the help
You need to use a local variable in the partial, and assign it in the locals hash. This line is a shortcut for iterating through the array and rendering the partial. I'm not sure if this works anymore in Rails 3:
<%= render #posts %>
The way I would do it:
<% #posts.each do |post| %>
<%= render 'posts/post', :post => post %>
<% end %>
The slightly older way of rendering partials:
<% #posts.each do |post| %>
<%= render :partial => 'posts/post', :locals => {:post => post} %>
<% end %>
I have a view with some embedded ruby in it... i want to insert a cell <td></td> into it, but when i do that it gives several error messages? This is because i concatenate a text field and a submit button into the embedded ruby.
This is my code:
<table>
<% for answer in #question.answers %>
<tr>
<!-- this displays all the possible answers -->
<td>
<%= answer.text %>
</td>
<% if current_user.can_vote_on? (#question) %> <!-- if a current user has not yet voted.. -->
<td> <%= form_tag('/vote', :method => "post") do
hidden_field_tag('vote[answer_id]', answer.id) +
submit_tag("Vote")
end %> <!-- vote button.. -->
<% end %>
</td>
</tr>
<% end %>
<% if current_user.can_vote_on? (#question) %> <!-- this shows a answer text field -->
<tr>
<td>
<%= form_tag('/vote/new_answer', :method => "post") do
hidden_field_tag('answer[question_id]', #question.id) +
hidden_field_tag('answer[user_id]', current_user.id) +
text_field_tag('answer[text]') + <!-- in here i want an extra </td><td> tag -->
submit_tag('Vote')
end %>
</td>
</tr>
<% end %>
My question is: how can i exit the embedded ruby and at the same time staying in the concatenated string...? I want to add the </td> and the <td> after the text_field_tag('answer[text]')
I tried this:
<td>
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag('answer[question_id]', #question.id) %>
<%= hidden_field_tag('answer[user_id]', current_user.id) %>
<%= text_field_tag('answer[text]') %>
</td>
<td>
<%= submit_tag('Vote') %>
<% end %>
</td>
And it works!
Thijs
Simple answer: It's not possible.
I suggest you try a different approach such as using divs inside your td elements. If I were you I wouldn't concatinate the strings together.
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag(answer[question_id], #question.id %>
... so on ...
<div class="position_it_somewhere_with_this_class"><%= submit_tag("vote") %></div>
<% end %>
You don't concatenate tags!
Also you don't use divs in table rows. put the classes in your tds
...
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag('answer[question_id]', #question.id) %>
<%= hidden_field_tag('answer[user_id]', current_user.id) %>
<%= text_field_tag('answer[text]') %>
<%= submit_tag('Vote') %>
<% end %>
</td>
</tr>
..