Undefined Method 'Merge' for '####':String <%= Form_For %> Helper

Undefined method `merge' for '####':string %= form_for % helper

second parameter in hidden_field should be an option hash, not a value

<%= f.hidden_field :original_number, :value => params[:original_number] %>

undefined method `merge' for xxxxxx:String rails form_for

you should write like this because your 2nd parameter should be an option hash, it should not be a value..

<%= form_for @vendor, multipart: true do |f| %>
<%= f.text_field :name, :value => "Store Name" %>
<%= f.text_field :address, :value => "Store Address" %>
<%= f.file_field :image %>
<%= f.submit "Save", class: "btn btn-success" %>
<% end %>

How do I fix undefined method `merge!' for a-sample-blog-post:String

It's telling you what the error is:

 undefined method `merge!' for "a-sample-blog-post":String

And that's only happening during the post :create, :post => blog_post section of your test which means it's directly related to creating a new Post object (and has nothing to do with the let(:user) section).

Ruby's merge method only works on hashes, but the params you are trying to merge the :user entry into is a string which is incompatible.

What I'm not seeing is how you're associating users with posts. For example, you should probably have a user_id field in the Post model which ties a user to a specific post (to tell you who created that post).

So rewriting this a bit:

class Post < ActiveRecord::Base
# tell Rails what we should let the user update via a form
attr_accessible :title, :content

# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# user_id :integer
# title :string(255)
# content :text
# created_at :datetime
# updated_at :datetime
end

In your PostsController

def create
@post = Post.new(params[:post])
@post.user_id = current_user.id

if @post.save
# great success
else
# Y U NO SAVE?!
end
end

And your params should include :title and :content.

If you want to automatically associate your Factory(:post) with a user, you can do:

Factory.define :post do |post|
post.association :user
post.title "A sample blog post"
post.content "Duis mollis, est non commodo eget lacinia odio sem nec elit."
end

Ruby on Rails, undefined method merge

Use this (if you already have current_user method available):

<%= f.hidden_field :user_id, :value => current_user.id %>

If you don't have current_user method implemented, in your corresponding controller, you can have something like this:

@current_user = User.find(params[:user_id])

Then, in your view, you can do:

<%= f.hidden_field :user_id, :value => @current_user.id %>

Update

After the above conversation, if you want to use session to store the user_id, then you can do something like this.

You can create a SessionsHelper module (which you can include in your ApplicationController) where you can define a log_in method:

  # Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end

(You can also put this: session[:user_id] = user.id in the create action where you create an user.)

You can also define a current_user method in this module:

  # Returns the current logged-in user (if any).
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end

Here are some other useful helper methods that you can add in this module:

  # Returns true if the given user is the current user.
def current_user?(user)
user == current_user
end

# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end

# Logs out the current user.
def log_out
session.delete(:user_id)
@current_user = nil
end

Finally, I would suggest you to take a look at Devise gem which is a very popular authentication solution for Rails application.

undefined method `merge' for nil:NilClass in a form using text_field_tag

You have the table name backwards. HABTM looks for the models alphabetically. Look at the error carefully. It says posts_tags cannot be found. You create tags_posts. So change your table name to posts_tags.



Related Topics



Leave a reply



Submit