Rails 3 select checkboxes - pass list to controller - ruby-on-rails

In a Rails 3 project, I have an index list of costprojects.
I have a controller action called `viewprojects'.
I would like to add a checkbox column to the index list. Then let the user select which projects should be included in the pdf.
index:
<% #costprojects.each do |costproject| %>
<tr>
<td><%= check_box_tag "costproject_ids[]", costproject.id %></td>
...
<%= link_to 'PDF Selected', costprojects_viewprojects_path(:format => "pdf",:costproject_ids => costproject_ids[] ), :class => 'btn btn-success', :target => "_blank" %>
Controller:
def viewprojects
#costprojects = params[:costproject_ids]
respond_to do |format|
format.html
format.pdf do
render :pdf => "costprojects.pdf",
:show_as_html => params[:debug].present?,
:page_size => 'letter'
end
end
end
I'm getting:
undefined local variable or method `costproject_ids'
Update1:
Tried:
def viewprojects
#costprojects = params[:costproject_ids[]]
...
<%= link_to 'PDF Selected', costprojects_viewprojects_path(:format => "pdf",:costproject_ids[] => costproject_ids[] ), :class => 'btn btn-success', :target => "_blank" %>
I get "wrong number of arguments (0 for 1..2)"
Do I need to add form_tag and submit_tag?
Thanks for the help!

As Legendary mentioned in the comments you need to wrap this in a form. A link_to isn't going to transmit the value of the selected ids unless its contained in its query parameters, which it can't know at render time.
Try something more like this:
<%= form_tag costprojects_viewprojects_path(format: "pdf") do %>
<% #costprojects.each do |costproject| %>
<tr>
<td><%= check_box_tag "costproject_ids[]", costproject.id %>
</td>
<% end %>
...
<%= submit_tag "PDF Selected", class: "btn btn-success" %>
<% end %>
You should then be able to access the array of cost_project_ids simply with params[:cost_project_ids] in the controller, which it seems you're already doing. Note that this will send an HTTP POST, rather than a GET, so ensure your routes are correctly setup for that verb.

Related

Using ajax to render partial

In my rails app I have a model Song. On one of the user's profile pages user_music_path(#user) I'm rendering all of their songs.
People can also like these songs, which is set up and working, I just want to use AJAX instead of page refreshes. The problem I'm having is when using ajax, I get the error undefined local variable or method "song" when I'm clicking the link "like".
Here is some of my code:
User's Music Page views/users/music.html.erb
...
<ul class="playlist list-group">
<%= render #songs %>
</ul>
views/songs/_song.html.erb
<li class="list-group-item">
<p class="pull-right">
<%= render :partial => 'songs/like_button', :locals => {:song => song} %>
<%= render :partial => 'songs/likes', :locals => {:song => song} %>
</p>
</li>
views/songs/_like_button.html.erb
<% if current_user.voted_on?(song) %>
<%= link_to "Unlike", unlike_song_path(song), :method => :post, :remote => true %>
<% else %>
<%= link_to "Like", like_song_path(song), :method => :post, :remote => true %>
<% end %>
views/songs/_likes.html.erb
<%= song.votes.count %>
views/songs/like.js.coffee
$("p.pull-right").html('<%= render :partial => "songs/like_button", :locals => {:song => song} %><%= render :partial => "songs/likes", :locals => {:song => song} %>');
controllers/songs_controller.rb
def like
begin
#vote = current_user.vote_for(#song = Song.find(params[:id]))
#vote.save
respond_with #song.user, :location => user_music_path(#song.user)
rescue ActiveRecord::RecordInvalid
redirect_to #song
end
end
And as mentioned earlier, the :like action worked perfectly before I starting using AJAX. I've also added respond_to :html, :js to my songs_controller. And the error I get when I try to like the song is ActionView::Template::Error (undefined local variable or method "song" for #<#<Class:0x000001028de9d0>:0x00000106ecd9f0>):
Where you have
<%= song.name %>
try instead
<%= #song.name %>
And do the same with your link_to paths and your render calls.
Do rake routes and use of of them for the link, e.g. (pseudo-code) link_to 'song', song_audio_path

Better way (or correct way) to receive values in functions in controller

I'm a newbie :PP, I have a function call "Search", this function received 2 values, theme and q, after the user to choise the type and to insert the key word in field box, the user click in submit.
I can see these values in function search, but not in index, so I'm using a global variable :PP
But is ugly, there is a better way?
welcome_controller.rb
class WelcomeController < ApplicationController
before_filter :require_user
def index
#theme = $a
#value = $b
end
def search
$a = params[:theme]
$b = params[:q]
redirect_to :root
end
end
views/welcome/_search.html.erb
<%= form_tag(:controller => 'welcome', :action => 'search', :method => 'get') do %>
<div style="background:#fafafa; color:#222; padding:10px;">
<h4>Pesquisa Avançada</h4>
<table>
<tr>
<td>
<input type="radio" name="radiog_dark" class="css-checkbox">
<% [ 'Analista', 'Estagiário' ].each do |theme| %>
<%= radio_button_tag 'theme', theme, #theme == theme, :class => "radio"%>
<%= theme %><br>
<% end %>
</input>
<td><%= text_field_tag :q, nil, :placeholder => 'Palavra-chave' %>
<%= submit_tag("Pesquisar") %></td>
</tr>
</table>
<% end %>
<% if #theme == "Analista"%>
<%= render "search_systems_analyst" %>
<% elsif #theme == "Estagiário" %>
<%= render "search_intern" %>
<% end %>
Ps: Sorry for my english
Try with this
redirect_to :root_path(theme: params[:theme], q: params[:q])
Lorena,
You have to pass in the redirect_to like NitinJ said.
redirect_to :root_path, theme: params[:theme], q: params[:q]
You are sending to the action search "params[:theme] and params[:q]" from your GET request, but you aren't sending these params to your index action

How to change to view helper method?

I am working on a app where you can add task etc. I know this is kind of weird but I just like to see how others would implement this. How would you change the following code into a helper method and use it?
The original code
<h2>My Tasks</h2>
<% task = #work.tasks %>
<% if task.empty? %>
<%= link_to 'Create a new task', new_task_path %>
<% else %>
<%= render :partial => 'notes/note', :locals => {:note => #note} %>
<% end %>
My way of doing a helper method
def task_check
task = #work.tasks
if task.empty?
link_to 'Create a new task', new_task_path
else
render :partial => 'notes/note', :locals => {:note => #note}
end
end
In my view
<%= #work.task_check %>
Personally, I wouldn't extract this out at all, this is view logic and it belongs in the views. It definitely doesn't belong in a model, but it could arguably be extracted into a helper. I'd change it slightly:
<h2>My Tasks</h2>
<% if #work.tasks.blank? %>
<%= link_to 'Create a new task', new_task_path %>
<% else %>
<%= render :partial => 'notes/note', :locals => {:note => #note} %>
<% end %>
Calling blank? instead of empty? will work even if #work.tasks is nil
.
You can't define a helper in the model. It won't have access to render, link_to or any other controller or view methods. So just define your method almost exactly as is in a file in your helpers directory, maybe application_helpers.rb or work_helpers.rb:
def task_check(work, note)
task = work.tasks
if task.empty?
link_to 'Create a new task', new_task_path
else
render :partial => 'notes/note', :locals => {:note => note}
end
end
And then call it in your view like so:
<%= task_check(work, note) %>

How do I implement a show all comments feature in ruby on rails?

I'm implementing show/hide feature for users comments.
Discussed here: https://stackoverflow.com/a/10174194/439688
My aim was to:
1. Limit the default shown comments to 2.
2. Have a span with text that states the number of total comments for that particular micropost and when clicked by a user have it expand and show all comments for that micropost. I would be using Jquery/Ajax to hide, show, prepend etc.
The first change was to limit the amount of comments shown to the user and I achieved this by creating a method in my helper called "comments" and here I pass in the id of the micropost the comment belongs to.
def get_comments(micropost_id)
Comment.limit(2).order("created_at DESC").where(:micropost_id => micropost_id)
end
Now the each loop that loops through each comment will only show the 2 most recent comments.
<<% #microposts.each do |m| %>
<% if m.poster_id.nil? %>
<div class="postHolder">
<nav class="micropostOptions">
<ul class="postMenu">
<li class="deletePost"><%= link_to content_tag(:span, "Delete post"), m, :method => :delete, :confirm => "Are you sure?", :title => m.content, :class => "message_delete", :remote => true %>
</li>
<li class="disableCommenting"><%= link_to content_tag(:span, "Pause commenting"), "2" %></li>
<li class="blockCommenter"><%= link_to content_tag(:span, "Block commenter"), "3" %></li>
<li class="openInNewWindow"><%= link_to content_tag(:span, "Open in new window"), "4" %></li>
<li class="reportAbuse"><%= link_to content_tag(:span, "Report abuse"), "5" %></li>
</ul>
</nav>
<%= link_to image_tag(default_photo_for_current_user, :class => "poster_photo"), current_users_username %>
<div class="post_content">
<div class="post_container">
<div class="mainUserNameFontStyle"><%= link_to current_users_username.capitalize, current_users_username %> - <div class="post_time"> <%= time_ago_in_words(m.created_at) %> ago.</div>
</div>
<%= simple_format h(m.content) %> </div>
<div class="commentsCount">
<%= content_tag :span, pluralize(m.comments.count, 'comment'), :class => "view_all_comments" if m.comments.any? %>
</div>
<% if m.comments.any? %>
<% comments(m.id).each do |comment| %>
<div class="comment_container">
<%= link_to image_tag(default_photo_for_commenter(comment), :class => "commenter_photo"), commenter(comment.user_id).username %>
<div class="commenter_content"> <div class="userNameFontStyle"><%= link_to commenter(comment.user_id).username.capitalize, commenter(comment.user_id).username %> - <%= simple_format h(comment.content) %> </div>
</div><div class="comment_post_time"> <%= time_ago_in_words(comment.created_at) %> ago. </div>
</div>
<% end %>
<% end %>
<% if logged_in? %>
<%= form_for #comment, :remote => true do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :micropost_id, :value => m.id %>
<%= f.text_area :content, :placeholder => 'Post a comment...', :class => "comment_box", :rows => 0, :columns => 0 %>
<div class="commentButtons">
<%= f.submit 'Post it', :class => "commentButton", :disable_with => "Post it" %>
<div class="cancelButton"> Cancel </div>
</div>
<% end %>
<% end %>
</div>
</div>
From here this is where it gets confusing for me. I got slightly further using link_to but then decided I'd prefer not to have the url to the comments count show in the browser status bar. This is why I switched to using span.. but now it's not quite easy to do what I wish to do as I can't use the link_to/remote => true now.
How do I make it so when a user clicks the comment count span an ajax call is made pointing to:
def load_comments
#load_comments = Comment.where(:micropost_id => params[:id])
respond_to do |format|
format.js { render :load_comments }
end
end
I thought about putting a click function in users.js but how would I pass the params of the micropost that is in the each loop in the code above into users.js? I don't think it's possible.
All my comment posting is done via ajax but because I used forms for these it was so much easier for me to just add remote => true and create some js templates and do something on success of ajax post.
Not sure if I'm even going about this the right way. I'd appreciate some help/advice from more experienced rails programmers.
Kind regards
Rails partial
#Display all the comments based on local passed to this partial
# Initially pass limit as 2(or whatever you want). then on click of span pass limit as nil. then you can check if limit is nil you can query the model without limit specifier.
<% #comments = Comment.custom_find(#your_params) %>
<% #comments.each do |comment| %>
<%= comment.title %>
<% end %>
javascript/jquery
function load_all_comments(id)
{
new Ajax.Updater('show_comments',
'<%=url_for(:controller => "your_controller", :action => "your_action")%>', {
parameters: {'id':id },
method: 'get',
onSuccess: function(request){
div_comments = document.getElementById("partial_comments_list");
div_comments.innerHTML = request.responseText;
}
});
} // you can call this js function on span click. use jquery if you want.
Controller:
Then inside your_action of your_controller, dont forget to render the partial
render :partial => "show_comments", :layout => false
Edit:
you can even pass locals to your partial
render :partial => "show_comments", :locals => {:post => #post}
Using this every time your partial view will get updated, on the basis of locals you pass.
of course this is just an example not a complete code/solution.
There may be better ways. but this worked fine for me.
Another option is to just output all of the comments and hide the ones you don't want to show first. <div class="hidden_comments" style="display:none;"> a comment </div>
Then just have some javascript to show them when the span is clicked?
<script type="text/javascript">
$("#span_id").click(function() {
$('.hidden_comments').show();
});
</script>
This works great if you do not don't have a ton of comments.
If you really want to do it your way, I have done it before but it gets messy.
Put this in your application.js
$('.comment_span').live('click', function () {
$.get(this.data_url, null, update_row, 'json');
return false;
});
Your span would look like this:
<span class="comment_span" data_url="http://website.com/resource/more_comments">
show all comments
</span>
This example returns the data as json, so I used the update_row function to update replace the comments data.
function update_row(data, status) {
$("#comments-table").append(data.html);
};
Here is what my controller looked like:
def more_comments
#comments = Comments.all
if #comments
respond_to do |format|
format.js {
render :json => {
:html => render_to_string(:partial => "comments"),
}.to_json
}
end
end
end
You should do this via your index action.
Pass a param to it to determine if you want to show all comments or just the current set (I'd use will_paginate to handle this.
Haven't looked too deep into your code as I'm on my phone right now, but something like this:
class CommentsController < ApplicationController
def index
If params[:show_all] == "true"
#comments = Comment.all
else
#comments = Comment.where(foo: bar).paginate(per_page: 2, page: params[:page])
end
end
Then you have it respond to JavaScript and send the page param with your Ajax request

Dynamic Paths in Helper

I'm trying to create a helper method for my admin links. In quite a few
views I have the code
<% if current_user %>
<%= link_to "Edit", edit_model_path(model) %>
<%= link_to "New", new_model_path %>
<%= link_to "Delete", model, :confirm => "You're a Noob", :method
=> :delete %>
<% end %>
that only display these when logged in.
I would like to do something like this in their place
<%= admin_links(model) %>
and pass the current item into the application helper method
def admin_links(m)
if current_user
a = "#{link_to "edit" edit_m_path(m)}"
a << "#{link_to "new" new_m_path}"
a << "#{link_to "Delete", m, :confirm => "Your a Noob", :method
=> :delete}"
end
end
Or something of the like.
basically you need to transform the class name of the model into something pointing to the correct path.
model_name = m.class.to_s.underscore
And then use it to call the appropriate path methods
link_to "edit", send("edit_#{model_name}_path", m)
As an aside, you don't need to put the link_tos in #{} because that function simply returns a string.
Rails provides polymorphic routes to handle this problem: http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html
= link_to "Edit", polymorphic_path(model), :method => :put
I would use a partial for this - instead of a helper. Wherever you want to display these links in your views, simply render the partial:
<%= render :partial => "admin_links", :locals => { :model => model } %>
In _admin_links.html.erb just paste the original code:
<% if current_user %>
<%= link_to "Edit", edit_model_path(model) %>
<%= link_to "New", new_model_path %>
<%= link_to "Delete", model, :confirm => "Your a Noob", :method => :delete %>
<% end %>

Resources