Reputation: 15374
I am trying to find out if I can use a link_to performing an Ajax request within an option value tag
<option value="<%= link_to 'Manage Sandpiper Posts', posts_path(:type => 'Sandpiper'), :remote => true %>">Sandpiper</option>
Because an option value tag only accepts text the output is invalid at the moment and the link obviously doesn't work
What i want to achieve is that upon selecting the option the request is carried out
Does anyone have any ideas how to get around this or what the correct syntax would be if this is possible?
EDIT
I already make this Ajax request from within a dropdown menu when the viewport is in desktop size and it works fine, I achieve this by
<%= link_to 'Sandpiper', posts_path(:type => 'Sandpiper'), :remote => true %>
Firebug returns params of
type = sandpiper
the GET request is
GET posts?type=Sandpiper
This all works fine and the url stays as localhost:3000/posts and my results are returned
Now when i am in mobile view using the select value the returned params are the same as is the get request, difference is the url in the address bar goes to
http://localhost:3000/posts?type=Sandpiper
and there are no results returned.
controller
def index
@posts = Post.all
@tynewyddpost = Post.tynewydd_posts
@woodsidepost = Post.woodside_posts
@sandpiperpost = Post.sandpiper_posts
@outreachpost = Post.outreach_posts
@companypost = Post.company_posts
@staffpost = Post.staff_posts
end
index.js.erb
<% if params[:type] == 'Tynewydd' %>
$('#newsResults').html('<%= escape_javascript(render partial: 'tynewyddposts') %>');
<% end %>
<% if params[:type] == 'Woodside' %>
$('#newsResults').html('<%= escape_javascript(render partial: 'woodsideposts') %>');
<% end %>
I did try changing my controller to match that of @SybariteManoj but that did not work either, though i think im doing the same thing, just in a different way
Upvotes: 0
Views: 2475
Reputation: 3083
I tried to edit Viktor's answer but it will not be visible until peer review it. So I adding the changes here.
<select onchange="follow_link(this)">
<option value="<%= posts_path(:type => 'Sandpiper')%>">Sandpiper</option>
<option value="<%= posts_path(:type => 'Outreach')%>">Outreach</option>
<option value="<%= posts_path(:type => 'Tynewydd ')%>">Tynewydd </option>
...
</select>
Changes Passed this as parameter to the function. and change the value of the option.
follow_link = function() {
var url = $(this).val();
$.get(url);
}
changes Modified the function name and the variable url.
Now to answer your second question, you can have something like this in your posts#index action:
def index
@type = params[:type]
if @type.blank?
@posts = Post.all
else
@posts = Post.where(:type => @type)
end
respond_to do |format|
format.html
format.js
end
end
and add a new file index.js.erb
under your views/posts directory with content something like:
<% if @type == 'Sandpiper' %>
$('#some_container_id').html("<%= escape_javascript(render 'some_partial')")
<% elsif @type == 'Outreach' %>
$('#some_container_id').html("<%= escape_javascript(render 'some_other_partial')")
<% else %>
so on...
<% end %>
Upvotes: 2
Reputation: 8894
i assume the request is a GET with no formdata, you only use the select input to choose a url, but you want behaviour as if the selected url was 'clicked': using jquery:
<select onchange="follow_link(this)">
<option value="<%= posts_path(:type => 'Sandpiper')%>">Sandpiper</option>
...
</select>
then your onchange function:
follow_link = function() {
var url = $(this).val();
$.get(url);
}
Upvotes: 2
Reputation: 417
You need to have :onclick=>"some_function()" instead of :remote=>true.Then in put id on the link and in application.js get the link and get the form that you want to submit and send the form.
UPDATE
try with this in application.rb:
jQuery(function($) {
somemethod();
});
function somemethod() {
jQuery("#id_of_the_option").bind("click", function() {
//do something with the content
});
}
Upvotes: 1