No route matches [POST] "/basic_pages/basic_pages/home" - ruby-on-rails-4

So I would simply like to post to my root. My problem is that if I try to do it the second time (first time works fine) I am posting /basic_pages/basic_pages/home instead of /basic_pages/home. Why does that happen and how do I fix this?
home.html.erb:
<h1>BasicPages#home</h1>
<p>Find me in app/views/basic_pages/home.html.erb</p>
<%= form_tag ('basic_pages/home') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>
routes.rb
Rails.application.routes.draw do
root 'basic_pages#home'
post 'basic_pages/home'
get 'about' => 'basic_pages#about'
end
Hope this provides all the information necessary.

Instead of using form_tag ('basic_pages/home') use form_tag ('/basic_pages/home'). When you have posted the url is getting changed to /basic_pages/home, and then the form is again posting to /basic_pages/basic_pages/home, because of the relative path given to the form.

Try this
Rails.application.routes.draw do
post '/' => "basic_pages#home", as: "root"
get 'about' => 'basic_pages#about'
end
home.html.erb:
<%= form_tag ('/') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>

Related

Rails 4 gem rails4-autocomplete 1 letter suggestions

I am using gem rails-jquery-autocomplete, but It shows suggestions only after 2 letters, is there a possibility to display suggestions for 1-letter long searching?
Yes there is! Just add 'min-length' => 1 in your search form like:
<%= form_tag terms_path, method: 'get' do %>
<%= autocomplete_field_tag :search, params[:search], autocomplete_term_phrase_terms_path, 'min-length' => 1 %>
<%= submit_tag "Search" %>
<% end %>

Rails: param is missing or the value is empty because param appearing as "#<Edge:0xb2535b8>"

I'm using form for and the name of the form in the controller is appearing as "#", but I expect it to appear as :edge.
Here's my controller:
def new
#some_stuff
#edge = Edge.new
#some_stuff
end
def create
#edge = Edge.new(edge_params)
#edge.save
end
def edge_params
params.require(:edge).permit(:location_1_id, :location_2_id)
end
View:
<%= form_for( :edge, :url => {:action => 'create'}) do |f| %>
<ul>
<li>
<%= f.label :location_1_id %>
<%= collection_select(#edge, :location_1_id, #location, :id, :record_as_string) %>
</li>
<%= submit_tag(t(:create_edge)) %>
</ul>
<% end %>
Param req:
{"utf8"=>"✓", "authenticity_token"=>"blah",
"#"=>{"location_1_id"=>"91", "location_2_id"=>"92"},
"commit"=>"Create Edge", "action"=>"create",
"controller"=>"admin/edges", "floor_map_floor_id"=>"1"}
So the name of the parameter should be :edge but it's an object that I can't access.
Can anybody tell me what I'm missing?
Any help is appreciated.
You should be using the form block on your collection set like so
<%= f.collection_select(:location_1_id, #location, :id, :record_as_string) %>
(notice that you are calling the collection_select on the block variable f, and NOT passing in the #edge as the first argument).
In addition, because you are creating the object in new (#edge = Edge.new), you should just be using in your form, like so
<%= form_for( #edge, :url => {:action => 'create'}) do |f| %>
(although using :edge wasn't the cause of your problems, I suspect it was because you were using :edge and #edge in the same form. You need to be consistent, use one or the other)

Indicate that an uploaded file is present in edit form -- paperclip

In my current solution, I am able to put a checkbox in the edit form so that users can delete attachment. However, there is no indication for the user that a file has been uploaded, the name of that file, etc. so that he can decide whether to delete.
Right now the form look like this. The first material is an existing one, the next 3 are due to
def edit
#post = Post.find(params[:id])
3.times { #post.post_materials.new }
end
As you can see, it's very hard to distinguish between them. Ideally, I want the first material file name to appear somehow.
<%= form_for #post, :html => { :multipart => true } do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
Materials:
<ul>
<%= f.fields_for :materials, :html => { :multipart => true } do |materials_form| %>
<li>
<%= materials_form.label :asset %>
<%= materials_form.file_field :asset %>
<%= materials_form.label :_destroy, class: "checkbox inline" do %>
Remove attachment <%= materials_form.check_box :_destroy %>
<% end %>
</li>
<% end %>
</ul>
<%= f.submit "Submit", class: "btn btn-large" %>
<% end %>
Running paperclip's generator creates a migration to add 4 attributes on your model, as you can see here. These attributes are:
<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at
So, If you ran the generator this way: rails generate paperclip post_material asset, on your PostMaterial model, you will have these attributes:
asset_file_name
asset_file_size
asset_content_type
asset_updated_at
Then, on your code you can do something like this:
if materials_form.object.asset.exists? #object represents the current post_material instance
#show a label with object.asset_file_name
else
#render materials_form.file_field :asset
end

Rails - loops and routes, and getting . instead of /

I have three controllers, books, users and contributions. In a view in the books controller I have the following snippet -
<%= link_to contrib.user.username, show_users_path(contrib.user_id) %>
Which returns /users.n rather than /users/n
<%= link_to contrib.user.username, "users/#{contrib.user_id}" %>
does the same thing.
I've read SO questions about this problem with nested resources, and with custom routes, but I've simply got my routes set up as resources, as follows -
devise_for :users, :controllers => { registrations: 'registrations' }
resources :blogs
resources :books
resources :users
resources :contributions
In users/index I have the snippet
<%= link_to user.username, "users/#{user.id}" %>
which works fine. What is going on?
.....
The problem was I'm using an each loop. The whole snippet goes
<% if controller.controller_name == "books" %>
<p><strong>by <%=link_to book.user.username, book.user %></strong></p>
<% book.contributions.each do |contrib| %>
<p><%= link_to contrib.user.username, user_path(contrib.user) %></p>
<%end%>
<% end %>
I changed the fourth line to this -
<p><%= link_to contrib.user.username, user_path(book.contrib.user) %></p>
Which works. Can anyone explain why?
It's because the default path for the show action is object_path(object_id) - there i no default route called show_object_path - I was confusing the path with action.

Adding a Class to a link_to Helper With Only 1 Parameter

I have the following:
<% #users.each do |user| %>
<% if user.profile %>
<%= link_to user do %>
<h2><%= user.profile.first_name %> <%= user.profile.last_name %></h2>
<% end %>
<% end %>
<% end %>
The above code works fine. What this code does is that it will output the first and last names of every user. These names are clickable and will take me to that user's page. My main issue is with the 3rd line. The issue I am having is that I am trying to get rid of the link underline, but I am unsure as to how to pass a class into it. Below is my attempt. My class "no-text-dec" is just one line of "text-decoration: none;"
<%= link_to (user, class: "no-text-dec") do %>
I'm new to Rails, but I understand that link_to has a body, url options, and then html options in that specific order, but how can I make it work in this case? The above line makes my application is crash, but it's the only thing I can think of that makes sense. I'm assuming it's because I am not giving it its body argument, but I'm not sure what that would be.
This should work fine if user contains url/path correct
<%= link_to(user, class: 'some_class') %> do
<span>Delete</span>
<% end %>
The space after method in sending argument in helper method link_to is crashing your application
you can give a try at irb
def test(a,b)
puts a; puts b;
end
test ("Ad","Cd")
It should throw an error