Using form_for with relational models [duplicate] - ruby-on-rails-4

I'm having trouble displaying my form at /users/2/friends/new. I'm receiving
undefined method `friends_path' for #<#<Class:0x21f0c14>:0x21ef364>
Here is the beginning of the form
<% form_for(#friend) do |f| %>
And the friends controller
def new
#user = User.find(params[:user_id])
#friend = #user.friends.build
end
This is the route
resources :users do
resources :friends
end
And the relevant path from "rake routes"
users/:user_id/friends/new(.:format) {:controller=>"friends", :action=>"new"}
Any help or insight is greatly appreciated. This is my first rails 3 app.

Try:
user_friends_path(#user)
It's because it's a nested resource:
http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Update:
As for the form, you can do:
<%= form_for [#user, #friend] do |f| %>

Related

Devise edit profile on a custom page

I am really new to rails and i encountered this problem,
so I am using Devise for the authentication, the problem is I have created a separate HTML page file for user to edit their information, and I was wondering how to use it on Devise to update the users' information.
Thank you very much!
You can define the resource and resource_name variables used by Devise, which aren't initialized from outside a "custom" Devise controller and this way use them as in every form, in any controller.
You can add them to your app/helpers/application_helper.rb to make them be available for the most of your views:
module ApplicationHelper
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
end
Or in the case you don't think is needed to use the helper "way", then you can pass the resource_name in your form_for and in your definedurl` option, like:
<%= form_for :user, url: session_path(:user) do |f| %>
...
<% end %>

Nested form_for in rails

I have Test model and User model. Test model is having many users. Test controller is as below.
class TestController
def create
Test.create(testparams)
end
private
def testparams
params.require(:test).permit(:test_name,user_attributes:[:user_name])
end
end
In the above code new Test would be created. I want to create new users for a existing test.How to do that??
You should be able to apply the same principles. Below is a basic framework which you will have to alter depending on your requirements.
test model
accepts_nested_attributes_for :users, allow_destroy: true
tests_controller
def edit
#test = Test.find(params["id"]
#test.users.build
end
def update
#test = Test.find(params["id"]
#test.update(testparams)
end
test view
<%= form_for #test do |f| %>
<%= f.text_field :test_name %>
<%= f.fields_for :users do |uf| %>
<%= uf.text_field :user_name %>
<% end %>
<% end %>

Rendering a form from another controller in Ruby on Rails 4

I am trying to show a form on the contact page, but it is from another controller.
The current code results in "First argument in form cannot contain nil or be empty"
After searching it looks there is a problem with the local hash that is not passed.
How can I correctly pass the locals with this code so that it works?
inquiries_controller.rb
class InquiriesController < ApplicationController
def new
#inquiry = Inquiry.new
end
def create
#inquiry = Inquiry.new(params[:inquiry])
if #inquiry.deliver
render :thank_you
else
render :new
end
end
end
inquiries_form.html.erb
<%= form_for #inquiry do |f|
f.text_field :name
f.text_field :email
f.text_area :message
f.submit "Send"
end %>
static_pages\contact.html.erb
<%= render "inquiries/form",
:inquiry => #inquiry %>
HI try adding this to your StaticPages Controller
class StaticPagesController < ApplicationController
def contact
#inquiry = Inquiry.new
end
end
Its a very common mistake. Also I believe your form may also be wrong unless you are using a gem that allows for that type of form. Let me know if this will fix your error.

Rails 4 Search By Category

I want to put a search form on my homepage at pages#home. I'd like to be able to search my model by category. I haven't been able to find a solution where you're actually putting the logic on a different controller than the models you're searching on. I was wondering if someone could help with the syntax and where it needs to go. Here are my relations:
vendor.rb
class Vendor < ActiveRecord::Base
validates :category_id, presence: true
belongs_to :category
end
category.rb
class Category < ActiveRecord::Base
has_one :vendor
end
pages_controller.rb
def home
#vendors = Vendor.all
end
routes.rb
root 'pages#home'
I'm trying to put the search form on home.html.erb, which is under the pages layouts. Was hoping someone could help with how I can accomplish this. This being (seemingly) a simple type of search, I'd hopefully not have to use a gem.
EDIT: ANSWER
For those searching, here's what worked. Thank you #Vla
vendors_controller.rb
def search
#vendors = Vendor.search(params)
end
pages/home.html.erb (this is my root 'pages#home')
<%= form_tag vendors_search_path, method: :get do |f| %>
<%= text_field_tag :search, params[:search], placeholder: "Search Vendors" %>
<%= select_tag 'category', options_for_select(Category.all.map{|el| [el.name, el.id]}) %>
<%= submit_tag 'Search' %>
<% end %>
routes.rb (make sure to put this near the top)
get 'vendors/search'
vendors/search.html.erb
<% #vendors.each do |vendor| %>
<%= vendor.name %>
<%= vendor.phone %>
<%= vendor.email %>
<% end %>
Ok, You can do it this way:
Go to Your vendor.rb file and add search method like this:
def self.search params
vendors = Vendor.where(category_id: params[:category].to_i) unless params[:category].blank?
vendors
end
Then in Your vendors_controller create search method:
def search
#vendors = Vendor.search(params)
end
Then create form at homepage similar to:
= form_tag search_vendors_path, method: :get, role: 'form' do
.form-group
%label Category
= select_tag 'category', options_for_select(Category.all.map{|el| [el.name, el.id]}), class: 'form-control'
= submit_tag 'Search'
And do not forget to put
get 'vendors/search'
into Your routes and add "search" view.
You can still do the same without search action, with result on Your homepage. Anyway I hope You got the idea.

Rails 4: how do I get ID on the index page?

I have Model1, which has "has_many" rel with Model2. What I need is to create form for Model2 under each object on Model1 index page. Lets say I have Tweets and each tweet has form to add comments. I have troubles in understanding how to do it, how to get Tweet id on index page and so on.
Seems like all you need to do to get #comment form working under each #tweet on index page is:
- #tweets.each do |tweet|
= tweet.name `#to show tweet`
- tweet.comments.each do |comment| `#to show each tweet comment`
= comment.content
= form_for [tweet, Comment.new] do |f| `#form forcomment belonging to this particular tweet`
= f.text_field :content
= f.submit
Hope it helps :)
Let's say tweets has many comments, so create model Tweet and Comment with relationship also create controllers tweets and comments.
In your tweets/index page there are list of tweets, right and script is like this.
<% #tweets.each do |tweet| %>
<%= link_to "show tweet", tweet_path(tweet) %>
<% end %>
show tweet link will take you to show page there you will get tweet id from params. Now you can make comment form in this show page as below
<%= form_for #comment do |f|%>
<%= f.text_area :comment %>
<%= f.submit %>
<% end %>
Routes file:
resources :tweets do
resources :comments
end
I have just provided you some way to work by giving examples of rails-views, so check action in your controllers properly you will do it.