As written in the title, i can't change the method of a link_to to :post. Actually, the html generated includes the expected tag data-method="post", but it sends a GET request.
<% #playlists.each do |pl| %>
<%= link_to new_entry_path(:music_id => #music.id, :playlist_id => pl.id), :method => :post do %>
<span><%= pl.name %></span>
<% end %>
<% end %>
This generates the following html:
<a href="/playlist/1/new_entry/3" data-method="post" rel="nofollow">
<span>dcastro's playlist</span>
</a>
Route:
match 'playlist/:playlist_id/new_entry/:music_id' => 'entries#create', :as => :new_entry, :via => :post
After clicking on the generated link:
No route matches [GET] "/playlist/1/new_entry/3"
Thanks in advance!
Edit: It works if i change the route to :via => :get though, but that's not what i need.
Edit: Nevermind, i fixed it. I had a jQuery code to hide/show this menu, and somewhere in it i used event.stopPropagation, which accidentally prevented unobtrusive javascript from changing the hyperlink method to post.
Are you sure you have jquery-ujs installed and enabled, which makes non-GET requests from hyperlinks?
If you haven't installed it, even the html5 attribute 'data-method' is properly generated, clicking the link sends a normal GET request.
Related
The forum_posts controller is located in app/controllers/forum_threads/forum_posts_controller.rb
I don't know if I have to call forum_threads:forum_posts in the link_to.
Controller:
http://pastebin.com/t9vuyxdP
HTML:
http://pastebin.com/LextuZ74
On a side note, how do you add a button to the link_to? I've tried adding :class => "button" at the end, doesn't cause an error but still just shows a link not a button.
If you want to use link_to with older-style controller/action/id arguments:
<%= link_to "Edit", :controller => "forum_threads/forum_posts", :action => "edit", :id => forum_post.id, :forum_thread_id => #forum_thread.id %>
But Rails prefer newer RESTful routes whenever possible. If you define your controller correctly in routes.rb, you can write link_to like this:
<%= link_to "Edit", edit_forum_thread_forum_post_path(#forum_thread, forum_post) %>
UPDATE 18/11/2015: You forgot to include forum_thread_id in link_to as I saw your route require forum_thread_id and id param to make it work.
For more information on link_to, refer to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
I'm trying to use link_to to send PUT request but every time i send a GET request instead.
I tried :
<%= link_to raw('Valider'), roommates_join_path(:reference => var['reference']), :method => :put, :class => "btn btn-primary" %>
in my browser I get :
<a class="btn btn-primary" data-method="put" href="/roommates/join" rel="nofollow">Valider</a>
my routes.rb:
put 'roommates/join'
When I click :
No route matches [GET] "/roommates/join"
I have the same problem where I use link_to path, method: :put, data: {} and it fire AJAX to GET and PUT.
Change the link_to to button_to to use the actual form instead of using jquery_ujs fixes the problem. Though, I did not really find put the is the cause.
Make sure you include your application's javascript in your layout.
For example, if you are using the default 'application' layout, ensure you include the application javascript in your <head/> like so:
app/views/layouts/application.html.erb
<head>
...
<%= javascript_include_tag "application" %>
...
</head>
It's a little late here so maybe this a trivial question where I'm missing something simple. But when I click a button (with link_to) I created the following gets appended to my URL:
%23<ActionView::Helpers::FormBuilder:0x3ef1fd8>
Why is this, and how can I prevent this? Again, I apologize if this is a shallow question. I can post more information regarding routes and whatnot if that is needed.
Thanks!
Edit: More information as requested.
View:
<%= link_to "Index", welcome_path(f), :class => 'button' %>
with f being part of a form_for loop. I think I'm passing the wrong parameter but I'm unsure.
Relevant Route:
get "index" => 'welcome#show', :as => 'index'
Update:
Thanks for the help everyone. I ended up getting it working by pluralizing my controller (I don't know why I didn't have that before) and utilizing welcome_url instead. That seemed to do the trick.
Check out the very first example and paragraph in the Rails API docs for ActionView::Helpers::FormBuilder:
<%= form_for #person do |f| %>
Name: <%= f.text_field :name %>
Admin: <%= f.check_box :admin %>
<% end %>
What this is saying is that f represents an instantiated FormBuilder object that you are passing to the welcome_path method in your link_to helper.
Typically, you would not mix #index and #show in your routes. Depending on what you want to use the WelcomesController for, you might actually want to route your root_path to welcome_index:
get "welcome/show" => 'welcome#show', :as => 'welcome'
root 'welcome#index'
You should run: $ rake routes in the terminal to get an idea of path view helpers that you can use in your app.
Maybe you're trying to send users to a personalized welcome page. You could have something like this for your corresponding link_to helpers would look best like this:
<%= link_to "Show", welcome_path(#user.id), :class => 'button %>
<%= link_to "Index", root_path, :class => 'button' %>
I am new to rails. Today I encountered a problem that I have no clue how to fix it.
Basically I am trying to put a input area and a submit button on one webpage, and the input values is stored in the params[:name], passing to the export_issues method defined in issues controller.
this is what the view file looks like
<%= form_tag(:controller => 'issues', :action => 'export_issues') do%>
<p>
<%= label_tag :name, "name:" %>
<%= text_field_tag :name, params[:name]%>
</p>
<%= submit_tag "Submit" %>
when i click the 'Submit' I got "Routing error". But if I just press F5 refresh the error pageor type 127.0.0.1/issues/export_issues it will work just as I wanted
and this is the code related to issues controller in routes.rb
resources :issues, :only => [:index, :destroy] do
member do
post 'create_comment'
get 'mark_readed'
end
collection do
get 'export_issues'
delete 'destroy_comment'
end
end
basically what the export_issues does is to read the database and export data to a CSV file.
It worked fine without the form_tag codes taking part in.
So what is the problem ?
The reason is simple. By default form_tag creates form element with method=post. Like:
form_tag('/myposts')
will create form tag as follows:
<form action="/myposts" method="post">
However, our routing says, it accepts only get. So, the form opening tag should be:
<%= form_tag(:controller => 'issues', :action => 'export_issues', :method => :get) do%>
For more information, please consult the apidocs.
Right now I can call a method using ajax (:remote=> 'true') at awisprotect_path by simply clicking on the "x" in this link
<%= link_to "x",
awisprotect_path,
:remote => true,
:method => :post,
%>
The controller action renders jquery so the response is included into the html in the view
<div class="awishanswer">
</div>
That's all working fine. However, instead of having an "x" to click, I wanted the user to click a button and get the same result. So I essentially just wanted to put the link info
<%= link_to "x",
awisprotect_path,
:remote => true,
:method => :post,
%>
into this button
<button class="btn small primary" >
check
</button>
So I created this form and put it in a partial
<%= form_tag(:controller => "sessions", :action => "awisprotect", :remote => true, :method => "post") do %>
<button type="submit" class="btn small secondary">check awis</button>
<% end %>
but the problem is that the controller action that renders js is not putting the result of the action into the html div. Instead, it's redirecting to a blank page and then printing the jquery method with the result that I was checking for with the controller action. the blank page just shows this...
$('div.awishanswer').html(' html to be inserted in div');
Can anyone explain?
In the url it says
http://localhost:3000/awisprotect?method=post&remote=true
in the view file
<div class="awishanswer" id="awishanswer">
<% form_remote_tag :url => {:controller => "sessions", :action => "awisprotect"},
:html => {:method => "post"}, :update => "awishanswer" do %>
<input type="submit" class="btn small primary" value="check" />
<% end %>
</div>
in the action
def awisprotect
#flag = params[:flag] // suppose sending parameter flag from form
// do something
render :partial => 'partial file containing html to send to view'
end
The form will be submitted when the submit button is clicked.
the action will send the html contained in partial file.
the form will update the div with id provided in form with the html code send back from action.
EDIT:partial file
<%if #flag%>
// include some html
<%else%>
// include some other html
<%end%>
The reason your getting a problem is probably because of your usage of the form_tag helper uses the :remote and :method values inside the url generation instead of being handled be the form. The correct usage would probably be like this:
<%= form_tag({:controller => "sessions", :action => "awisprotect"},
:remote => true,
:method => "post")
However, Rails already has a helper method to create a button to submit data called button_to. It basically takes the exact same arguments as the link_to helper so I would probably use it like this in your case:
<%= button_to "x", awisprotect_path, :remote => true, :method => :post %>
The :method argument could possibly even be left out because I think the button_to helper defaults to the POST protocol.
You can disguise a link as a button, using some CSS. Here's a nice article.
This might be better than all these experiments with partials and forms. :-)
I'm thinking you didn't wrap the options for form_tag properly. Try something like this:
form_tag( { :controller => :sessions, :action => :awisprotect, :method => post }, { :remote => true } ) do ....
It may or may not also help to use button_tag.