i have the below code, and i just want to link my button to the next view
<button type="submit" class="login-button"><link_to home_page.html.erb></link_to>
<i class="fa fa-chevron-right"></i></button>
how can i fix this
I suppose you have form_tag or form_for somewhere outside your button.
You could do it totally the Rails way like
<%= submit_tag("submit") %>
or if you prefer a link
<%= link_to 'submit', your_path, your_options %>
or a button
<%= button_to 'subimit, your_path, your_options %>
just have a look rails guides and you will find plenty of examples
=> link_to generates a <a> tag, which is not input type="submit".Use button_to, which generates a form with a input type="submit" button to the link.
Assuming path of home_page.html.erb is homepage_path(replace it with your path.)
<%= button_to home_page_path,class: "login-button", method: :get do%>
<i class="fa fa-chevron-right"></i>
<% end %>
Alternatively you can use like this:-
<%= button_to "<i class="fa fa-chevron-right"></i>".html_safe, homepage_path, class: "login-button", method: :get %>
I'm not sure to have understood, but if you need to link to another page you don't need a button, you need a link.
You can add a simple link to that url (not the page, but the route to that page) and style it as a button with css if you want to make it look as you like.
If you need to submit and elaborate some stuff calling a method from your controller, it depends:
1.If your controller is a simple controller you can manage it with a redirect_to at the end of your method
def method
# your elaboration
redirect_to :your_route
end
2.If it is an API controller and you call it from a javascript function, you can add to your callback a window.location.href to your route
success: (data) => {
// your elaboration
window.location.href = "path/to/your/page"
}
Related
I have a search form in the header navbar so that it is always available to users, but I can't get it to send to the correct controller/action. It works correctly when already on the page for search results, when used from any other page it sends to the controller/action for the current page . I found a similar problem here Rails 4 Bootstrap Search Form in Navbar but I did not run into the issue in the same fashion, nor did it help me. Where am I going wrong?
The form in my header navbar
<ul class="navbar-form navbar-left">
<form class="form-inline">
<%= form_tag(search_path, method: :get) do %>
<%=select_tag :gender, options_for_select
([['Male & Female', 'All'],['Male', 'Male'],
['Female', 'Female']]) %>
<%= label_tag "Include unavailable" %>
<%= check_box_tag :unavailable %>
<%= submit_tag "Search", class: "btn btn-success navbar-btn" %>
<% end %>
</form>
</ul>
Routing
get '/search', to: 'searches#search'
I have tried building the form both as form_for and form_tag, as well as URL options with either to force the correct controller/action, to no avail. It only appends the form params to the current url.
What am I missing?
I think I see the issue now.
<form class="form-inline">
<%= form_tag(search_path, method: :get) do %>
is actually putting two form tags on your page. You want to get rid of the outer <form> tag and add a class: 'form-inline' to your form_tag call.
You'll want a name on that route too:
get '/search', to: 'searches#search', as: 'search'
I want to make erb template of searchbar that will call search method of its controller. Different controllers got same method, but will have different logic. Is it possible to make form that will work with every controller just by rendering it on their views?
--- UPDATE ---
I have found that kind of solution:
<%= form_tag "#{controller_name}/search", method: 'get' do %>
<div class="input-group">
<%= text_field_tag :sub_string, '', class: 'form-control', placeholder: "Search..." %>
<span class="input-group-btn">
<%= submit_tag 'Search', class: %w(btn btn-primary) %>
</span>
</div>
<% end %>
However when I hit the submit button, it goes to search action of my current controller and it thows an error:
Couldn't find Basis with 'id'=search
it tries to call set_basis private method, even if it is not called by before_action?
Set search form's controllers name with params[:controller].
Use Rails Guide to see how to pass controller and action for a form_tag or form_for.
I think you can
create the template/partial under views/shared folder
then in controller do something like
def search
render :partial => "shared/search"
end
Ok Then add this
in your partial so you can dynamically change your controller for the form
form_tag({controller: controller.controller_name, action: "search_action"}, method: "post", class: "form")
I would like to have a button as follow:
[ Sign in with FB]
where FB is a font-awesome icon. I tried the following, but couldn't figure out how to embed the icon to the button:
= button_to "Login with", user_omniauth_authorize_path(:facebook)
Here is how font-awesome is normally invoked (in haml):
%i.icon-facebook-sign
How do I achieve the effect I want?
Thank you.
You can pass a block to button_to like so:
= button_to user_omniauth_authorize_path(:facebook) do
Login with
%i.icon-facebook-sign
(Though I'm not sure why you wouldn't just use the Facebook icon itself as the button.)
Add class option for button_to helper
= button_to "Login with", user_omniauth_authorize_path(:facebook), :class => 'icon-facebook-sign'
try this code, it runs for me
<%= button_to line_items_path(product_id: produit.id),class:"btn btn-outline-primary" do %>
<i class="fas fa-shopping-basket"></i>
<% end %>
just change the icon
You can create a helper method that uses the button tag but customizes the output:
#application_helper.rb
def button_to_with_icon(text, path, classes)
form_tag path, :method => :post do
button_tag(classes) do
raw text
end
end
end
Then call the helper method with the raw html embedded as an argument:
<%= button_to_with_icon("login with <i class='fa fa-facebook-official'></i>", { action: "omniauth_authorize", controller: "users" }, class: "btn btn-info") %>
The action, controller, and class settings are just examples. But you can modify this to suit your needs, I think.
Here's the helper I made that works for me:
def html_button_to(html = nil, options = nil, html_options = nil)
button_to(options, html_options) do
html
end
end
Combined with the font-awesome-rails gem, it lets you do:
html_button_to fa_icon(:facebook, text: "Login with"), user_omniauth_authorize_path(:facebook)
<%= link_to 'New Post', new_post_path %>
This produces link to new_post_path. Previously i used <input type="submit" class="new" name="Addlist" value="Add New" /> which resembled like a button. So how can i make the link look like button in erb?
Just to throw another option out there since I had a scenario where the button_to option didn't work. This looks kind of similar to that.
<%= link_to '<button type="button">New Post</button>'.html_safe, new_post_path %>
What I basically wanted is a button that doesn't turn into a submit, since I have multiple buttons on the page that aren't related to a form, and I really just want it to just go to another page.
Take a look at button_to. In summary it will be simmilar to this:
<%= button_to "New Post", { :action => "new" }, :method => :get %>
Although be aware, this method accepts the :method and :confirm modifiers described in the link_to documentation. If no :method modifier is given, it will default to performing a POST operation. You can also disable the button by passing :disabled => true in html_options. If you are using RESTful routes, you can pass the :method to change the HTTP verb used to submit the form.
#Ryan's answer is good but sadly fails html validation http://validator.w3.org/
error: The element button must not appear as a descendant of the a element.
Why not simply apply a (CSS) class to your link and make it appear as a button.
erb:
<%= link_to "Button Text", new_post_path, class: 'button' %>
produces (valid & semantic) HTML:
<a class="button" href="/post/new">Button Text</a>
which you can then style to look like a button.
Example: http://jsfiddle.net/nelsonic/FQK9M/7/
I have a block -iterator to display a user and a related action to be displayed on the same line for every iteration ?
You can visualize like this :-
user1 update_attribute_button
user2 update_attribute_button.
...
and so on.
But if I use a button_to method the button is getting displayed on a newline. which I don't want.heres my code snippet:-
<% #post.bids.each do |bid| %>
<p>
<%= bid.user.email %>
<%= button_to "Offer Bid", offer_bid_post_bid_path(#post, bid), :action => "offer_bid">
</p>
<% end %>
But with the above code the 'email' and 'offer bid' are appearing in two lines, but i want to display them as pairs, with each pair appearing on one line.
I can achieve it using a 'link_to'.
If I use 'link_to' instead of 'button_to' I'm able to achieve my idea, but not able to do it with a button_to. Why is this difference between link_to and button_to.
I want to display the 'offer bid' as a button only.
Now, How to make the button_to buttin appear on the same line as the 'email'.
Please let me know if the question's description is not clear.
Thanks in advance.
A button_to generates a form and a div around the button. So, if you do not restrict the width of the container which is before the button, it will take 100% of the width pushing the button down.
<% #post.bids.each do |bid| %>
<p>
<div style="float: left; width: auto;"><%= bid.user.email %></div>
<%= button_to "Offer Bid", offer_bid_post_bid_path(#post, bid), :action => "offer_bid" %>
</p>
<% end %>
This is not to do with rails but rather how web browser's render forms.
A button_to is just a convenient way to create a form with a non-visible field. If you want the form on the same row as the email address you'll need to put it into a container, most usually a div, set the div to float left and overflow hidden.
button_to renders to a form tag, so I just altered the CSS to ensure the form tag doesn't create a new line.
But to apply it only to a specific form tag then give add form_class: "myButton" see below.
In your something.html.erb
<%= button_to "Offer Bid", offer_bid_post_bid_path(#post, bid), :action => "offer_bid", form_class: "myButton">
Put this in your application.css
myButton {
display: inline;
}