pass parameters as POST through path - ruby-on-rails-3

I currently have the following link:
<%= link_to 'New campus', new_campus_path(:university_id => #university.id) %>
and the url I get is:
/campus/new?university_id=1
How can I pass the parameter as POST?
I tried adding method => :post, but no luck there. I appreciate any help. Thanks.

The method parameter of link_to should work - what exactly was wrong with it?
<%= link_to 'New campus', new_campus_path(:university_id => #university.id), method: :post %>
Alternatively, you can try button_to
<%= button_to 'New campus', new_campus_path(:university_id => #university.id), method: :post %>
Edit: in both of these cases, the paremeter will still be added to the query string, but as long as the HTTP verb is correct, this shouldn't make a difference for rails, because access to both is done through the params hash.
If it's really important to not include the parameter, you can create the form yourself and add a hidden field:
<%= form_tag new_campus_path, method: :post %>
<%= hidden_field_tag :university_id, #university.id %>
<%= submit_tag 'New campus' %>
<% end %>
I don't think there's a shortcut for this in rails
Edit 2: As an aside, if I'm guessing correctly, this link is supposed to open a form for creating a new campus with a default university selected. If that's the case, you should really be using GET, because it's just a read action that doesn't cause any side effects.

You can use JQuery. Like this :
$("#my_link").click(function() {
$.ajax({
type: 'POST',
url: '/campus/new?university_id=1'
});
});
You can see the doc here : http://api.jquery.com/click/ and here : http://api.jquery.com/jQuery.post/.

Related

Rails: No route matches [PUT] "/blog/2"

I am creating blog application in rails. I have a common form for creating and updating blog.
This is view of edit and new.html.erb
<%= render :partial => "form"%>
This is view of _form.html.erb blog:
<%= form_for #blog do |f| %>
<%= f.text_field :title, :placeholder => "Title" %><br>
<%= f.cktext_area :article, :placeholder => "Content", :ckeditor => {:toolbar => "MyToolbar"} %>
<%= f.submit %>
<% end %>
My blog is creating successfully but I am getting error on update action. This is my edit and update action in blog controller:
def edit
#blog = Blog.find_by_slug(params[:id])
end
def update
#blog = Blog.find(params[:id]) || not_found
#blog.update_attributes(params[:blog])
redirect_to "/blogs/#{#blog.slug}"
end
When I open form from edit view, and click on update button, it throws error:
No route matches [PUT] "/blog/2"
My routes.rb is:
resources :blogs
get 'blog', to: 'blogs#index'
get '/blog/:id', to: 'blogs#show', as: 'blog'
I am not getting where it is going wrong. I tried to add "url: blogs_path" in form_for, it removes the error but doesn't save the edit changes.
Can anybody help me where I am going wrong here?
Thank you.
Okay. I dont understand why you want to go against conventions. Anyway, using form_for resource would automatically generate action URL as a PUT to /resources/:id if its an update operation.
So to override this you need to do two things.
update your routes to support this:
Add this line to your routes file:
put 'blog/:id' => 'blogs#update', :as => 'update_blog'
It is important that you put this line above your 'resources :blogs` call.
2 . specify the URL to which the form should submit:
You will need to create the form tag like this:
<%= form_for #blog, :url => update_blog_path(#blog) do |f| %>
Try this and let us know.

Submit form from e-mail rails 3

I have an action which sends info to the serve through a POST method and I want it to be trigger from an email through a link.
I'v tried this in my email template:
First, specify the post method in a link_to tag:
<%= link_to 'click here', action_url(:id => #model.id), :method => 'post' %>
but the action is triggered with a GET method and I get a routing error.
And second, do it through a form with a hidden field but the csrf token was not recognized:
<%= form_tag action_url, :method => 'post' do %>
<%= hidden_field_tag :id, #model.id %>
<%= submit_tag 'click here' %>
<% end %>
Is there anyway to do this? Or if there isn't, what's my best option?
Couldn't make the post request, so just add the get route for the action.
Positive part: Worked fine, not complications
Negative part: Had to implement some before_filter methods for security due the parameters were passed through the URL

rails 3 link_to with nested content_tag to create <a href with nested span - how to?

Hi I got a noob question, I want to create the following HTML result:
TEXT<span class="arrow-big"></span>
In the above HTML I want to have text with a span-class to style in an image via css.
When I try the following implementations the result reflects just one part of the needed implementation:
<%= link_to "TEXT", controller_path, :class => "button-big layer" %>
results in:
TEXT
and
<%= link_to(content_tag(:span, "", :class => "arrow-big"), controller_path, :class => "button-big layer") %>
results in:
<span class="arrow-big"></span>
Does anyone know how to accomplish?
You could also nest tags by using alternative syntax for link_to helper
<%= link_to controller_path, :class=> "button-big layer" do %>
Text
<%= content_tag(:span, "", :class => "arrow_big" %>
<% end %>
Simply concatenate your text with the 'span':
<%= link_to(("TEXT" + content_tag(:span, "", :class => "arrow-big")).html_safe,
controller_path,
:class => "button-big layer") %>
You'll need the .html_safe around the concatenation since the + operator will otherwise escape the HTML of the content_tag.
Reading your question I did solve my problem.
Than I propose another way to answer your question.
You could create a helper method to make this kind of link that you need.
It would be something like this
def link_with_text_and_span(href, text, span_options= {}, link_options = {})
span_tag = content_tag(:span, span_options[:content] || '', :class => span_options[:class] || '')
link_to(span_tag, href, :class => link_options[:class] || '')
end
The good about it is that your view will be cleaner.
Then you can just call this helper method in your view
<%= link_with_text_and_span("/controller/action", "TEXT", {class: 'arrow-big'}, class: button-big) %>
PS: This code can be improved for sure, if other users want to, please do it.
Here's another way you could use without the content_tag. Not the cleanest but it works!
<%= link_to '<span class="arrow_big"></span>'.html_safe, controller_path, class: "button-big layer" %>

Rendering dynamic content in rails view?

What's the diference between:
<% content_for :something do %>
and
<% provide :something do %>
#provide works in almost the same manner as #content_for, save that #provide does not concatenate blocks. That is--and the following code is cribbed from the documentation--say you have these #content_fors, possibly in multiple templates but defined in this order:
<%Qcontent_for :navigation do %Q<
<li><%= link_to 'Home', :action => 'index' %></li>
<% end %>
<% content_for :navigation do %>
<li><%= link_to 'Login', :action => 'login' %></li>
<%Qend %>
Now, the following will render both navigation items in an un-ordered list, 'Home' first then 'Login':
<ul><%= content_for :navigation %></ul>
#provide will not concatenate and would have rendered a list with but one element.
Provide will send straight to the layout whereas ONCE content_for will allow addition information to be sent to the yield.
Use provide if you know you will only send once to the layout, use content_for if you will or may dynamically send more information.
It's actually a little more complex than that but the above is the shortened version.
Dave

Rails 3, Form submitting to its controller

I'm new to Rails and I've just spent another hour Googling and not finding an example.
So I have a simple form that I need to submit to an API. So I tried submitting it to the API directly but got advice that I do it in my app so I can do something with the results. Anyway, here's my simple form:
<%= form_tag(:action => 'submit') do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => #referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Everything I'm seeing has forms that that use a model, I have no need to persist this data, just pass it on to the API.
So my thought was I just create an action in the home controller, where this page lives and have the action submit to it but I get a RoutingError and it's: No route matches {:action=>"submit", :controller=>"home"}
So what do I need to put in the Routes.rb? I tried:
namespace :home do
resources :submit
end
No Joy... I'm sure it's simple but I just can't find the right example.
I think that you should have a look at the ruby guides, it's very well explained (but I don't think it talks about API) and it will save you a lot of time in the future, I swear.
Not sure what you mean but I see some wired stuff, so I hope to be useful, but if you're following some tutorials from the net let us know the link.
Basically what I do is always to call an action of a controller (MVC), following this way you should have a controller (?? apis_controller ??) and call one action of it.
So you want to use form_tag instead of form_for because you're not addressing a model, therefor you want to get rid of f. and use suffix _tag (api).
<%= form_tag(send_api_path) do %>
<%= text_field_tag :email, "Your email address..." %>
<%= hidden_field_tag :ref_code, #referralid %>
<%= hidden_field_tag :api_name, 'your_api_name' %>
<%= submit_tag "Enter To Win" %>
<% end %>
Then, in your apis_controller.rb you create an action send where you send and manage your request.
#apis_controller.rb
def send
# TODO: your code here
end
Another thing is to set the routes, something like
#routes.rb
match 'apis/send' => 'apis#send', :as => :send_api
I'm sure this is not 100% working code, but it should be useful
How to call the api? I had I fast look and found this.
When you ask for help it's always good to attach the error you get, this makes it easier for people to understand the problem.