2011-03-07 26 views
17


Tôi bị kẹt trong phòng thí nghiệm Rails for Zombies thứ tư ở bài tập thứ ba. Đây là nhiệm vụ của tôi: Tạo hành động sẽ tạo ra một Zombie mới và sau đó chuyển hướng đến trang hiển thị của zombie được tạo ra. Tôi đã có mảng params sau:Rails for Zombies Lab 4> Bài tập 3

params = { :zombie => { :name => "Greg", :graveyard => "TBA" } } 

tôi đã viết đoạn mã sau như một giải pháp:

def create 
    @zombie = Zombie.create 
    @zombie.name = params[ :zombie [ :name ] ] 
    @zombie.graveyard = params[ :zombie [ :graveyard ] ] 
    @zombie.save 

    redirect_to(create_zombie_path) 
end 

Nhưng khi tôi gửi nó tôi đã nhận lỗi sau:
#<TypeError: can't convert Symbol into Integer>

Tôi biết rằng tôi đã phạm sai lầm nhưng tôi không thể tìm ra nơi. Làm ơn giúp tôi.

+2

'zombies' không phải là một thẻ hữu ích vì bản thân nó không có ý nghĩa. Tôi đã thay đổi nó thành 'rails-for-zombies'. –

+0

@skaffman nếu bạn không thích thẻ 'rails-for-zombies', vui lòng giải thích có vấn đề gì với nó. –

Trả lời

25
def create 
    @zombie = Zombie.create(params[:zombie]) 
    redirect_to @zombie 
end 
+0

Điều này làm việc tốt, cảm ơn bạn! – nosferat

+6

Bạn cũng có thể thực hiện 'redirect_to Zombie.create (params [: zombie])' – jonnii

+0

tiết kiệm thời gian của tôi :) – apis17

4

Trước hết bạn đang làm một sai lầm ở đây

@zombie.name = params[ :zombie [ :name ] ] 

nó phải được

@zombie.name = params[:zombie][:name] 

Bạn có thể cố gắng làm điều này

def create 
    @zombie = Zombie.create(params[:zombie]) 
    redirect_to(create_zombie_path) 
end 

Điều này làm cho mã của bạn DRY, theo wiki

"Don't repeat yourself"(DRY) means that information is located in a single, unambiguous place. For example, using the ActiveRecord module of Rails, the developer does not need to specify database column names in class definitions. Instead, Ruby on Rails can retrieve this information from the database based on the class name.

+0

Bạn có lỗi đánh máy tại @ zombie.name = params [: zombie] [: name]] – jhlllnd

+0

@jhllnd Cảm ơn. – Rohit

3

Tôi không thể làm ra bất kỳ lỗi trong mã của bạn (trừ [, ] thứ nhưng tôi nghĩ rằng nó đến khi dán mã ở đây, vì nó là bộ đồng phục trong suốt và khi tôi thử nó, tôi nhận được một lỗi NoMethodError: undefined method '[]' for :zombie:Symbol), nhưng nó có thể được chắc chắn refractored như thế này:

def create 
    @zombie = Zombie.new(params[:zombie]) 
    if @zombie.save 
    redirect_to @zombie 
    else 
    render :action => :new 
    end 
end 

Kiểm tra xem các params băm từ bộ điều khiển là giống hệt nhau bằng cách sử dụng p params.

0

def create @zombie = Zombie.tạo (params [: zombie]) redirect_to (@zombie) cuối

là câu trả lời đúng

0
def create 
    @zombie = Zombie.create(params[:zombie]) 
    redirect_to(@zombie) 
end 

được câu trả lời đúng

(xin lỗi vì sự bài đôi, didnt nhận thấy mã hộp lần đầu tiên)

0
def create 
    @zombie = Zombie.create(params[:zombie]) 
    redirect_to (zombies_path) 
end 
0

gốc Gợi ý # 1

You'll want to pass params[:zombie] to the Zombie.create method.

gốc Gợi ý # 2

Then use the redirect_to method with the new zombie to send them to the right zombie_path

gốc Gợi ý # 3

Một câu trả lời chính xác nhất có thể là:

def create 
    @zombie = Zombie.create(params[:zombie]) 
    redirect_to zombie_path(@zombie) 
end 
1
def create 
    @zombie = Zombie.create(params.require(:zombie).permit(:name, :graveyard)) 
    redirect_to zombie_path(@zombie) 
end 

Đó là những gì làm việc cho tôi.

+0

Rất tốt, cảm ơn. Đối với những người khác, lưu ý rằng đây là cách mới để thực hiện nó bằng cách sử dụng Tham số mạnh cho Rails 4. Các câu trả lời khác không sai, nhưng chúng đã lỗi thời (và có thể không an toàn) –

1

này làm việc cho tôi

def create 
     @zombie = Zombie.create(zombie_params) 
     redirect_to zombie_path(@zombie) 
end 
0

Kể từ khi zombie_params chức năng đã được xác định, bạn có thể chỉ cần sử dụng nó như là một tham số để các tạo chức năng.

class ZombiesController < ApplicationController 
    def create 
    @zombie= Zombie.create(zombie_params) 
    redirect_to @zombie 
    end 

    private 

    def zombie_params 
    params.require(:zombie).permit(:name, :graveyard) 
    end 
end